You are on page 1of 130

CS1006T Data Structures

Sorting Algorithms

Chandrabose Aravindan
<AravindanC@ssn.edu.in>

Professor of Information Technology


SSN College of Engineering

May 20, 2024

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 1 / 32


Introduction

A sequence of objects can be arranged according to an order imposed


by a partial order ≤
A partial order is a relation among the objects which is reflexive,
anti-symmetric, and transitive
A sequence is sorted when every element xi ≤ xi+1 (except for the
last element!)

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 2 / 32


Inversions

A sequence of objects can be arranged according to an order imposed


by a partial order ≤
A partial order is a relation among the objects which is reflexive,
anti-symmetric, and transitive
Given a sequence of objects, out-of-the-order pairs are known as
inversions
For any two indices i and j, if i < j and A[i] > A[j] then the pair
(A[i], A[j]) is an inversion
A sequence is sorted when there are no inversions in it
Algorithm Idea: Systematically check all the pairs and “correct” the
inversions, if any

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 3 / 32


Basic Sorting Algorithms

Brute-force approach — exhaustive search (search all the pairs)


Selection Sort
Bubble Sort
Divide-Conquer-Merge (Does it help in reducing the number of
comparisons?)
Insertion Sort
Merge Sort
Quick Sort
Hybrid approaches

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 4 / 32


Insertion Sort

Divide and Conquer!


Decompose the sequence to head and tail
Sort the tail
Insert head in to the sorted tail (at the appropriate position)

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 5 / 32


Insertion Sort

Divide and Conquer!


Decompose the sequence to head and tail
Sort the tail
Insert head in to the sorted tail (at the appropriate position)

def i s o r t ( l s t ) :
i f ( l e n ( l s t ) == 0 ) :
return [ ]
else :
return insert ( l s t [0] , isort ( l s t [ 1 : ] ) )

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 5 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))
ins(32, ins(108, ins(14, ins(72, isort([12])))))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))
ins(32, ins(108, ins(14, ins(72, isort([12])))))
ins(32, ins(108, ins(14, ins(72, ins(12, isort([]))))))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))
ins(32, ins(108, ins(14, ins(72, isort([12])))))
ins(32, ins(108, ins(14, ins(72, ins(12, isort([]))))))
ins(32, ins(108, ins(14, ins(72, ins(12, [])))))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))
ins(32, ins(108, ins(14, ins(72, isort([12])))))
ins(32, ins(108, ins(14, ins(72, ins(12, isort([]))))))
ins(32, ins(108, ins(14, ins(72, ins(12, [])))))
ins(32, ins(108, ins(14, ins(72, [12]))))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))
ins(32, ins(108, ins(14, ins(72, isort([12])))))
ins(32, ins(108, ins(14, ins(72, ins(12, isort([]))))))
ins(32, ins(108, ins(14, ins(72, ins(12, [])))))
ins(32, ins(108, ins(14, ins(72, [12]))))
ins(32, ins(108, ins(14, [12, 72])))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))
ins(32, ins(108, ins(14, ins(72, isort([12])))))
ins(32, ins(108, ins(14, ins(72, ins(12, isort([]))))))
ins(32, ins(108, ins(14, ins(72, ins(12, [])))))
ins(32, ins(108, ins(14, ins(72, [12]))))
ins(32, ins(108, ins(14, [12, 72])))
ins(32, ins(108, [12, 14, 72]))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))
ins(32, ins(108, ins(14, ins(72, isort([12])))))
ins(32, ins(108, ins(14, ins(72, ins(12, isort([]))))))
ins(32, ins(108, ins(14, ins(72, ins(12, [])))))
ins(32, ins(108, ins(14, ins(72, [12]))))
ins(32, ins(108, ins(14, [12, 72])))
ins(32, ins(108, [12, 14, 72]))
ins(32, [12, 14, 72, 108])

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

Consider the same sequence [32, 108, 14, 72, 12]


ins(32, isort([108, 14, 72, 12]))
ins(32, ins(108, isort([14, 72, 12])))
ins(32, ins(108, ins(14, isort([72, 12]))))
ins(32, ins(108, ins(14, ins(72, isort([12])))))
ins(32, ins(108, ins(14, ins(72, ins(12, isort([]))))))
ins(32, ins(108, ins(14, ins(72, ins(12, [])))))
ins(32, ins(108, ins(14, ins(72, [12]))))
ins(32, ins(108, ins(14, [12, 72])))
ins(32, ins(108, [12, 14, 72]))
ins(32, [12, 14, 72, 108])
[12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 6 / 32


Insertion Sort

de f i n s e r t ( obj , seq ) :
i f ( l e n ( s e q ) == 0 ) :
return [ obj ]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 7 / 32


Insertion Sort

de f i n s e r t ( obj , seq ) :
i f ( l e n ( s e q ) == 0 ) :
return [ obj ]

e l i f ( o b j <= s e q [ 0 ] ) :
res = [ obj ]
r e s . extend ( seq )
return res

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 7 / 32


Insertion Sort

de f i n s e r t ( obj , seq ) :
i f ( l e n ( s e q ) == 0 ) :
return [ obj ]

e l i f ( o b j <= s e q [ 0 ] ) :
res = [ obj ]
r e s . extend ( seq )
return res

else :
r e s = seq [ : 1 ]
r e s . extend ( i n s e r t ( obj , seq [ 1 : ] ) )
return res

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 7 / 32


Insertion Sort

Consider the case ins(32, [12, 14, 72, 108])

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 8 / 32


Insertion Sort

Consider the case ins(32, [12, 14, 72, 108])


ins(32, [12, 14, 72, 108])

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 8 / 32


Insertion Sort

Consider the case ins(32, [12, 14, 72, 108])


ins(32, [12, 14, 72, 108])
[12]:::ins(32, [14, 72, 108])

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 8 / 32


Insertion Sort

Consider the case ins(32, [12, 14, 72, 108])


ins(32, [12, 14, 72, 108])
[12]:::ins(32, [14, 72, 108])
[12]:::([14]:::ins(32, [72, 108]))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 8 / 32


Insertion Sort

Consider the case ins(32, [12, 14, 72, 108])


ins(32, [12, 14, 72, 108])
[12]:::ins(32, [14, 72, 108])
[12]:::([14]:::ins(32, [72, 108]))
[12]:::([14]:::([32]:::[72, 108]))

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 8 / 32


Insertion Sort

Consider the case ins(32, [12, 14, 72, 108])


ins(32, [12, 14, 72, 108])
[12]:::ins(32, [14, 72, 108])
[12]:::([14]:::ins(32, [72, 108]))
[12]:::([14]:::([32]:::[72, 108]))
[12]:::([14]:::[32, 72, 108])

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 8 / 32


Insertion Sort

Consider the case ins(32, [12, 14, 72, 108])


ins(32, [12, 14, 72, 108])
[12]:::ins(32, [14, 72, 108])
[12]:::([14]:::ins(32, [72, 108]))
[12]:::([14]:::([32]:::[72, 108]))
[12]:::([14]:::[32, 72, 108])
[12]:::[14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 8 / 32


Insertion Sort

Consider the case ins(32, [12, 14, 72, 108])


ins(32, [12, 14, 72, 108])
[12]:::ins(32, [14, 72, 108])
[12]:::([14]:::ins(32, [72, 108]))
[12]:::([14]:::([32]:::[72, 108]))
[12]:::([14]:::[32, 72, 108])
[12]:::[14, 32, 72, 108]
[12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 8 / 32


Analysis of Insertion

(
d n≤1
T (n) =
T (n − 1) + c n>1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 9 / 32


Analysis of Insertion

(
d n≤1
T (n) =
T (n − 1) + c n>1
Time complexity is Θ(n)

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 9 / 32


Analysis of Insertion Sort

(
d n≤1
T (n) =
T (n − 1) + cn n>1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 10 / 32


Analysis of Insertion Sort

(
d n≤1
T (n) =
T (n − 1) + cn n>1

Time complexity is Θ(n2 )

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 10 / 32


Insertion Sort — in-place

It is possible to implement this algorithm as an “in-place” version

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 11 / 32


Insertion Sort — in-place

It is possible to implement this algorithm as an “in-place” version


When the sub-sequence defined by positions 0 · · · j is already sorted,
insert the next object at position i = j + 1 by bubbling it down

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 11 / 32


Insertion Sort — in-place

It is possible to implement this algorithm as an “in-place” version


When the sub-sequence defined by positions 0 · · · j is already sorted,
insert the next object at position i = j + 1 by bubbling it down

def i n s e r t i o n _ s o r t ( l s t ) :
n = len ( l s t )
f o r i in range (1 , n ) :
tmp = l s t [ i ]
j = i − 1
w h i l e ( ( j >= 0 ) and ( l s t [ j ] > tmp ) ) :
l s t [ j +1] = l s t [ j ]
j −= 1
l s t [ j +1] = tmp
return

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 11 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0
h i
32, 108, 14, 72, 12

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0
h i h i
32, 108, 14, 72, 12 → 32, X , 108, 72, 12

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0
h i h i
32, 108, 14, 72, 12 → 32, X , 108, 72, 12 → [X , 32, 108, 72, 12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0
h i h i
32, 108, 14, 72, 12 → 32, X , 108, 72, 12 → [X , 32, 108, 72, 12] →
h i
14, 32, 108, 72, 12

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0
h i h i
32, 108, 14, 72, 12 → 32, X , 108, 72, 12 → [X , 32, 108, 72, 12] →
h i
14, 32, 108, 72, 12
In the next pass, i = 3 and index j runs from 2 to 0

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0
h i h i
32, 108, 14, 72, 12 → 32, X , 108, 72, 12 → [X , 32, 108, 72, 12] →
h i
14, 32, 108, 72, 12
In the next pass, i = 3 and index j runs from 2 to 0
h i
We get, 14, 32, 72, 108, 12

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0
h i h i
32, 108, 14, 72, 12 → 32, X , 108, 72, 12 → [X , 32, 108, 72, 12] →
h i
14, 32, 108, 72, 12
In the next pass, i = 3 and index j runs from 2 to 0
h i
We get, 14, 32, 72, 108, 12
Finally, i = 4 and index j runs from 3 to 0

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


In the first pass, i = 1 and index j runs from 0 to 0
That is, up to index 0 is already sorted and insert object at index 1 in
to that list h i
[32, 108, 14, 72, 12] → 32, 108, 14, 72, 12
In the second pass, i = 2 and index j runs from 1 to 0
h i h i
32, 108, 14, 72, 12 → 32, X , 108, 72, 12 → [X , 32, 108, 72, 12] →
h i
14, 32, 108, 72, 12
In the next pass, i = 3 and index j runs from 2 to 0
h i
We get, 14, 32, 72, 108, 12
Finally, i = 4 and index j runs from 3 to 0
h i
We get, 12, 14, 32, 72, 108

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 12 / 32


Insertion Sort — Analysis

n−1 i−1
T (n) = 1
XX

i=1 j=0

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 13 / 32


Insertion Sort — Analysis

n−1 i−1
T (n) = 1
XX

i=1 j=0

n−1
= (i − 1) − 0 + 1
X

i=1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 13 / 32


Insertion Sort — Analysis

n−1 i−1
T (n) = 1
XX

i=1 j=0

n−1
= (i − 1) − 0 + 1
X

i=1
n−1
=
X
i
i=1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 13 / 32


Insertion Sort — Analysis

n−1 i−1
T (n) = 1
XX

i=1 j=0

n−1
= (i − 1) − 0 + 1
X

i=1
n−1
=
X
i
i=1

= (n − 1) + (n − 2) + · · · + 1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 13 / 32


Insertion Sort — Analysis

n−1 i−1
T (n) = 1
XX

i=1 j=0

n−1
= (i − 1) − 0 + 1
X

i=1
n−1
=
X
i
i=1

= (n − 1) + (n − 2) + · · · + 1

n(n − 1)
=
2

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 13 / 32


Insertion Sort — Analysis

n−1 i−1
T (n) = 1
XX

i=1 j=0

n−1
= (i − 1) − 0 + 1
X

i=1
n−1
=
X
i
i=1

= (n − 1) + (n − 2) + · · · + 1

n(n − 1)
=
2
Time Complexity is Θ(n2 )

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 13 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only
Minimum number of inversions: 0

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only
Minimum number of inversions: 0
In such a case, bubble sort and insertion sort perform Θ(n)
comparisons only

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only
Minimum number of inversions: 0
In such a case, bubble sort and insertion sort perform Θ(n)
comparisons only
Maximum number of inversions: n(n − 1)/2

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only
Minimum number of inversions: 0
In such a case, bubble sort and insertion sort perform Θ(n)
comparisons only
Maximum number of inversions: n(n − 1)/2
The number of comparisons and swaps are to that effect and
worst-case time complexity is Θ(n2 )

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only
Minimum number of inversions: 0
In such a case, bubble sort and insertion sort perform Θ(n)
comparisons only
Maximum number of inversions: n(n − 1)/2
The number of comparisons and swaps are to that effect and
worst-case time complexity is Θ(n2 )
What is the average case?

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only
Minimum number of inversions: 0
In such a case, bubble sort and insertion sort perform Θ(n)
comparisons only
Maximum number of inversions: n(n − 1)/2
The number of comparisons and swaps are to that effect and
worst-case time complexity is Θ(n2 )
What is the average case?
Expected number of inversions: n(n − 1)/4

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only
Minimum number of inversions: 0
In such a case, bubble sort and insertion sort perform Θ(n)
comparisons only
Maximum number of inversions: n(n − 1)/2
The number of comparisons and swaps are to that effect and
worst-case time complexity is Θ(n2 )
What is the average case?
Expected number of inversions: n(n − 1)/4
Hence, Ω(n2 ) comparisons are required

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32


Lower bound for brute-force sorting algorithms
Given a sequence of n objects, there are n C2 pairs and that many
comparisons are made
Since adjacent objects are compared (either directly or indirectly),
each swap corrects one inversion only
Minimum number of inversions: 0
In such a case, bubble sort and insertion sort perform Θ(n)
comparisons only
Maximum number of inversions: n(n − 1)/2
The number of comparisons and swaps are to that effect and
worst-case time complexity is Θ(n2 )
What is the average case?
Expected number of inversions: n(n − 1)/4
Hence, Ω(n2 ) comparisons are required
Is it possible to eliminate more than one inversion per swap?
C. Aravindan (SSN Institutions) Data Structures May 20, 2024 14 / 32
Merge Sort

Divide: Partition the sequence in to two equal halves, positions


begin · · · (mid − 1) and mid · · · end

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 15 / 32


Merge Sort

Divide: Partition the sequence in to two equal halves, positions


begin · · · (mid − 1) and mid · · · end
Conquer: Sort the sub-sequences separately

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 15 / 32


Merge Sort

Divide: Partition the sequence in to two equal halves, positions


begin · · · (mid − 1) and mid · · · end
Conquer: Sort the sub-sequences separately
Merge: Merge the sorted sub-sequences to single sorted sequence

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 15 / 32


Merge Sort

Divide: Partition the sequence in to two equal halves, positions


begin · · · (mid − 1) and mid · · · end
Conquer: Sort the sub-sequences separately
Merge: Merge the sorted sub-sequences to single sorted sequence

de f msort ( l s t ) :
length = len ( l s t )
i f ( length < 2):
return l s t [ : ]
else :
mid = l e n g t h // 2
r e t u r n merge ( m s o r t ( l s t [ : mid ] ) ,
m s o r t ( l s t [ mid : ] )
)

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 15 / 32


Merge Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 16 / 32


Merge Sort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]

[32, 108, 14, 72, 12]

[32, 108] [14, 72, 12]

[32] [108] [14] [72, 12]

[72] [12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 16 / 32


Merge Sort — Illustration

When the recursions return, at each internal node the two sorted
sequences are merged to create a unified sorted sequences

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 17 / 32


Merge Sort — Illustration

When the recursions return, at each internal node the two sorted
sequences are merged to create a unified sorted sequences

[12, 14, 32, 72, 108]

[32, 108] [12, 14, 72]

[32] [108] [14] [12, 72]

[72] [12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 17 / 32


Merging — Illustration

Merge [32, 108] with [12, 14, 72]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 18 / 32


Merging — Illustration

Merge [32, 108] with [12, 14, 72]


[32, 108] ; [12, 14, 72] ; []

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 18 / 32


Merging — Illustration

Merge [32, 108] with [12, 14, 72]


[32, 108] ; [12, 14, 72] ; []
[32, 108] ; [14, 72] ; [12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 18 / 32


Merging — Illustration

Merge [32, 108] with [12, 14, 72]


[32, 108] ; [12, 14, 72] ; []
[32, 108] ; [14, 72] ; [12]
[32, 108] ; [72] ; [12, 14]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 18 / 32


Merging — Illustration

Merge [32, 108] with [12, 14, 72]


[32, 108] ; [12, 14, 72] ; []
[32, 108] ; [14, 72] ; [12]
[32, 108] ; [72] ; [12, 14]
[108] ; [72] ; [12, 14, 32]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 18 / 32


Merging — Illustration

Merge [32, 108] with [12, 14, 72]


[32, 108] ; [12, 14, 72] ; []
[32, 108] ; [14, 72] ; [12]
[32, 108] ; [72] ; [12, 14]
[108] ; [72] ; [12, 14, 32]
[108] ; [] ; [12, 14, 32, 72]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 18 / 32


Merging — Illustration

Merge [32, 108] with [12, 14, 72]


[32, 108] ; [12, 14, 72] ; []
[32, 108] ; [14, 72] ; [12]
[32, 108] ; [72] ; [12, 14]
[108] ; [72] ; [12, 14, 32]
[108] ; [] ; [12, 14, 32, 72]
[] ; [] ; [12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 18 / 32


Merge Sort
d e f merge ( l s t 1 , l s t 2 ) :
i = j = 0
res = [ ]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 19 / 32


Merge Sort
d e f merge ( l s t 1 , l s t 2 ) :
i = j = 0
res = [ ]
w h i l e ( ( i < l e n ( l s t 1 ) ) and ( j < l e n ( l s t 2 ) ) ) :
if ( lst1 [ i ] < lst2 [ j ]):
r e s . append ( l s t 1 [ i ] )
i += 1
else :
r e s . append ( l s t 2 [ j ] )
j += 1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 19 / 32


Merge Sort
d e f merge ( l s t 1 , l s t 2 ) :
i = j = 0
res = [ ]
w h i l e ( ( i < l e n ( l s t 1 ) ) and ( j < l e n ( l s t 2 ) ) ) :
if ( lst1 [ i ] < lst2 [ j ]):
r e s . append ( l s t 1 [ i ] )
i += 1
else :
r e s . append ( l s t 2 [ j ] )
j += 1
i f ( i < len ( lst1 )):
res . extend ( l s t 1 [ i : ] )
e l i f ( j < len ( lst2 )):
res . extend ( l s t 2 [ j : ] )
return res
C. Aravindan (SSN Institutions) Data Structures May 20, 2024 19 / 32
Merge Sort — Analysis

(
d n≤1
T (n) =
2T (n/2) + n n>1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 20 / 32


Merge Sort — Analysis

(
d n≤1
T (n) =
2T (n/2) + n n>1
Time complexity is O(n log n)

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 20 / 32


Mergesort — Discussion

Improved worst-case time complexity: O(n log n)


But, requires extra space for merging
We have discussed a variant that actually returns a new sorted
sequence
In-place variation is possible, but still requires extra space
Multi-way merging is also possible
Suitable for external sorting
Suitable for sorting dynamic structures such as linked lists

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 21 / 32


Quicksort

Invented by C. A. R. Hoare in 1962

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 22 / 32


Quicksort

Invented by C. A. R. Hoare in 1962


Like in the case of merge sort, the input sequence L is partitioned,
but not in an uninformed way

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 22 / 32


Quicksort

Invented by C. A. R. Hoare in 1962


Like in the case of merge sort, the input sequence L is partitioned,
but not in an uninformed way
One of the objects is selected as a pivot p. Partitioning is done wrt
the pivot: All the objects x < p belong to one partition L1 and the
objects x > p constitute the other partition L2

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 22 / 32


Quicksort

Invented by C. A. R. Hoare in 1962


Like in the case of merge sort, the input sequence L is partitioned,
but not in an uninformed way
One of the objects is selected as a pivot p. Partitioning is done wrt
the pivot: All the objects x < p belong to one partition L1 and the
objects x > p constitute the other partition L2
Now L1 and L2 are sorted separately

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 22 / 32


Quicksort

Invented by C. A. R. Hoare in 1962


Like in the case of merge sort, the input sequence L is partitioned,
but not in an uninformed way
One of the objects is selected as a pivot p. Partitioning is done wrt
the pivot: All the objects x < p belong to one partition L1 and the
objects x > p constitute the other partition L2
Now L1 and L2 are sorted separately
Finally sorted L is obtained as: sorted L1 , followed by p, followed by
sorted L2

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 22 / 32


Quicksort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 23 / 32


Quicksort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


Suppose we select 32 as the pivot

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 23 / 32


Quicksort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


Suppose we select 32 as the pivot
Then partition may result in [14, 12], [32], [108, 72]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 23 / 32


Quicksort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


Suppose we select 32 as the pivot
Then partition may result in [14, 12], [32], [108, 72]
Now, sort the two partitions, resulting in [12, 14], [32], [72, 108]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 23 / 32


Quicksort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


Suppose we select 32 as the pivot
Then partition may result in [14, 12], [32], [108, 72]
Now, sort the two partitions, resulting in [12, 14], [32], [72, 108]
Now, simply append them to get the sorted sequence
[12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 23 / 32


Quicksort — Illustration

Consider the same sequence [32, 108, 14, 72, 12]


Suppose we select 32 as the pivot
Then partition may result in [14, 12], [32], [108, 72]
Now, sort the two partitions, resulting in [12, 14], [32], [72, 108]
Now, simply append them to get the sorted sequence
[12, 14, 32, 72, 108]
It is possible to perform these steps in-place, in which case the final
append is not necessary

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 23 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object
Instead of median of the entire sequence, select the median of, say 3
objects

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object
Instead of median of the entire sequence, select the median of, say 3
objects
What may be a better partition strategy?

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object
Instead of median of the entire sequence, select the median of, say 3
objects
What may be a better partition strategy?
Lomuto partition algorithm

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object
Instead of median of the entire sequence, select the median of, say 3
objects
What may be a better partition strategy?
Lomuto partition algorithm
Hoare’s partition algorithm

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object
Instead of median of the entire sequence, select the median of, say 3
objects
What may be a better partition strategy?
Lomuto partition algorithm
Hoare’s partition algorithm
What may be a better base case?

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object
Instead of median of the entire sequence, select the median of, say 3
objects
What may be a better partition strategy?
Lomuto partition algorithm
Hoare’s partition algorithm
What may be a better base case?
When n < 2, the sequence is already sorted

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object
Instead of median of the entire sequence, select the median of, say 3
objects
What may be a better partition strategy?
Lomuto partition algorithm
Hoare’s partition algorithm
What may be a better base case?
When n < 2, the sequence is already sorted
Note that for small sequences, the overhead of divide and conquer may
be more

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32


Quicksort — Implementation issues
How is this different from Mergesort and what may go wrong?
How to select a pivot?
Obviously median is the best choice, but how to find the median!?
Pick the object at a pre-determined location — such as first object or
the last object
Pick a random object
Instead of median of the entire sequence, select the median of, say 3
objects
What may be a better partition strategy?
Lomuto partition algorithm
Hoare’s partition algorithm
What may be a better base case?
When n < 2, the sequence is already sorted
Note that for small sequences, the overhead of divide and conquer may
be more
Hence we can think of hybrid sorting — perform insertion sorting when
the size of the partition is below a threshold
C. Aravindan (SSN Institutions) Data Structures May 20, 2024 24 / 32
Quicksort — Implementation
def quick_sort ( l s t ) :
QUICK_BASE_CASE = 3

d e f q u i c k _ s o r t _ r e c ( l s t , b e g i n , end ) :
i f ( ( end − b e g i n ) < QUICK_BASE_CASE ) :
i n s e r t i o n _ s o r t _ r a n g e ( l s t , b e g i n , end )
return

p = p a r t i t i o n ( l s t , b e g i n , end )
q u i c k _ s o r t _ r e c ( l s t , b e g i n , p−1)
q u i c k _ s o r t _ r e c ( l s t , p+1, end )
return

q u i c k _ s o r t _ r e c ( l s t , 0 , l e n ( l s t ) − 1)

return
C. Aravindan (SSN Institutions) Data Structures May 20, 2024 25 / 32
Quicksort — Finding a pivot

d e f f i n d _ p i v o t _ m 3 ( l s t , b e g i n , end ) :
mid = ( b e g i n + end ) // 2
i f ( l s t [ b e g i n ] > l s t [ mid ] ) :
l s t [ b e g i n ] , l s t [ mid ] = l s t [ mid ] , l s t [ begin ]
i f ( l s t [ b e g i n ] > l s t [ end ] ) :
l s t [ b e g i n ] , l s t [ end ] = l s t [ end ] , l s t [ begin ]
i f ( l s t [ mid ] > l s t [ end ] ) :
l s t [ mid ] , l s t [ end ] = l s t [ end ] , l s t [ mid ]
l s t [ mid ] , l s t [ end −1] = l s t [ end −1] , l s t [ mid ]
r e t u r n l s t [ end −1]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 26 / 32


Quicksort — Finding a pivot

d e f f i n d _ p i v o t _ m 3 ( l s t , b e g i n , end ) :
mid = ( b e g i n + end ) // 2
i f ( l s t [ b e g i n ] > l s t [ mid ] ) :
l s t [ b e g i n ] , l s t [ mid ] = l s t [ mid ] , l s t [ begin ]
i f ( l s t [ b e g i n ] > l s t [ end ] ) :
l s t [ b e g i n ] , l s t [ end ] = l s t [ end ] , l s t [ begin ]
i f ( l s t [ mid ] > l s t [ end ] ) :
l s t [ mid ] , l s t [ end ] = l s t [ end ] , l s t [ mid ]
l s t [ mid ] , l s t [ end −1] = l s t [ end −1] , l s t [ mid ]
r e t u r n l s t [ end −1]

Note that this function has side effects!!!

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 26 / 32


Quicksort — Finding a pivot

d e f f i n d _ p i v o t _ m 3 ( l s t , b e g i n , end ) :
mid = ( b e g i n + end ) // 2
i f ( l s t [ b e g i n ] > l s t [ mid ] ) :
l s t [ b e g i n ] , l s t [ mid ] = l s t [ mid ] , l s t [ begin ]
i f ( l s t [ b e g i n ] > l s t [ end ] ) :
l s t [ b e g i n ] , l s t [ end ] = l s t [ end ] , l s t [ begin ]
i f ( l s t [ mid ] > l s t [ end ] ) :
l s t [ mid ] , l s t [ end ] = l s t [ end ] , l s t [ mid ]
l s t [ mid ] , l s t [ end −1] = l s t [ end −1] , l s t [ mid ]
r e t u r n l s t [ end −1]

Note that this function has side effects!!!


The pivot is temporarily moved to (end − 1) position

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 26 / 32


Quicksort — Finding a pivot

d e f f i n d _ p i v o t _ m 3 ( l s t , b e g i n , end ) :
mid = ( b e g i n + end ) // 2
i f ( l s t [ b e g i n ] > l s t [ mid ] ) :
l s t [ b e g i n ] , l s t [ mid ] = l s t [ mid ] , l s t [ begin ]
i f ( l s t [ b e g i n ] > l s t [ end ] ) :
l s t [ b e g i n ] , l s t [ end ] = l s t [ end ] , l s t [ begin ]
i f ( l s t [ mid ] > l s t [ end ] ) :
l s t [ mid ] , l s t [ end ] = l s t [ end ] , l s t [ mid ]
l s t [ mid ] , l s t [ end −1] = l s t [ end −1] , l s t [ mid ]
r e t u r n l s t [ end −1]

Note that this function has side effects!!!


The pivot is temporarily moved to (end − 1) position
We need to partition the sequence in the range (begin + 1) to
(end − 2)

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 26 / 32


Quicksort — Partitioning Algorithm
d e f p a r t i t i o n ( l s t , b e g i n , end ) :
p i v o t = f i n d _ p i v o t _ m 3 ( l s t , b e g i n , end )
i = begin ; j = end − 1
w h i l e ( True ) :
i += 1
while ( l s t [ i ] < pivot ):
i += 1
j −= 1
while ( l s t [ j ] > pivot ):
j −= 1
if ( i < j ):
lst [ i ] , lst [ j ] = lst [ j ] , lst [ i ]
else :
break
l s t [ i ] , l s t [ end −1] = l s t [ end −1] , l s t [ i ]
return i
C. Aravindan (SSN Institutions) Data Structures May 20, 2024 27 / 32
Quicksort — Illustration

Trace the execution of quick sort on the same sequence


[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 28 / 32


Quicksort — Analysis

(
d n≤1
T (n) =
T (i) + T (n − i − 1) + cn n>1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 29 / 32


Quicksort — Analysis

(
d n≤1
T (n) =
T (i) + T (n − i − 1) + cn n>1
Worst case occurs when i = 0 or i = n − 1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 29 / 32


Quicksort — Analysis

(
d n≤1
T (n) =
T (i) + T (n − i − 1) + cn n>1
Worst case occurs when i = 0 or i = n − 1
(
d n≤1
T (n) =
T (n − 1) + cn n>1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 29 / 32


Quicksort — Analysis

(
d n≤1
T (n) =
T (i) + T (n − i − 1) + cn n>1
Worst case occurs when i = 0 or i = n − 1
(
d n≤1
T (n) =
T (n − 1) + cn n>1

Worst-case time complexity is O(n2 )

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 29 / 32


Quicksort — Analysis

Best case occurs when i = n/2

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 30 / 32


Quicksort — Analysis

Best case occurs when i = n/2


(
d n≤1
T (n) =
2T (n/2) + cn n>1

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 30 / 32


Quicksort — Analysis

Best case occurs when i = n/2


(
d n≤1
T (n) =
2T (n/2) + cn n>1

Best-case time complexity is O(n log n)

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 30 / 32


Quicksort — Analysis

To find the expected time on the average, consider i = 0 to i = n − 1,


where each has equal probability 1/n

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 31 / 32


Quicksort — Analysis

To find the expected time on the average, consider i = 0 to i = n − 1,


where each has equal probability 1/n

1 n−1
T (n) = T (i) + T (n − i − 1) + cn
X
n i=0

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 31 / 32


Quicksort — Analysis

To find the expected time on the average, consider i = 0 to i = n − 1,


where each has equal probability 1/n

1 n−1
T (n) = T (i) + T (n − i − 1) + cn
X
n i=0

2 n−1
" #
T (n) = T (i) + cn
X
n i=0

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 31 / 32


Quicksort — Analysis

To find the expected time on the average, consider i = 0 to i = n − 1,


where each has equal probability 1/n

1 n−1
T (n) = T (i) + T (n − i − 1) + cn
X
n i=0

2 n−1
" #
T (n) = T (i) + cn
X
n i=0
Average case complexity is O(n log n)

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 31 / 32


Summary

Generate-and-Test
Check all the permutations of the input sequence
Brute force (or) Exhaustive search
Check all the pairs and swap all the inversions
Divide-Conquer-Merge
Decomposing 1 and (n − 1) does not help
Decomposing n/2 and n/2 changes the complexity class
Do more work on decomposition where merging becomes trivial
Hybrid Approaches

C. Aravindan (SSN Institutions) Data Structures May 20, 2024 32 / 32

You might also like