***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Wednesday, 4 December 2019

Bubble Sort




































Performance of bubble sort:

The best case for bubble sort occurs when the list is already sorted. In this case, bubble sort makes one pass through the list, performing no swaps and N-1 comparisons.

The worst case for bubble sort occurs when the list is in reverse order. In this case, every item will  have to be swapped with every other item on every pass through the algorithm. As in selection sort, there will be O(2comparisons. The number of swaps in the worst case is greater than in selection sort: each comparison results in a swap, so there are O(N 2) swaps in bubble sort.

The average case looks like the worst case: O(2) comparisons and O(2) swaps. 

No comments:

Post a Comment