Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort. Cocktail sort does pretty well, but it still retains O(n2) worst-case complexity. Comb sort compares elements large gaps apart 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.
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.
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 )
(1 4 5 2 8 ) (1 4 2 5 8 )
(1 4 2 5 8 ) (1 4 2 5 8 ) Now, since these elements are already in order, 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 )
(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. 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 )
Finally, the array is sorted, and the algorithm can terminate.
procedure bubbleSort(A : list of sortable items ) defined as:
do
swapped := false
for each i in 0 to length(A ) - 1 do:
if A[i ] > A[i + 1 ] then
swap(A[i ], A[i + 1 ] )
swapped := true
end if
end for
while swapped
end procedure
The algorithm can also be expressed as:
procedure bubbleSort(A : list of sortable items ) defined as:
for each i in 1 to length(A) do:
for each j in length(A) downto i + 1 do:
if A[j -1 ] > A[j ] then
swap(A[j - 1], A[j ] )
end if
end for
end for
end procedure
The difference between this and the first pseudocode implementation is discussed later in the article.
In pseudocode, this will cause the following change:
We can then do bubbling passes over increasingly smaller parts of the list. More precisely, instead of doing n2 comparisons (and swaps), we can use only (n-1) + (n-2) + ... + 1 comparisons. This sums up to n(n - 1) / 2, which is still O(n2), but which can be considerably faster in practice.
procedure bubbleSort(A : list of sortable items ) defined as:
n := length(A )
do
swapped := false
n := n - 1
for each i in 0 to n do:
if A[i ] > A[i + 1 ] then
swap(A[i ], A[i + 1 ] )
swapped := true
end if
end for
while swapped
end procedure
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 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, which famously calls bogosort "the archetypical perversely awful algorithm", also calls bubble sort "the generic bad algorithm".
Donald Knuth, in his famous The Art of Computer Programming, 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 discusses therein.
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 requires 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 show bubble sort to be roughly 5 times slower than insertion sort and 40% slower than selection sort.