Examplealgorithm 1
Examplealgorithm 1
LATEX assignment
Contents
1 Introduction 1
2 Analysis 2
2.1 Performance . . . . . . . . . . . . . . . 2
2.2 Rabbits and turtles . . . . . . . . . . . 2
2.3 Step-by-step example . . . . . . . . . . 2
3 Implementation 3
3.1 Pseudocode implementation . . . . . . 3
3.2 Optimizing bubble sort . . . . . . . . . 3
4 In practice 4
5 Variations 4
1
Class Sorting algorithm because elements move in different directions at dif-
Data structure Array ferent speeds. An element that must move toward the
Worst-case O(n2 ) comparisons end of the list can move quickly because it can take
performance O(n2 ) swaps part in successive swaps. For example, the largest
Best-case O(n) comparisons element in the list will win every swap, so it moves
performance O(1) swaps to its sorted position on the first pass even if it starts
Average O(n2 ) comparisons near the beginning. On the other hand, an element
performance O(n2 ) swaps that must move toward the beginning of the list can-
Worst-case space O(1) auxiliary not move faster than one step per pass, so elements
complexity move toward the beginning very slowly. If the small-
est element is at the end of the list, it will take n − 1
Table 1: Characteristics and complexity of bubble passes to move it to the beginning. This has led to
sort. these types of elements being named rabbits and tur-
tles, respectively, after the characters in Aesop’s fable
of The Tortoise and the Hare.
2 Analysis
Various efforts have been made to eliminate turtles
2.1 Performance to improve upon the speed of bubble sort. Cocktail
sort is a bi-directional bubble sort that goes from
Bubble sort [2] has a worst-case and average complex- beginning to end, and then reverses itself, going end
ity of O(n2 ), where n is the number of items being to beginning. It can move turtles fairly well, but
sorted. Most practical sorting algorithms have sub- it retains O(n2 ) worst-case complexity. Comb sort
stantially better worst-case or average complexity, of- compares elements separated by large gaps, and can
ten O(n log n). Even other O(n2 ) sorting algorithms, move turtles extremely quickly before proceeding to
such as insertion sort, generally run faster than bub- smaller and smaller gaps to smooth out the list. Its
ble sort, and are no more complex. Therefore, bubble average speed is comparable to faster algorithms like
sort is not a practical sorting algorithm. quicksort.
The only significant advantage that bubble sort has
over most other algorithms, even quicksort, but not
insertion sort, is that the ability to detect that the list 2.3 Step-by-step example
is sorted efficiently is built into the algorithm. When
Let us take the array of numbers “5 1 4 2 8”, and
the list is already sorted (best-case), the complexity
sort the array from lowest number to greatest number
of bubble sort is only O(n). By contrast, most other
using bubble sort. In each step, elements written
algorithms, even those with better average-case com-
in bold are being compared. Three passes will be
plexity, perform their entire sorting process on the
required.
set and thus are more complex. However, not only
does insertion sort share this advantage, but it also
performs better on a list that is substantially sorted First Pass
(having a small number of inversions).
Bubble sort should be avoided in the case of large ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ),
collections. It will not be efficient in the case of a Here, algorithm compares the first two elements, and
reverse-ordered collection. 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.
2.2 Rabbits and turtles ( 1 4 2 5 8 ) → ( 1 4 2 5 8 ),
The distance and direction that elements must move Now, since these elements are already in order (8 >
during the sort determine bubble sort’s performance 5), algorithm does not swap them.
2
Second Pass loop can avoid looking at the last n − 1 items when
running for the n-th time:
(14258)→(14258)
( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2
(12458)→(12458) procedure bubbleSort( A : list of sortable
(12458)→(12458) items )
Now, the array is already sorted, but the algorithm n = length(A)
does not know if it is completed. The algorithm repeat
needs one whole pass without any swap to know swapped = false
it is sorted. for i = 1 to n-1 inclusive do
if A[i-1] > A[i] then
swap(A[i-1], A[i])
Third Pass swapped = true
(12458)→(12458) end if
(12458)→(12458) end for
(12458)→(12458) n = n - 1
(12458)→(12458) until not swapped
end procedure
3
Alternate modifications, such as the cocktail shaker sertion sort and 70% as fast as a selection sort [1].
sort attempt to improve on the bubble sort perfor- In computer graphics bubble sort is popular for its
mance while keeping the same idea of repeatedly com- capability to detect a very small error (like swap of
paring and swapping adjacent items. 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
4 In practice lines are sorted by their x coordinate at a specific
scan line (a line parallel to the x axis) and with in-
Although bubble sort is one of the simplest sorting crementing y their order changes (two elements are
algorithms to understand and implement, its O(n2 ) swapped) only at intersections of two lines. Bubble
complexity means that its efficiency decreases dra- sort is a stable sort algorithm, like insertion sort.
matically on lists of more than a small number of ele-
ments. Even among simple O(n2 ) sorting algorithms,
algorithms like insertion sort are usually considerably 5 Variations
more efficient.
Due to its simplicity, bubble sort is often used to • Odd-even sort is a parallel version of bubble sort,
introduce the concept of an algorithm, or a sorting al- for message passing systems.
gorithm, to introductory computer science students. • Cocktail shaker sort is another parallel version
However, some researchers such as Owen Astrachan of the bubble sort
have gone to great lengths to disparage bubble sort
and its continued popularity in computer science ed- • In some cases, the sort works from right to left
ucation, recommending that it no longer even be (the opposite direction), which is more appro-
taught [1]. priate for partially sorted lists, or lists with un-
The Jargon File, which famously calls bogosort sorted items added to the end.
“the archetypical [sic] perversely awful algorithm”,
also calls bubble sort “the generic bad algorithm”.
Donald Knuth, in The Art of Computer Program- 6 Debate over name
ming, concluded that “the bubble sort seems to have
Bubble sort has been occasionally referred to as a
nothing to recommend it, except a catchy name and
“sinking sort”.
the fact that it leads to some interesting theoretical
For example, in Donald Knuth’s The Art of Com-
problems”, some of which he then discusses [3].
puter Programming, Volume 3: Sorting and Search-
Bubble sort is asymptotically equivalent in running
ing he states in section 5.2.1 ‘Sorting by Insertion’,
time to insertion sort in the worst case, but the two
that [the value] “settles to its proper level” and that
algorithms differ greatly in the number of swaps nec-
this method of sorting has sometimes been called the
essary. Experimental results such as those of Astra-
sifting or sinking technique.
chan have also shown that insertion sort performs
This debate is perpetuated by the ease with which
considerably better even on random lists. For these
one may consider this algorithm from two different
reasons many modern algorithm textbooks avoid us-
but equally valid perspectives:
ing the bubble sort algorithm in favor of insertion
sort. 1. The larger values might be regarded as heavier
Bubble sort also interacts poorly with modern and therefore be seen to progressively sink to the
CPU hardware. It produces at least twice as many bottom of the list
writes as insertion sort, twice as many cache misses,
and asymptotically more branch mispredictions. Ex- 2. The smaller values might be regarded as lighter
periments by Astrachan sorting strings in Java show and therefore be seen to progressively bubble up
bubble sort to be roughly one-fifth as fast as an in- to the top of the list
4
Some formulas
Equations (1), (2) and (3) are examples of a
LATEX equations.
s
ln N (s)
U CT (s, a) = Q(s, a) + C × (1)
N (s, a)
Z b Z b Z b
(αf + βg)(x)dx = α f (x)dx + β g(x)dx ,
a a a
(2)
d
X
p = hp1 , ..., pd i) ≈
R(~ Ri (pi ) , (3)
i=1
References
[1] Owen Astrachan. Bubble sort: an archaeological
algorithmic analysis. SIGCSE Bulletin, 35(1):1–
5, 2003.
[2] Thomas H Cormen, Charles E Leiserson,
Ronald L Rivest, and Clifford Stein. Introduc-
tion to algorithms, Second Edition. MIT press,
2009.
[3] Donald E Knuth. The Art of Computer Pro-
gramming: Sorting and Searching, Second Edi-
tion, volume 3. Addison-Wesley, 1998.