From Wikipedia, the free encyclopedia
A visual representation of how bubble sort works.
Class Sorting algorithm
Data structure Array
Worst case performance O(n2)
Best case performance O(n)
Average case performance O(n2)
Worst case space complexity O(1) auxiliary
Bubble sort, often incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, some other algorithms are more efficient for sorting large lists.
1.2 Rabbits and turtles
1.3 Step-by-step example
2.1 Pseudocode implementation
2.2 Optimizing bubble sort
3 In practice
8 External links
An example on bubble sort. 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.
Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.
The only significant advantage that... [continues]
Cite This Essay
(2012, 02). Bubble Sort. StudyMode.com. Retrieved 02, 2012, from http://www.studymode.com/essays/Bubble-Sort-924675.html
"Bubble Sort" StudyMode.com. 02 2012. 02 2012 <http://www.studymode.com/essays/Bubble-Sort-924675.html>.
"Bubble Sort." StudyMode.com. 02, 2012. Accessed 02, 2012. http://www.studymode.com/essays/Bubble-Sort-924675.html.