0% found this document useful (0 votes)
13 views21 pages

Bubble and Insertion Sort

The document discusses sorting, defining it as the arrangement of a list in increasing or decreasing order, and categorizes sorting methods into internal and external storage sorting. It highlights various sorting algorithms, including Bubble Sort and Insertion Sort, detailing their processes and examples. Additionally, it provides an analysis of the running time for both algorithms, noting that the worst-case scenario is O(N²) and the best-case scenario is O(N).

Uploaded by

sunidhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

Bubble and Insertion Sort

The document discusses sorting, defining it as the arrangement of a list in increasing or decreasing order, and categorizes sorting methods into internal and external storage sorting. It highlights various sorting algorithms, including Bubble Sort and Insertion Sort, detailing their processes and examples. Additionally, it provides an analysis of the running time for both algorithms, noting that the worst-case scenario is O(N²) and the best-case scenario is O(N).

Uploaded by

sunidhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Sorting

• Sorting refers to one permutation of the list so that


they are increasing or decreasing order.
• The sorting methods are broadly categories:
Internal storage sorting – if the file to be stored
small enough, so that entire sort can be carried
out in the main memory.
External Storage Sorting - if the file to be stored
very large, requiring a large amount of physical
memory , may be sorted on secondary memory.
Types of Sorting Algorithms

• There are many different types of sorting


algorithms, but the primary ones are:

Bubble Sort ●
Radix Sort

Selection Sort ●
Heap Sort

Insertion Sort

Merge Sort

Quick Sort
Bubble Sort
• It is commonly used sorting algorithm.

• Two successive elements are compared and


swapping is performed if the first element is
greater/smaller than the second element.

• Then the elements are sorted in ascending/


descending order.
Bubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
the
6,
Bubblesort
exchanging
second
9,
Bubblesortcompares
when
and as
2,
comparesthe
necessary.
it is
12,
thenumbers
numbersin

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:

– Worst case: O(N2)

– Best case: O(N)


Insertion Sort
• Insertion sort is an efficient algorithm for
sorting a small number of element.

• Insertion is based on the principle of inserting


a particular element in its proper position.

• An example of an insertion sort occurs while


playing cards.
Insert Action: i=1
key

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:

– Worst case: O(N2)

– Best case: O(N)

You might also like