HOME

TheInfoList



OR:

Bubble sort, sometimes referred to as sinking sort, is a simple
sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is importa ...
that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the list are repeated until no swaps had to be performed during a pass, meaning that the list has become fully sorted. The algorithm, which is a comparison sort, is named for the way the larger elements "bubble" up to the top of the list. This simple algorithm performs poorly in real world use and is used primarily as an educational tool. More efficient algorithms such as
quicksort Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
,
timsort Timsort is a hybrid, stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was implemented by Tim Peters in 2002 for use in the Python programming language. The algori ...
, or
merge sort In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same ...
are used by the sorting libraries built into popular programming languages such as Python and Java.


Analysis


Performance

Bubble sort has a worst-case and average complexity of O(n^2), where n is the number of items being sorted. Most practical sorting algorithms have substantially better worst-case or average complexity, often O(n\log n). Even other O(n^2) sorting algorithms, such as
insertion sort Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Howev ...
, generally run faster than bubble sort, and are no more complex. For this reason, bubble sort is rarely used in practice. Like
insertion sort Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Howev ...
, bubble sort is
adaptive Adaptation, in biology, is the process or trait by which organisms or population better match their environment Adaptation may also refer to: Arts * Adaptation (arts), a transfer of a work of art from one medium to another ** Film adaptation, a ...
, giving it an advantage over algorithms like
quicksort Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
. This means that it may outperform those algorithms in cases where the list is already mostly sorted (having a small number of inversions), despite the fact that it has worse average-case time complexity. For example, bubble sort is O(n) on a list that is already sorted, while quicksort would still perform its entire O(n \log n) sorting process. While any sorting algorithm can be made O(n) on a presorted list simply by checking the list before the algorithm runs, improved performance on almost-sorted lists is harder to replicate.


Rabbits and Turtles

The distance and direction that elements must move during the sort determine bubble sort's performance because elements move in different directions at different speeds. An element that must move toward the end of the list can move quickly because it can take part in successive swaps. For example, the largest element in the list will win every swap, so it moves to its sorted position on the first pass even if it starts near the beginning. On the other hand, an element that must move toward the beginning of the list cannot move faster than one step per pass, so elements move toward the beginning very slowly. If the smallest element is at the end of the list, it will take n -1 passes to move it to the beginning. This has led to these types of elements being named rabbits and turtles, respectively, after the characters in Aesop's fable of
The Tortoise and the Hare "The Tortoise and the Hare" is one of Aesop's Fables and is numbered 226 in the Perry Index. The account of a race between unequal partners has attracted conflicting interpretations. The fable itself is a variant of a common folktale theme in wh ...
. Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort. Cocktail sort is a bi-directional bubble sort that goes from beginning to end, and then reverses itself, going end to beginning. It can move turtles fairly well, but it retains O(n^2) worst-case complexity.
Comb sort Comb sort is a relatively simple sorting algorithm originally designed by WÅ‚odzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble ...
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 Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
.


Step-by-step example

Take an array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. 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 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. ;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 the algorithm does not know if it is completed. The algorithm needs one additional 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 )


Implementation


Pseudocode implementation

In
pseudocode In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
the algorithm can be expressed as (0-based array): procedure bubbleSort(A : list of sortable items) n := length(A) repeat swapped := false for i := 1 to n-1 inclusive do /* if this pair is out of order */ if A -1> A then /* swap them and remember something changed */ swap(A -1 A swapped := true end if end for until not swapped end procedure


Optimizing bubble sort

The bubble sort algorithm can be optimized by observing that the ''n''-th pass finds the ''n''-th largest element and puts it into its final place. So, the inner loop can avoid looking at the last ''n'' − 1 items when running for the ''n''-th time: procedure bubbleSort(A : list of sortable items) n := length(A) repeat swapped := false for i := 1 to n - 1 inclusive do if A - 1> A then swap(A - 1 A swapped := true end if end for n := n - 1 until not swapped end procedure More generally, it can happen that more than one element is placed in their final position on a single pass. In particular, after every pass, all elements after the last swap are sorted, and do not need to be checked again. This allows to skip over many elements, resulting in about a worst case 50% improvement in comparison count (though no improvement in swap counts), and adds very little complexity because the new code subsumes the "swapped" variable: To accomplish this in pseudocode, the following can be written: procedure bubbleSort(A : list of sortable items) n := length(A) repeat newn := 0 for i := 1 to n - 1 inclusive do if A - 1> A then swap(A - 1 A newn := i end if end for n := newn until n ≤ 1 end procedure Alternate modifications, such as the cocktail shaker sort attempt to improve on the bubble sort performance while keeping the same idea of repeatedly comparing and swapping adjacent items.


Use

Although bubble sort is one of the simplest sorting algorithms to understand and implement, its ''O''(''n''2) complexity means that its efficiency decreases dramatically on lists of more than a small number of elements. Even among simple ''O''(''n''2) sorting algorithms, algorithms like
insertion sort Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Howev ...
are usually considerably more efficient. Due to its simplicity, bubble sort is often used to introduce the concept of an algorithm, or a sorting algorithm, to introductory computer science students. However, some researchers such as
Owen Astrachan Owen Astrachan is an American computer scientist and professor of the practice of computer science at Duke University, where he is also the department's director of undergraduate studies. He is known for his work in curriculum development and meth ...
have gone to great lengths to disparage bubble sort and its continued popularity in computer science education, recommending that it no longer even be taught. The
Jargon File The Jargon File is a glossary and usage dictionary of slang used by computer programmers. The original Jargon File was a collection of terms from technical cultures such as the MIT AI Lab, the Stanford AI Lab (SAIL) and others of the old ARPANET ...
, which famously calls
bogosort In computer science, bogosort (also known as permutation sort, stupid sort, slowsort or bozosort) is a sorting algorithm based on the generate and test paradigm. The function successively generates permutations of its input until it finds one t ...
"the archetypical icperversely awful algorithm", also calls bubble sort "the generic bad algorithm".
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of computer sci ...
, in ''
The Art of Computer Programming ''The Art of Computer Programming'' (''TAOCP'') is a comprehensive monograph written by the computer scientist Donald Knuth presenting programming algorithms and their analysis. Volumes 1–5 are intended to represent the central core of comp ...
'', concluded that "the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems", some of which he then discusses.
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of computer sci ...
. ''
The Art of Computer Programming ''The Art of Computer Programming'' (''TAOCP'') is a comprehensive monograph written by the computer scientist Donald Knuth presenting programming algorithms and their analysis. Volumes 1–5 are intended to represent the central core of comp ...
'', Volume 3: ''Sorting and Searching'', Second Edition. Addison-Wesley, 1998. . Pages 106–110 of section 5.2.2: Sorting by Exchanging. " though the techniques used in the calculations o analyze the bubble sortare instructive, the results are disappointing since they tell us that the bubble sort isn't really very good at all. Compared to straight insertion �� bubble sorting requires a more complicated program and takes about twice as long!" (Quote from the first edition, 1973.)
Bubble sort is asymptotically equivalent in running time to insertion sort in the worst case, but the two algorithms differ greatly in the number of swaps necessary. Experimental results such as those of Astrachan have also shown that insertion sort performs considerably better even on random lists. For these reasons many modern algorithm textbooks avoid using the bubble sort algorithm in favor of insertion sort. Bubble sort also interacts poorly with modern CPU hardware. It produces at least twice as many writes as insertion sort, twice as many cache misses, and asymptotically more branch mispredictions. Experiments by Astrachan sorting strings in
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's most ...
show bubble sort to be roughly one-fifth as fast as an insertion sort and 70% as fast as a
selection sort In computer science, selection sort is an in-place comparison sorting algorithm. It has an O(''n''2) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is n ...
. In computer graphics bubble sort 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 (2''n''). 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 the ''x'' axis) and with incrementing ''y'' their order changes (two elements are swapped) only at intersections of two lines. Bubble sort is a stable sort algorithm, like insertion sort.


Variations

*
Odd–even sort In computing, an odd–even sort or odd–even transposition sort (also known as brick sort or parity sort) is a relatively simple sorting algorithm, developed originally for use on parallel processors with local interconnections. It is a compar ...
is a parallel version of bubble sort, for message passing systems. * Passes can be from right to left, rather than left to right. This is more efficient for lists with unsorted items added to the end. * Cocktail shaker sort alternates leftwards and rightwards passes. * ''I can't believe it can sort'' is a sorting algorithm that appears to be an incorrect version of bubble sort, but can be formally proven to work in a way more akin to insertion sort.


Debate over name

Bubble sort has been occasionally referred to as a "sinking sort". For example, Donald Knuth describes the insertion of values at or towards their desired location as letting " he valuesettle to its proper level", and that "this method of sorting has sometimes been called the ''sifting'' or ''sinking'' technique. This debate is perpetuated by the ease with which one may consider this algorithm from two different but equally valid perspectives: # The ''larger'' values might be regarded as ''heavier'' and therefore be seen to progressively ''sink'' to the ''bottom'' of the list # The ''smaller'' values might be regarded as ''lighter'' and therefore be seen to progressively ''bubble up'' to the ''top'' of the list.


In popular culture

In 2007, former
Google Google LLC () is an American multinational technology company focusing on search engine technology, online advertising, cloud computing, computer software, quantum computing, e-commerce, artificial intelligence, and consumer electronics. I ...
CEO
Eric Schmidt Eric Emerson Schmidt (born April 27, 1955) is an American businessman and software engineer known for being the CEO of Google from 2001 to 2011, executive chairman of Google from 2011 to 2015, executive chairman of Alphabet Inc. from 2015 to 20 ...
asked then-presidential candidate
Barack Obama Barack Hussein Obama II ( ; born August 4, 1961) is an American politician who served as the 44th president of the United States from 2009 to 2017. A member of the Democratic Party, Obama was the first African-American president of the U ...
during an interview about the best way to sort one million
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language o ...
s; Obama paused for a moment and replied: "I think the bubble sort would be the wrong way to go."


Notes


References

*
Thomas H. Cormen Thomas H. Cormen is the co-author of ''Introduction to Algorithms'', along with Charles Leiserson, Ron Rivest, and Cliff Stein. In 2013, he published a new book titled '' Algorithms Unlocked''. He is a professor of computer science at Dartm ...
,
Charles E. Leiserson Charles Eric Leiserson is a computer scientist, specializing in the theory of parallel computing and distributed computing, and particularly practical applications thereof. As part of this effort, he developed the Cilk multithreaded language. H ...
,
Ronald L. Rivest Ronald Linn Rivest (; born May 6, 1947) is a cryptographer and an Institute Professor at MIT. He is a member of MIT's Department of Electrical Engineering and Computer Science (EECS) and a member of MIT's Computer Science and Artificial Int ...
, and Clifford Stein. ''
Introduction to Algorithms ''Introduction to Algorithms'' is a book on computer programming by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The book has been widely used as the textbook for algorithms courses at many universities and is ...
'', Second Edition. MIT Press and McGraw-Hill, 2001. . Problem 2-2, pg.40.
Sorting in the Presence of Branch Prediction and Caches
* Fundamentals of Data Structures by Ellis Horowitz,
Sartaj Sahni Professor Sartaj Kumar Sahni (born July 22, 1949, in Pune, India) is a computer scientist based in the United States, and is one of the pioneers in the field of data structures. He is a distinguished professor in the Department of Computer and ...
and Susan Anderson-Freed *
Owen Astrachan Owen Astrachan is an American computer scientist and professor of the practice of computer science at Duke University, where he is also the department's director of undergraduate studies. He is known for his work in curriculum development and meth ...

Bubble Sort: An Archaeological Algorithmic Analysis
*Computer Integrated Manufacturing by Spasic PhD, Srdic MSc, Open Source, 1987br>


External links

* – graphical demonstration * (Java applet animation) * {{sorting Articles with example pseudocode Sorting algorithms Comparison sorts Stable sorts no:Sorteringsalgoritme#Boblesortering