0
Find the number of comparisons required in bubble sort of the following list having the 5 numbers : 10 20 30 40 50
Data structure using c example
6 odpowiedzi
- 5
Russ yes sir..
If we don't have any swap operation in a iteration then it means already in sorted order.. So we can break loop..
I did not notice that, till now I was not used that boolean check before. Thanks for the point..
Yogita Dose
So best case : n-1 comparisons (sorted list)
Worst case : n(n-1)/2 comparisons..
+ 1
See here...
https://www.sololearn.com/learn/649/?ref=app
+ 1
But list is already sorted
+ 1
Yogita Dose You asked number of comparisons..
There Must check every element with all other elements for in sorted or not..
If you mean, number of swaps, then no need of any swaps if it in sorted order.
Edit :
Atleast n-1 comparisons (i.e for best case, list in order already)
Maximum n(n-1)/2, for all need swappings.
+ 1
As far as I understand bubble sort, it will take n-1 comparisons for an already sorted list. In the example, it compares 10 with 20, since 10 < 20, no swap. Then it compares 20 with 30, no swap, 30 with 40, no swap and 40 with 50, no swap. 4 comparisons before it finishes
0
Thnxx for answering my question...
I got it now....