Вы находитесь на странице: 1из 6

Bubble sort, sometimes referred to as sinking sort, is a simple sorting

algorithm that works by repeatedly stepping through the list to be sorted,


comparing each pair of adjacent items and swapping them if they are in the
wrong order. The pass through the list is repeated until no swaps are needed,
which indicates that the list is sorted. The algorithm gets its name from the
way smaller elements "bubble" to the top of the list. Because it only uses
comparisons to operate on elements, it is a comparison sort. Although the
algorithm is simple, most of the other sorting algorithms are more efcient
for large lists.Performanceedit!
Bubble sort has worst"case and a#erage comple$ity both %&n'(, where n is
the number of items being sorted. There e$ist many sorting algorithms with
substantially better worst"case or a#erage comple$ity of )&n log n(. *#en
other %&n'( sorting algorithms, such as insertion sort, tend to ha#e better
performance than bubble sort. Therefore, bubble sort is not a practical sorting
algorithm when n is large.
The only signi+cant ad#antage that bubble sort has o#er most other
implementations, e#en ,uicksort, but not insertion sort, is that the ability to
detect that the list is sorted is efciently built into the algorithm. -hen the
list is already sorted &best"case(, the comple$ity of bubble sort is only )&n(.
By contrast, most other algorithms, e#en those with better a#erage"case
comple$ity, perform their entire sorting process on the set and thus are more
comple$. .owe#er, not only does insertion sort ha#e this mechanism too, but
it also performs better on a list that is substantially sorted &ha#ing a small
number of in#ersions(.
Bubble sort should be a#oided in case of large collections. /t will not be
efcient in case of re#erse ordered collection.
0abbits and turtlesedit!
The positions of the elements in bubble sort will play a large part in
determining its performance. 1arge elements at the beginning of the list do
not pose a problem, as they are ,uickly swapped. 2mall elements towards
the end, howe#er, mo#e to the beginning e$tremely slowly. This has led to
these types of elements being named rabbits and turtles, respecti#ely.
3arious e4orts ha#e been made to eliminate turtles to impro#e upon the
speed of bubble sort. 5ocktail sort is a bi"directional bubble sort that goes
from beginning to end, and then re#erses itself, going end to beginning. /t
can mo#e turtles fairly well, but it retains )&n'( worst"case comple$ity. 5omb
sort compares elements separated by large gaps, and can mo#e turtles
e$tremely ,uickly before proceeding to smaller and smaller gaps to smooth
out the list. /ts a#erage speed is comparable to faster algorithms like
,uicksort.
2tep"by"step e$ampleedit!
1et us take the array of numbers "6 7 8 ' 9", and sort the array from lowest
number to greatest number using bubble sort. /n each step, elements written
in bold are being compared. Three passes will be re,uired.
:irst Pass;
& 6 7 8 ' 9 ( <to & 7 6 8 ' 9 (, .ere, algorithm compares the +rst two elements,
and swaps since 6 = 7.
& 7 6 8 ' 9 ( <to & 7 8 6 ' 9 (, 2wap since 6 = 8
& 7 8 6 ' 9 ( <to & 7 8 ' 6 9 (, 2wap since 6 = '
& 7 8 ' 6 9 ( <to & 7 8 ' 6 9 (, >ow, since these elements are already in order
&9 = 6(, algorithm does not swap them.
2econd Pass;
& 7 8 ' 6 9 ( <to & 7 8 ' 6 9 (
& 7 8 ' 6 9 ( <to & 7 ' 8 6 9 (, 2wap since 8 = '
& 7 ' 8 6 9 ( <to & 7 ' 8 6 9 (
& 7 ' 8 6 9 ( <to & 7 ' 8 6 9 (
>ow, 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;
& 7 ' 8 6 9 ( <to & 7 ' 8 6 9 (
& 7 ' 8 6 9 ( <to & 7 ' 8 6 9 (
& 7 ' 8 6 9 ( <to & 7 ' 8 6 9 (
& 7 ' 8 6 9 ( <to & 7 ' 8 6 9 (
/mplementationedit!
Pseudocode implementationedit!
The algorithm can be e$pressed as &?"based array(;
procedure bubble2ort& A ; list of sortable items (
n @ length&A(
repeat
swapped @ false
for i @ 7 to n"7 inclusi#e do
AB if this pair is out of order BA
if Ai"7! = Ai! then
AB swap them and remember something changed BA
swap& Ai"7!, Ai! (
swapped @ true
end if
end for
until not swapped
end procedure
)ptimiCing bubble sortedit!
The bubble sort algorithm can be easily optimiCed by obser#ing that the n"th
pass +nds the n"th largest element and puts it into its +nal place. 2o, the
inner loop can a#oid looking at the last n"7 items when running for the n"th
time;
procedure bubble2ort& A ; list of sortable items (
n @ length&A(
repeat
swapped @ false
for i @ 7 to n"7 inclusi#e do
if Ai"7! = Ai! then
swap&Ai"7!, Ai!(
swapped @ true
end if
end for
n @ n " 7
until not swapped
end procedure
Dore generally, it can happen that more than one element is placed in their
+nal position on a single pass. /n particular, after e#ery pass, all elements
after the last swap are sorted, and do not need to be checked again. This
allows us to skip o#er a lot of the elements, resulting in about a worst case
6?E impro#ement in comparison count &though no impro#ement in swap
counts(, and adds #ery little comple$ity because the new code subsumes the
"swapped" #ariable;
To accomplish this in pseudocode we write the following;
procedure bubble2ort& A ; list of sortable items (
n @ length&A(
repeat
newn @ ?
for i @ 7 to n"7 inclusi#e do
if Ai"7! = Ai! then
swap&Ai"7!, Ai!(
newn @ i
end if
end for
n @ newn
until n @ ?
end procedure
Alternate modi+cations, such as the cocktail shaker sort attempt to impro#e
on the bubble sort performance while keeping the same idea of repeatedly
comparing and swapping adjacent items.
/n practiceedit!
A bubble sort, a sorting algorithm that continuously steps through a list,
swapping items until they appear in the correct order. The list was plotted in
a 5artesian coordinate system, with each point &$,y( indicating that the #alue
y is stored at inde$ $. Then the list would be sorted by Bubble sort according
to e#ery pi$elFs #alue. >ote that the largest end gets sorted +rst, with smaller
elements taking longer to mo#e to their correct positions.
Although bubble sort is one of the simplest sorting algorithms to understand
and implement, its )&n'( comple$ity means that its efciency decreases
dramatically on lists of more than a small number of elements. *#en among
simple )&n'( sorting algorithms, algorithms like insertion sort are usually
considerably more efcient.
Gue to its simplicity, bubble sort is often used to introduce the concept of an
algorithm, or a sorting algorithm, to introductory computer science students.
.owe#er, some researchers such as )wen Astrachan ha#e gone to great
lengths to disparage bubble sort and its continued popularity in computer
science education, recommending that it no longer e#en be taught.7!
The Hargon +le, which famously calls bogosort "the archetypical sic!
per#ersely awful algorithm", also calls bubble sort "the generic bad
algorithm".'! Gonald Inuth, in his famous book The Art of 5omputer
Programming, concluded that "the bubble sort seems to ha#e nothing to
recommend it, e$cept a catchy name and the fact that it leads to some
interesting theoretical problems", some of which he then discusses.J!
Bubble sort is asymptotically e,ui#alent in running time to insertion sort in
the worst case, but the two algorithms di4er greatly in the number of swaps
necessary. *$perimental results such as those of Astrachan ha#e also shown
that insertion sort performs considerably better e#en on random lists. :or
these reasons many modern algorithm te$tbooks a#oid using the bubble sort
algorithm in fa#or of insertion sort.
Bubble sort also interacts poorly with modern 5PK hardware. /t re,uires at
least twice as many writes as insertion sort, twice as many cache misses, and
asymptotically more branch mispredictions. *$periments by Astrachan
sorting strings in Ha#a show bubble sort to be roughly 6 times slower than
insertion sort and 8?E slower than selection sort.7!
/n computer graphics it is popular for its capability to detect a #ery small
error &like swap of just two elements( in almost"sorted arrays and +$ it with
just linear comple$ity &'n(. :or e$ample, it is used in a polygon +lling
algorithm, where bounding lines are sorted by their $ coordinate at a speci+c
scan line &a line parallel to $ a$is( 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.
3ariationsedit!
)dd"e#en sort is a parallel #ersion of bubble sort, for message passing
systems.
5ocktail sort is another parallel #ersion of the bubble sort
/n some cases, the sort works from right to left &the opposite direction(, which
is more appropriate for partially sorted lists, or lists with unsorted items
added to the end.
Alone bubble sortedit!
Alone bubble sort is a 7LL' modi+cationcitation needed! of the simple
bubble sorting algorithm. Knlike the normal bubble sort where the loop resets
after e#ery performed swap of elements, in the alone bubble sort, the loop
inde$ only returns back by one step thus allowing the swapping to continue
until a smaller #alue element in the array is reached. The following is the
alone bubble realiCation &the algorithm only( in Pascal;

Вам также может понравиться