-1

I have a question about the analysis of the bubble sort algorithm. I created the bubble sort algorithm in Python without including temporary storage. That means it directly swaps the adjacent pairs. how can I analyze the number of copying operations for that version of the bubble sort algorithm? Can someone help me with that if possible? The number of copying operations means the space complexity for the bubble sort algorithm for the worst-case scenario

4
  • 2
    Please edit and include the code you wrote. Don't describe it, show it. Also point out what you try to do and how you attempted to do it. Commented Apr 7 at 8:43
  • "the number of copying operations": do you mean the number of swaps? Do you mean the average number of swaps, the worst case, or for a specific input? Commented Apr 7 at 9:40
  • 1
    You can't directly swap adjacent pairs without temporary storage, at least for one element, unless you are pulling the XOR trick. Commented Apr 7 at 9:50
  • Note that the number of copy operations is not related to the algorithm's space complexity (at least not when implemented "the usual way"). Besides one temp variable for the swap, all the copying/swapping will take place within the array itself, so even if the number of copies/swaps is O(n²), the (auxiliary) space complexity would likely be O(1). Commented Apr 7 at 14:08

1 Answer 1

2

a simple counting variable should do the trick: define a the variable with value zero (i.e. i = 0) before sorting and then increment (i+=1 )whenever you are swapping numbers.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.