Bubble Sort Example
Bubble Sort Example
9,
6,
6,
6,
6,
6,
6,
6,
6,
9,
2,
2,
2,
2,
2,
2,
2,
2,
9,
9,
9,
9,
9,
9,
12,
12,
12,
12,
11,
11,
11,
11,
11, 9, 3, 7
11, 9, 3, 7
11, 9, 3, 7
11, 9, 3, 7
12, 9, 3, 7
9, 12, 3, 7
9, 3, 12, 7
9, 3, 7, 12
Bubblesort
Bubblesortcompares
comparesthe
thenumbers
numbersin
inpairs
pairsfrom
fromleft
leftto
toright
right
exchanging
when
necessary.
Here
the
first
number
is
compared
exchanging when necessary. Here the first number is comparedto
to
the
second
and
as
it
is
larger
they
are
exchanged.
the second and as it is larger they are exchanged.
Now
Nowthe
thenext
nextpair
pairof
ofnumbers
numbersare
arecompared.
compared. Again
Againthe
the99isisthe
the
larger
and
so
this
pair
is
also
exchanged.
larger and so this pair is also exchanged.
In
Inthe
thethird
thirdcomparison,
comparison,the
the99isisnot
notlarger
largerthan
thanthe
the12
12so
sono
no
exchange
is
made.
We
move
on
to
compare
the
next
pair
without
exchange is made. We move on to compare the next pair without
any
anychange
changeto
tothe
thelist.
list.
The
The12
12isislarger
largerthan
thanthe
the11
11so
sothey
theyare
areexchanged.
exchanged.
The
Thetwelve
twelveisisgreater
greaterthan
thanthe
the99so
sothey
theyare
areexchanged
exchanged
The
Theend
endof
ofthe
thelist
listhas
hasbeen
beenreached
reachedso
sothis
thisisisthe
theend
endof
ofthe
thefirst
firstpass.
pass. The
The
twelve
twelveat
atthe
theend
endof
ofthe
thelist
listmust
mustbe
belargest
largestnumber
numberin
inthe
thelist
listand
andso
soisisnow
nowin
in
The
12
is
greater
than
the
3
so
they
are
exchanged.
the
We
pass
Theposition.
12
is greater
than
the
3aso
they
are from
exchanged.
thecorrect
correct
position.
Wenow
nowstart
start
anew
new
pass
fromleft
leftto
toright.
right.
The
The12
12isisgreater
greaterthan
thanthe
the77so
sothey
theyare
areexchanged.
exchanged.
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
6, 6,
2,
2, 9, 9,
11, 3,
11,
9, 7,
11,
3, 11,
7, 12
Notice
Noticethat
thatthis
thistime
timewe
wedo
donot
nothave
haveto
tocompare
comparethe
thelast
lasttwo
two
numbers
as
we
know
the
12
is
in
position.
This
pass
therefore
numbers as we know the 12 is in position. This pass thereforeonly
only
requires
6
comparisons.
requires 6 comparisons.
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2,
6,
9,
9,
3,
7,
11,
12
Third Pass
2, 6, 9, 3,
9, 7,
3, 9,
9,
7, 11, 12
This
Thistime
timethe
the11
11and
and12
12are
arein
inposition.
position. This
Thispass
passtherefore
thereforeonly
only
requires
requires55comparisons.
comparisons.
6,
Second Pass
2,
Third Pass
2,
Fourth Pass
2,
2,
6,
6,
6,
9,
9,
9,
9,
3,
11, 9, 3, 7,
9, 3, 7, 11,
3, 7, 9, 11,
3, 9,
9,
7,
7, 9, 11,
12
12
12
12
Each
Eachpass
passrequires
requiresfewer
fewercomparisons.
comparisons. This
Thistime
timeonly
only44are
areneeded.
needed.
6,
Second Pass
2,
Third Pass
2,
Fourth Pass
2,
Fifth Pass
2,
2,
6,
6,
6,
6,
3,
9,
9,
9,
3,
3,
6,
11, 9, 3, 7,
9, 3, 7, 11,
3, 7, 9, 11,
7, 9, 9, 11,
7, 9, 9, 11,
12
12
12
12
12
The
Thelist
listisisnow
nowsorted
sortedbut
butthe
thealgorithm
algorithmdoes
doesnot
notknow
knowthis
thisuntil
untilitit
completes
completesaapass
passwith
withno
noexchanges.
exchanges.
6,
Second Pass
2,
Third Pass
2,
Fourth Pass
2,
Fifth Pass
2,
Sixth Pass
2,
2,
6,
6,
6,
3,
3,
9,
9,
9,
3,
6,
6,
11, 9, 3, 7,
9, 3, 7, 11,
3, 7, 9, 11,
7, 9, 9, 11,
7, 9, 9, 11,
7, 9, 9, 11,
12
12
12
12
12
12
This
Thispass
passno
noexchanges
exchangesare
aremade
madeso
sothe
thealgorithm
algorithmknows
knowsthe
thelist
listisis
sorted.
sorted. ItItcan
cantherefore
thereforesave
savetime
timeby
bynot
notdoing
doingthe
thefinal
finalpass.
pass. With
With
other
otherlists
liststhis
thischeck
checkcould
couldsave
savemuch
muchmore
morework.
work.