You are on page 1of 12

Design and Analysis of Algorithms

Lecture 9:
Basic Sorting Algorithms

Tauseef Iftikhar
Department of Computer Science
Government College University, Lahore.

Todays Agenda

Sorting Problem
Insertion Sort Analysis
Bubble Sort Analysis
Selection Sort Analysis
Merge Analysis

Sorting

Sorting the sequence is to arrange its elements such that they


are in increasing (or decreasing) order

If the sequence is stored in 1-dimensional array then a sorted


array is: a1 a2 . . . an or a1 a2 . . . an
Types of sorting:

Internal sort: the sequence is stored in a random access


memory
External sort: the sequence could be stored in a secondary
storage

We will study only internal sorting algorithms

Properties of Sorting Algorithms

Efficiency: use minimum resources

In place: use no extra space in memory (at most for single


element)

Stability: order of same elements in unsorted array must be


preserved in sorted array.

Naturalness: at most as much operations should be


performed as much elements are out of order.

Insertion sort: Running time analysis

Algorithm 1 Insertion sort


1: for j 2, n do
2:
key A[j]
3:
i j 1
4:
while i > 0 AND A[i] > key do
5:
A[i + 1] A[i]
6:
i i 1
7:
end while
8:
A[i + 1] key
9: end for
T (n) = 5n 3 +

Pn

j=2 f (j)

Pn

j=2 (f (j)

Line no.
1
2
3
4
5
6
7
8
9
1) +

Iterations
2n
n1
n
Pn 1
f (j)
Pj=2
n
f (j) 1
Pj=2
n
j=2 f (j) 1
n1

Pn

j=2 (f (j)

1)

Insertion sort: Running time analysis


Best-case: When array is already sorted
f (j) = 1
P
P
P
T (n) = 5n 3 + nj=2 1 + nj=2 1 1 + nj=2 1 1
T (n) = 5n 3 + n 1 + n 2 + n 2
T (n) = 8n 8
T (n) = (n)
Worst-case: When array is in reverse order
f (j) = j
P
P
P
T (n) = 5n 3 + nj=2 j + nj=2 j 1 + nj=2 j 1
Pn
P
n(n1)
1 and
since nj=2 j = n(n+1)
j=2 j 1 =
2
2
T (n) = 5n 3 + n(n+1)
1+
2
2
T (n) = 5n 3 + O(n )
T (n) = O(n2 )

n(n1)
2

n(n1)
2

Bubble sort: Running time analysis

Algorithm 2 Bubble sort


1: for i 1, n do
2:
for j n, i + 1 do
3:
if A[j] < A[j 1] then
4:
A[j] A[j 1]
5:
end if
6:
end for
7: end for
T (n) = 2n + 2 +

Pn

i=1 f (i)

Line no.
1
2
3
4
5
6

Pn

i=1 f (i)

Iterations
2(n + 1)
P
n
f (i)
Pi=1
n
i=1 f (i) 1
g (n)

1 + g (n)

Bubble sort: Running time analysis


We see that both loop will run mandatory. Inner loop depends on
outer loop.
f (i) = n i + 1
P
n
i=1 (n i +
P
P1)
P
= Pni=1 n Pni=1 i + Pni=1 1
= ni=1 n ni=1 i + ni=1 1
+n
= n2 n(n+1
2
n2 +n
2
=n 2 +n
2
= n2 n2 n2 + n
2
= n2 + n2 = n(n+1)
2
Running time:
T (n) = 2n + 2 + n(n+1)
+ n(n+1)
1 + g (n)
2
2

Bubble sort: Running time analysis


Best-case: When array is already sorted
g (n) = 0
T (n) = 2n + 2 + n(n+1)
+ n(n+1)
1+1
2
2
n(n+1)
n(n+1)
T (n) = 2n + 3 + 2 + 2 1
T (n) = (n2 )
Worst-case: When array is in reverse order
g (n) = n(n+1)
1
2
T (n) = 2n + 2 + n(n+1)
+ 2 n(n+1)
1
2
2
2
T (n) = O(n )
Hence T (n) = (n2 )

Selection sort: Running time analysis

Algorithm 3 Selection sort


1: for j 1ton 1 do
2:
smallest j
3:
for i j + 1, n do
4:
if A[i] < A[smallest] then
5:
smallest i
6:
end if
7:
end for
8:
A[j] A[smallest]
9: end for
T (n) = 4n 2 +

Pn

j=1 f (j)

Line no.
1
2
3
4
5
8

Pn

j=1 f (j)

1 + g (n)

Iterations
2n
n1
Pn1
f (j)
Pj=1
n1
j=1 f (j) 1
g (n)
n1

Selection sort: Running time analysis

We see that both loop will run mandatory. Inner loop depends on
outer loop.
f (j) = n j + 1
P
n
i=1
P (n j +
P1)
P
= nj=1 n nj=1 i + nj=1 1
+n
= n2 n(n+1
2
n2 +n
2
=n 2 +n
2
= n2 n2 n2 + n
2
= n2 + n2 = n(n+1)
2
Running time:
T (n) = 4n 2 + n(n+1)
+
2

n(n+1)
2

1 + g (n)

Selection sort: Running time analysis

Best-case: When array is already sorted


g (n) = 0
T (n) = 4n 2 + n(n+1)
+ n(n+1)
1
2
2
T (n) = (n2 )
Worst-case: When array is in reverse order
1
g (n) = n(n+1)
2
T (n) = 4n 2 + n(n+1)
+ n(n+1)
1+
2
2
2
T (n) = O(n )
Hence T (n) = (n2 )

n(n+1)
2

You might also like