Sei sulla pagina 1di 16

Design & Analysis of Algorithms

Lecture#03
Insertion Sort,
Bubble Sort,
Selection Sort
Lecture Contents
Slow Sorting Algorithms
Insertion Sort
Bubble Sort
Selection Sort
Sorting Algorithms …….. Insertion Sort
Say an array (to be sorted) has N elements
We split array into two parts; first part contains sorted elements while
second part contains elements that are not sorted yet
Initially the sorted part contains only one element (as it is sorted obviously)
while second part contains N-1 elements

Sorted Elements Elements not sorted yet


3 4 6 22 98 35 34 18 11 65
Insertion Sort Process
Insertion sort is an iterative process
During every iteration one element (immediately next to sorted part) is taken
from unsorted part and is inserted into sorted part such a way that resultant
sorted part remains sorted with one more elements into it

Sorted Elements Elements not sorted yet


3 4 6 22 98 35 34 18 11 65

Sorted Elements Elements not sorted yet


3 4 6 22 98 35 34 18 11 65
Insertion Sort Process
We keep iterating until that part of list containing unsorted elements keeps
no element at all
At this point resultant list is sorted

6 4 3 22 98 35 34 18 11 65

4 6 3 22 98 35 34 18 11 65

3 4 6 22 98 35 34 18 11 65

3 4 6 11 18 22 34 35 65 98
Sorting Algorithms …….. Insertion Sort
33 10 20 12 8

n=5
i=2
key = 10
j=1
A[j] = 33

Execute the algorithm on board for given


data:
Sorting Algorithms …….. Insertion Sort

How many times each of statements 1-8 will execute?


Sorting Algorithms …….. Insertion Sort
Statement Execution Cost

proc insertionSort(a as array)

n  |a| 1

for i  2 to n do n times

key  a[i] n-1 times

ji–1 n-1 times


while j > 0 and a[j] > key do 1 + 2 + 3 + 4 + …. + n times

a[j+1]  a[j] 1 + 2 + 3 + 4 + …. + n-1 times

jj-1 1 + 2 + 3 + 4 + …. + n-1 times


next

a[j+1]  key n-1 times


next
end proc
Sorting Algorithms …….. Insertion Sort
Calculate the running time (execution cost) of Insertion Sort
T(n) = 1 + n + n-1 + n-1 + (1+2+3+…+n)+ (1+2+3+…+n-1)+ (1+2+3+…+n-1)+n-1
= 1 + n + n-1 + n-1 + n(n+1)/2+ (n-1)n/2+ (n-1)n/2 +n-1
= 1 + n + n-1 + n-1 + n2/2 + n/2+ n2/2 - n/2 + n2/2 - n/2 +n-1
= 3n2/2 + 7n/2 - 2
= O(n2)
Sorting Algorithms …….. Bubble Sort
Execute it on board for array:
proc bubbleSort(A as array) 33 10 20 12 8
n  |A|
for j  1 to n-1 do
for i n to j+1 step -1 do
if A[i-1] > A[i] then
swap (A[i], A[i-1])
end-if
next
next
end proc
Sorting Algorithms …….. Bubble Sort
proc bubbleSort(A as array)
n  |A|
for j  1 to n-1 do
for i n to j+1 step -1 do
if A[i-1] > A[i] then
swap (A[i], A[i-1])
end-if
next
next
end proc

How many times each of above statements will execute?


Sorting Algorithms …….. Bubble Sort
Calculate the running cost of Bubble Sort in class
Statement Execution Cost
proc bubbleSort(A as array)
n  |a|
for j  1 to n-1 do
for i  n to j+1 step -1 do
if a[i-1] > a[i] then
swap (a[i], a[i-1])
end if
next
next
end proc
Sorting Algorithms …….. Selection Sort
33 10 20 12 8

Execute it on board for given data:


Sorting Algorithms …….. Selection Sort

How many times each of statements 1-8 will execute?


Sorting Algorithms …….. Selection Sort
Calculate the running cost of Selection Sort yourself
Sorting Algorithms ….. Videos
1. Selection Sorthttps://www.youtube.com/watch?v=f8hXR_Hvybo

https://www.youtube.com/watch?v=lx9G71uLXIg
2. Insertion Sort
https://www.youtube.com/watch?v=DFG-XuyPYUQ
https://www.youtube.com/watch?v=TwGb6ohsvUU
3. Bubble Sort
https://www.youtube.com/watch?v=Ui97-_n5xjo
https://www.youtube.com/watch?v=Ui97-_n5xjo

Potrebbero piacerti anche