Bubble Sort

Topics: Sorting algorithm, Bubble sort, Selection sort Pages: 6 (1539 words) Published: February 26, 2012
Bubble sort
From Wikipedia, the free encyclopedia
Bubble sort

A visual representation of how bubble sort works.
ClassSorting algorithm
Data structureArray
Worst case performanceO(n2)
Best case performanceO(n)
Average case performanceO(n2)
Worst case space complexityO(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. Contents [hide]

1 Analysis
1.1 Performance
1.2 Rabbits and turtles
1.3 Step-by-step example
2 Implementation
2.1 Pseudocode implementation
2.2 Optimizing bubble sort
3 In practice
4 Variations
5 Misnomer
6 Notes
7 References
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. [edit]Performance

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 bubble sort has over most other implementations, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently built into the algorithm. Performance of bubble sort over an already-sorted list (best-case) is O(n). By contrast, most other algorithms, even those with better average-case complexity, perform their entire sorting process on the set and thus are more complex. However, not only does insertion sort have this mechanism too, but it also performs better on a list that is substantially sorted (having a small number of inversions). [edit]Rabbits and turtles

The positions of the elements in bubble sort will play a large part in determining its performance. Large elements at the beginning of the list do not pose a problem, as they are quickly swapped. Small elements towards the end, however, move to the beginning extremely slowly. This has led to these types of elements being named rabbits and turtles, respectively. Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort. Cocktail sort achieves this goal fairly well, but it retains O(n2) worst-case complexity. Comb sort compares elements separated by large gaps, and can move turtles extremely quickly before proceeding to smaller and smaller gaps to smooth out the list. Its average speed is comparable to faster algorithms like quicksort. [edit]Step-by-step example

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. Three passes will be required. First Pass:

( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them. ( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second Pass:
( 1 4 2 5 8 ) ( 1 4 2 5 8 )...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Insertion Sort Algorithm Implementation Essay
  • Insertion Sort Essay
  • Essay about Shaker Sort Algorithm
  • Performance Analysis of Bubble Sort Using a Modified Diminishing Increment Sorting Essay
  • Bubble Sort Using Assembly Language Essay
  • Water and Bubble Essay
  • External Sort Research Paper
  • Bubbles Essay

Become a StudyMode Member

Sign Up - It's Free