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

12th Sep 2020, 5:46 PM
Yogita Dose
Yogita Dose - avatar
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..
12th Sep 2020, 8:16 PM
Jayakrishna 🇮🇳
12th Sep 2020, 6:28 PM
Jayakrishna 🇮🇳
+ 1
But list is already sorted
12th Sep 2020, 6:31 PM
Yogita Dose
Yogita Dose - avatar
+ 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.
12th Sep 2020, 6:43 PM
Jayakrishna 🇮🇳
+ 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
12th Sep 2020, 7:55 PM
Russ
Russ - avatar
0
Thnxx for answering my question... I got it now....
13th Sep 2020, 5:39 AM
Yogita Dose
Yogita Dose - avatar