Bubble and Insertion Sort
Bubble and Insertion Sort
larger
Here
they
the
are
11, 9, 3, 7
inpairs
pairsfrom
first
fromleft
leftto
number
exchanged.
is
toright
exchanging when necessary. Here the first number is comparedto
right
compared to
the second and as it is larger they are exchanged.
Now
6,
Nowthe
larger and 2,
thenext
nextpair
so
pairof
this 9,
ofnumbers
pair is 12,
numbersare
also 11, 9, 3, 7
arecompared.
compared. Again
exchanged.
larger and so this pair is also exchanged.
Againthethe99isisthe
the
In
6,
Inthe
thethird
exchange 2,
is made. 9,
thirdcomparison,
comparison,the
We 12,
the99isisnot
move on to 11, 9, 3, 7
notlarger
largerthan
compare
thanthe
the
the12
next
12so
sono
pair
exchange is made. We move on to compare the next pair without
any
no
without
anychange
changeto tothe
thelist.
list.
The
6,
The12 2,
12isislarger
largerthan9,
thanthe
the11 11,
11so
sothey
theyare 12, 9, 3, 7
areexchanged.
exchanged.
The
Theend
The
endof
6,
Thetwelve
ofthe
2,
twelveisisgreater
thelist
listhas
9,
greaterthan
hasbeen
thanthe
beenreached
11,
the99so
reachedso
sothey
sothis
9, 12, 3, 7
theyare
areexchanged
exchanged
thisisisthe
theend
endof ofthe
thefirst
firstpass.
pass. The
The
6, 2, 9, 11, 9, 3, 12, 7
twelve
twelveatatthe
theend
endof ofthe
thelist
listmust
mustbe belargest
largestnumber
numberin inthe
thelist
listand
andsosoisisnow
nowinin
the The
correct 12 is greater
position.
The 12 We
is greater than
now the 3
start so
a they
new are
pass exchanged.
from left to right.
the correct position. Wethan
now the 3 so
start theypass
a new are exchanged.
from left to right.
The
6,
The12 2,
12isisgreater 9,
greaterthan
thanthe 11,
the77so
sothey
theyare 9, 3, 7, 12
areexchanged.
exchanged.
Bubble Sort Example
First Pass
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
wedodonot
nothave
havetotocompare
comparethe
thelast
lasttwo
two
numbers
numbers as we know the 12 is in position. This pass thereforeonly
as we know the 12 is in position. This pass therefore only
requires 6 comparisons.
requires 6 comparisons.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3,
9, 9,
7, 9,
3, 7, 11, 12
This
Thistime
timethe
the11
11and
and12
12are
arein
inposition.
position. This
Thispass
passtherefore
thereforeonly
only
requires
requires55comparisons.
comparisons.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3,
9, 9,
7, 9,
3, 7, 9, 11, 12
Each
Eachpass
passrequires
requiresfewer
fewercomparisons.
comparisons. This
Thistime
timeonly
only44are
areneeded.
needed.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
2, 6,
3, 3,
6, 7, 9, 9, 11, 12
The
Thelist
listisisnow
nowsorted
sortedbut
butthe
thealgorithm
algorithmdoes
doesnot
notknow
knowthis
thisuntil
untilitit
completes
completesaapasspasswith
withno
noexchanges.
exchanges.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2,
This
Thispass
6,
passno
3,
noexchanges
7,
exchangesarearemade
9,
madesosothe
9,
thealgorithm
11,
algorithmknows
knowsthe
12
thelist
listisis
sorted.
sorted. ItItcan
cantherefore
thereforesave
savetime
timeby
bynot
notdoing
doingthe
thefinal
finalpass.
pass. With
2, 3, 6, 7, 9, 9, 11, 12
With
other lists this check could save much more work.
other lists this check could save much more work.
Sixth Pass
2, 3, 6, 7, 9, 9, 11, 12
Bubble Sort: Algorithm
bubble_sort(a[], no_value)
1. for(i=0;i<no_value;i++)
2. for(j=0;j<no_value-i-1;j++)
3. if(a[j]>a[j+1])
4. temp=a[j];
5. a[j]=a[j+1];
6. a[j+1]=temp;
end
Bubble Sort: Program
Ascending order
Bubble Sort: Analysis
• Running time:
8 20 8 5 10 7 i = 1, first iteration
8 20 20 5 10 7
--- 8 20 5 10 7
Insert Action: i=2
key
5 8 20 5 10 7 i = 2, second iteration
5 8 20 20 10 7
5 8 8 20 10 7
--- 5 8 20 10 7
Insert Action: i=3
key
10 5 8 20 10 7 i = 3, third iteration
10 5 8 20 20 7
--- 5 8 10 20 7
Insert Action: i=4
key
7 5 8 10 20 7 i = 4, forth iteration
7 5 8 10 20 20
7 5 8 10 10 20
7 5 8 8 10 20
--- 5 7 8 10 20
Insertion Sort Program
Insertion Sort Program
Insertion Sort : Analysis
• Running time: