PlanetMath (more info)
 Math for the people, by the people.
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS  
Login
create new user
name:
pass:
forget your password?
Main Menu
Owner confidence rating: Very high Entry average rating: Very high
bubblesort (Algorithm)

The bubblesort algorithm is a simple and naïve approach to the sorting problem. Let $ \le$ define a total ordering over a list $ A$ of $ n$ values. The bubblesort consists of advancing through $ A$, swapping adjacent values $ A[i]$ and $ A[i+1]$ if $ A[i+1]\le A[i]$ holds. By going through all of $ A$ in this manner $ n$ times, one is guaranteed to achieve the proper ordering.

Pseudocode

The following is pseudocode for the bubblesort algorithm. Note that it keeps track of whether or not any swaps occur during a traversal, so that it may terminate as soon as $ A$ is sorted.


\begin{program} \mathrm{BubbleSort}(A, n, \le) \text{{\bf Input}: List $A$\ of $... ... \mathrm{swap}(A[i], A[i+1]) done \gets \mathbf{false} \FI \OD \OD \end{program}

Analysis

The worst-case scenario is when $ A$ is given in reverse order. In this case, exactly one element can be put in order during each traversal, and thus all $ n$ traversals are required. Since each traversal consists of $ n-1$ comparisons, the worst-case complexity of bubblesort is $ \mathcal{O}(n^2)$.

Bubblesort is perhaps the simplest sorting algorithm to implement. Unfortunately, it is also the least efficient, even among $ \mathcal{O}(n^2)$ algorithms. Bubblesort can be shown to be a stable sorting algorithm (since two items of equal keys are never swapped, initial relative ordering of items of equal keys is preserved), and it is clearly an in-place sorting algorithm.



Anyone with an account can edit this entry. Please help improve it!

"bubblesort" is owned by CWoo. [ full author list (2) | owner history (2) ]
(view preamble | get metadata)

View style:

See Also: sorting problem, stable sorting algorithm, in-place sorting algorithm

Other names:  bubble sort
Log in to rate this entry.
(view current ratings)

Cross-references: in-place sorting algorithm, keys, stable sorting algorithm, relation, ordering, adjacent, total ordering, sorting problem, simple, algorithm
There are 2 references to this entry.

This is version 6 of bubblesort, born on 2002-03-07, modified 2006-07-21.
Object id is 2757, canonical name is Bubblesort.
Accessed 20500 times total.

Classification:
AMS MSC68P10 (Computer science :: Theory of data :: Searching and sorting)

Pending Errata and Addenda
None.
[ View all 4 ]
Discussion
Style: Expand: Order:
forum policy

No messages.

Interact
post | correct | update request | add example | add (any)