TOPICS
Search

Bubble Sort


Bubble sort is a sorting algorithm that repeatedly compares adjacent elements and exchanges them when they are out of order. After one complete pass through a list of n elements, an element of maximum value has moved to its final position. Repeating the process on the remaining prefix sorts the list.

Bubble sort uses O(n^2) comparisons and exchanges in the average and worst cases. In big-O notation, this means that the number of operations is bounded by a constant multiple of n^2 for all sufficiently large n. With a test that stops after a pass containing no exchanges, its best-case running time on an already sorted list is O(n).


See also

Insertion Sort, Sorting

Explore with Wolfram|Alpha

References

Cormen, T. H.; Leiserson, C. E.; Rivest, R. L.; and Stein, C. Introduction to Algorithms, 2nd ed. Cambridge, MA: MIT Press, 2001.Knuth, D. E. The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed. Reading, MA: Addison-Wesley, 1998.

Cite this as:

Weisstein, Eric W. "Bubble Sort." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/BubbleSort.html

Subject classifications