Sunday, July 24, 2016

Bubble Sort



Data structure: Array

Worst case performance: O(n^{2})

Best case performance: O(n)

Average case performance: O(n^{2})




Example:

Array elements - 5 3 1 6 7 8 2 4


Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one is smaller than the former one). After each iteration, one less element (the last one) is needed to be compared until there are no more elements left to be compared. The largest element gets sorted to the end of the list first, with the smaller elements getting sorted with each iteration one by one.

Bubble sort is not a practical sorting algorithm when n is large.


https://en.wikipedia.org/wiki/Bubble_sort

No comments:

Post a Comment