Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 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.

[ad type=”banner”]

Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

Following are C/C++, Python and Java implementations of Bubble Sort.

C and C++

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20for%20implementation%20of%20Bubble%20sort%0A%23include%20%3Cstdio.h%3E%0A%20%0Avoid%20swap(int%20*xp%2C%20int%20*yp)%0A%7B%0A%20%20%20%20int%20temp%20%3D%20*xp%3B%0A%20%20%20%20*xp%20%3D%20*yp%3B%0A%20%20%20%20*yp%20%3D%20temp%3B%0A%7D%0A%20%0A%2F%2F%20A%20function%20to%20implement%20bubble%20sort%0Avoid%20bubbleSort(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20int%20i%2C%20j%3B%0A%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n-1%3B%20i%2B%2B)%20%20%20%20%20%20%0A%20%0A%20%20%20%20%20%20%20%2F%2F%20Last%20i%20elements%20are%20already%20in%20place%20%20%20%0A%20%20%20%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%20n-i-1%3B%20j%2B%2B)%20%0A%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3E%20arr%5Bj%2B1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20swap(%26arr%5Bj%5D%2C%20%26arr%5Bj%2B1%5D)%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20an%20array%20*%2F%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%20%3C%20size%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B64%2C%2034%2C%2025%2C%2012%2C%2022%2C%2011%2C%2090%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20bubbleSort(arr%2C%20n)%3B%0A%20%20%20%20printf(%22Sorted%20array%3A%20%5Cn%22)%3B%0A%20%20%20%20printArray(arr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c and c++” highlight=”” provider=”manual”/] [ad type=”banner”]

Java

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20for%20implementation%20of%20Bubble%20Sort%0Aclass%20BubbleSort%0A%7B%0A%20%20%20%20void%20bubbleSort(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n-1%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20n-i-1%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3E%20arr%5Bj%2B1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20swap%20temp%20and%20arr%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20temp%20%3D%20arr%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%5D%20%3D%20arr%5Bj%2B1%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%2B1%5D%20%3D%20temp%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Prints%20the%20array%20*%2F%0A%20%20%20%20void%20printArray(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%20to%20test%20above%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20BubbleSort%20ob%20%3D%20new%20BubbleSort()%3B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B64%2C%2034%2C%2025%2C%2012%2C%2022%2C%2011%2C%2090%7D%3B%0A%20%20%20%20%20%20%20%20ob.bubbleSort(arr)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Sorted%20array%22)%3B%0A%20%20%20%20%20%20%20%20ob.printArray(arr)%3B%0A%20%20%20%20%7D%0A%7D” message=”java” highlight=”” provider=”manual”/]

Python

[pastacode lang=”python” manual=”%23%20Python%20program%20for%20implementation%20of%20Bubble%20Sort%0A%20%0Adef%20bubbleSort(arr)%3A%0A%20%20%20%20n%20%3D%20len(arr)%0A%20%0A%20%20%20%20%23%20Traverse%20through%20all%20array%20elements%0A%20%20%20%20for%20i%20in%20range(n)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Last%20i%20elements%20are%20already%20in%20place%0A%20%20%20%20%20%20%20%20for%20j%20in%20range(0%2C%20n-i-1)%3A%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20traverse%20the%20array%20from%200%20to%20n-i-1%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Swap%20if%20the%20element%20found%20is%20greater%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20than%20the%20next%20element%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20arr%5Bj%5D%20%3E%20arr%5Bj%2B1%5D%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%5D%2C%20arr%5Bj%2B1%5D%20%3D%20arr%5Bj%2B1%5D%2C%20arr%5Bj%5D%0A%20%0A%23%20Driver%20code%20to%20test%20above%0Aarr%20%3D%20%5B64%2C%2034%2C%2025%2C%2012%2C%2022%2C%2011%2C%2090%5D%0A%20%0AbubbleSort(arr)%0A%20%0Aprint%20(%22Sorted%20array%20is%3A%22)%0Afor%20i%20in%20range(len(arr))%3A%0A%20%20%20%20print%20(%22%25d%22%20%25arr%5Bi%5D)%2C%20″ message=”python” highlight=”” provider=”manual”/]

 

Output

Sorted array: 11 12 22 25 34 64 90

Illustration :

bubble sort

[ad type=”banner”]

Optimized Implementation:
The above function always runs O(n^2) time even if the array is sorted. It can be optimized by stopping the algorithm if inner loop didn’t cause any swap.

[pastacode lang=”c” manual=”%2F%2F%20Optimized%20implementation%20of%20Bubble%20sort%0A%23include%20%3Cstdio.h%3E%0A%20%0Avoid%20swap(int%20*xp%2C%20int%20*yp)%0A%7B%0A%20%20%20%20int%20temp%20%3D%20*xp%3B%0A%20%20%20%20*xp%20%3D%20*yp%3B%0A%20%20%20%20*yp%20%3D%20temp%3B%0A%7D%0A%20%0A%2F%2F%20An%20optimized%20version%20of%20Bubble%20Sort%0Avoid%20bubbleSort(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20int%20i%2C%20j%3B%0A%20%20%20bool%20swapped%3B%0A%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n-1%3B%20i%2B%2B)%0A%20%20%20%7B%0A%20%20%20%20%20swapped%20%3D%20false%3B%0A%20%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%20n-i-1%3B%20j%2B%2B)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3E%20arr%5Bj%2B1%5D)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20swap(%26arr%5Bj%5D%2C%20%26arr%5Bj%2B1%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20swapped%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%2F%2F%20IF%20no%20two%20elements%20were%20swapped%20by%20inner%20loop%2C%20then%20break%0A%20%20%20%20%20if%20(swapped%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20an%20array%20*%2F%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%20%3C%20size%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B64%2C%2034%2C%2025%2C%2012%2C%2022%2C%2011%2C%2090%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20bubbleSort(arr%2C%20n)%3B%0A%20%20%20%20printf(%22Sorted%20array%3A%20%5Cn%22)%3B%0A%20%20%20%20printArray(arr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]

Output:

Sorted array: 11 12 22 25 34 64 90

Worst and Average Case Time Complexity: O(n*n). Worst case occurs when array is reverse sorted.

Best Case Time Complexity: O(n). Best case occurs when array is already sorted.

Auxiliary Space: O(1)

Boundary Cases: Bubble sort takes minimum time (Order of n) when elements are already sorted.

Sorting In Place: Yes

Stable: Yes

Due to its simplicity, bubble sort is often used to introduce the concept of a sorting algorithm.
In computer graphics it is popular for its capability to detect a very small error (like swap of just two elements) in almost-sorted arrays and fix it with just linear complexity (2n). For example, it is used in a polygon filling algorithm, where bounding lines are sorted by their x coordinate at a specific scan line (a line parallel to x axis) and with incrementing y their order changes (two elements are swapped) only at intersections of two lines.

[ad type=”banner”]

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,