Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Basic Sorting Algorithms and Their Analysis

Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

CS1006T Data Structures

Basic sorting algorithms and their analysis

Chandrabose Aravindan
<AravindanC@ssn.edu.in>

Professor of Information Technology


SSN College of Engineering

March 12, 2024

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 1 / 18


Introduction

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


by a partial order ≤

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 2 / 18


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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 2 / 18


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 March 12, 2024 2 / 18


A trivial sorting algorithm

def isSorted ( l s t ) :
n = len ( l s t )
f o r i i n r a n g e ( n −1):
i f ( l s t [ i ] > l s t [ i +1] ) :
return False
r e t u r n True

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 3 / 18


A trivial sorting algorithm

def isSorted ( l s t ) :
n = len ( l s t )
f o r i i n r a n g e ( n −1):
i f ( l s t [ i ] > l s t [ i +1] ) :
return False
r e t u r n True

from i t e r t o o l s i m p o r t p e r m u t a t i o n s

def permSort ( l s t ) :
for l in permutations ( l s t ) :
res = l i s t ( l )
i f isSorted ( res ):
return res

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 3 / 18


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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 4 / 18


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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 4 / 18


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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 4 / 18


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 March 12, 2024 4 / 18


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 March 12, 2024 5 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]
After the first pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]
After the first pass, we will have [12, 108, 32, 72, 14]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]
After the first pass, we will have [12, 108, 32, 72, 14]
After the second pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]
After the first pass, we will have [12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]
After the first pass, we will have [12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]
After the first pass, we will have [12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 108, 72]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]
After the first pass, we will have [12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 108, 72]
After the fourth pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.
Consider the sequence [32, 108, 14, 72, 12]
After the first pass, we will have [12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 108, 72]
After the fourth pass, we will have [12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 6 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 7 / 18


Selection Sort

Algorithm idea: In the first pass, select the smallest object and bring
it to the beginning of the list. In the next pass, next smallest object
should be selected as the second object, and so on.

def selection_sort ( l s t ) :
n = len ( l s t )
f o r i i n r a n g e ( n −1):
f o r j i n r a n g e ( i +1, n ) :
if ( lst [ i ] > lst [ j ]):
lst [ i ] , lst [ j ] = lst [ j ] , lst [ i ]
return

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 7 / 18


Selection Sort

We can make an assertion that ’min’ is found at the end of each


outer iteration.

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 8 / 18


Selection Sort

We can make an assertion that ’min’ is found at the end of each


outer iteration.

def selection_sort ( l s t ) :
n = len ( l s t )
f o r i i n r a n g e ( n −1):
f o r j i n r a n g e ( i +1, n ) :
if ( lst [ i ] > lst [ j ]):
lst [ i ] , lst [ j ] = lst [ j ] , lst [ i ]
a s s e r t ( l s t [ i ] == min ( l s t [ i : ] ) )
return

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 8 / 18


Selection Sort

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]
[14, 108, 32, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]
[14, 108, 32, 72, 12]
[12, 108, 32, 72, 14]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]
[14, 108, 32, 72, 12]
[12, 108, 32, 72, 14]
After the second pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]
[14, 108, 32, 72, 12]
[12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]
[14, 108, 32, 72, 12]
[12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]
[14, 108, 32, 72, 12]
[12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 108, 72]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]
[14, 108, 32, 72, 12]
[12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 108, 72]
After the fourth pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[14, 108, 32, 72, 12]
[14, 108, 32, 72, 12]
[12, 108, 32, 72, 14]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 108, 72]
After the fourth pass, we will have [12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 9 / 18


Selection Sort

It is trivial to reduce the number of swaps by choosing the minimum


first and then swap

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 10 / 18


Selection Sort

It is trivial to reduce the number of swaps by choosing the minimum


first and then swap

def selection_sort ( l s t ) :
n = len ( l s t )
f o r i i n r a n g e ( n −1):
m in _i nd ex = i
f o r j i n r a n g e ( i +1, n ) :
i f ( l s t [ min_index ] > l s t [ j ] ) :
min_index = j
i f ( i != m i n _ i n d e x ) :
l s t [ i ] , l s t [ min_index ] = l s t [ min_index ] , l
return

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 10 / 18


Selection Sort

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[12, 108, 14, 72, 32]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[12, 108, 14, 72, 32]
After the second pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[12, 108, 14, 72, 32]
After the second pass, we will have [12, 14, 108, 72, 32]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[12, 108, 14, 72, 32]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[12, 108, 14, 72, 32]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[12, 108, 14, 72, 32]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 72, 108]
After the fourth pass, we will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort

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


In the first pass, index i = 0 and index j runs from 1 to 4
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[12, 108, 14, 72, 32]
After the second pass, we will have [12, 14, 108, 72, 32]
After the third pass, we will have [12, 14, 32, 72, 108]
After the fourth pass, we will have [12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 11 / 18


Selection Sort — Analysis

n−2
X n−1
X
T (n) = 1
i=0 j=i+1

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 12 / 18


Selection Sort — Analysis

n−2
X n−1
X
T (n) = 1
i=0 j=i+1

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 12 / 18


Selection Sort — Analysis

n−2
X n−1
X
T (n) = 1
i=0 j=i+1

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 12 / 18


Selection Sort — Analysis

n−2
X n−1
X
T (n) = 1
i=0 j=i+1

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

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 12 / 18


Selection Sort — Analysis

n−2
X n−1
X
T (n) = 1
i=0 j=i+1

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

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

n(n − 1)
=
2

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 12 / 18


Selection Sort — Analysis

n−2
X n−1
X
T (n) = 1
i=0 j=i+1

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

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

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 12 / 18


Bubble Sort

An alternate idea: In the first pass, keep comparing the adjacent


objects, and swap if they are out of order

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 13 / 18


Bubble Sort

An alternate idea: In the first pass, keep comparing the adjacent


objects, and swap if they are out of order
At the end of the first pass, the largest object bubbles to the last
position. In the second pass, second largest object bubbles, and so on.

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 13 / 18


Bubble Sort

An alternate idea: In the first pass, keep comparing the adjacent


objects, and swap if they are out of order
At the end of the first pass, the largest object bubbles to the last
position. In the second pass, second largest object bubbles, and so on.

def bubble_sort_ ( l s t ) :
n = len ( l s t )
f o r i i n r a n g e ( n −1):
f o r j i n r a n g e ( n−1− i ) :
i f ( l s t [ j ] > l s t [ j +1]):
l s t [ j ] , l s t [ j +1] = l s t [ j +1] , l s t [ j ]
return

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 13 / 18


Bubble Sort

What can be asserted at the end of each outer iteration?

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 14 / 18


Bubble Sort

What can be asserted at the end of each outer iteration?

def bubble_sort_ ( l s t ) :
n = len ( l s t )
f o r i i n r a n g e ( n −1):
f o r j i n r a n g e ( n−1− i ) :
i f ( l s t [ j ] > l s t [ j +1]):
l s t [ j ] , l s t [ j +1] = l s t [ j +1] , l s t [ j ]
a s s e r t ( l s t [ n−i −1] == max ( l s t [ : n−i ] ) )
return

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 14 / 18


Bubble Sort

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]
[32, 14, 72, 12, 108]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]
[32, 14, 72, 12, 108]
After the second pass, count i = 1, index j runs from

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]
[32, 14, 72, 12, 108]
After the second pass, count i = 1, index j runs from 0 to 2, and we
will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]
[32, 14, 72, 12, 108]
After the second pass, count i = 1, index j runs from 0 to 2, and we
will have [14, 32, 12, 72, 108]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]
[32, 14, 72, 12, 108]
After the second pass, count i = 1, index j runs from 0 to 2, and we
will have [14, 32, 12, 72, 108]
After the third pass, count i = 2, index j runs from 0 to 1, and we
will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]
[32, 14, 72, 12, 108]
After the second pass, count i = 1, index j runs from 0 to 2, and we
will have [14, 32, 12, 72, 108]
After the third pass, count i = 2, index j runs from 0 to 1, and we
will have [14, 12, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]
[32, 14, 72, 12, 108]
After the second pass, count i = 1, index j runs from 0 to 2, and we
will have [14, 32, 12, 72, 108]
After the third pass, count i = 2, index j runs from 0 to 1, and we
will have [14, 12, 32, 72, 108]
After the fourth pass, count i = 3, index j runs from 0 to 0, and we
will have

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

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


In the first pass, count i = 0 and index j runs from 0 to 3
[32, 108, 14, 72, 12]
[32, 108, 14, 72, 12]
[32, 14, 108, 72, 12]
[32, 14, 72, 108, 12]
[32, 14, 72, 12, 108]
After the second pass, count i = 1, index j runs from 0 to 2, and we
will have [14, 32, 12, 72, 108]
After the third pass, count i = 2, index j runs from 0 to 1, and we
will have [14, 12, 32, 72, 108]
After the fourth pass, count i = 3, index j runs from 0 to 0, and we
will have [12, 14, 32, 72, 108]

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 15 / 18


Bubble Sort

Since adjacent objects are compared, it is possible to detect if the


sequence is already sorted!
So, it may be possible to break the loop and save a lot of comparisons!

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 16 / 18


Bubble Sort

Since adjacent objects are compared, it is possible to detect if the


sequence is already sorted!
So, it may be possible to break the loop and save a lot of comparisons!

def bubble_sort ( l s t ) :
n = len ( l s t )
f o r i i n r a n g e ( n −1):
swapped = F a l s e
f o r j i n r a n g e ( n−1− i ) :
i f ( l s t [ j ] > l s t [ j +1]):
l s t [ j ] , l s t [ j +1] = l s t [ j +1] , l s t [ j ]
swapped = True
i f ( n o t swapped ) : b r e a k
return

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 16 / 18


Bubble Sort — Analysis

n−2
X n−2−i
X
T (n) = 1
i=0 j=0

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 17 / 18


Bubble Sort — Analysis

n−2
X n−2−i
X
T (n) = 1
i=0 j=0

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 17 / 18


Bubble Sort — Analysis

n−2
X n−2−i
X
T (n) = 1
i=0 j=0

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 17 / 18


Bubble Sort — Analysis

n−2
X n−2−i
X
T (n) = 1
i=0 j=0

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

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 17 / 18


Bubble Sort — Analysis

n−2
X n−2−i
X
T (n) = 1
i=0 j=0

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

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

n(n − 1)
=
2

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 17 / 18


Bubble Sort — Analysis

n−2
X n−2−i
X
T (n) = 1
i=0 j=0

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

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

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

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 17 / 18


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
Selection Sort
Bubble Sort

C. Aravindan (SSN Institutions) Data Structures March 12, 2024 18 / 18

You might also like