Basic Sorting Algorithms and Their Analysis
Basic Sorting Algorithms and Their Analysis
Basic Sorting Algorithms and Their Analysis
Chandrabose Aravindan
<AravindanC@ssn.edu.in>
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
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
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.
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]
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
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]
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
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]
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
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]
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
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]
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.
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
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
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
n−2
X n−1
X
T (n) = 1
i=0 j=i+1
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−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−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−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
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 )
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
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
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
n−2
X n−2−i
X
T (n) = 1
i=0 j=0
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−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−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−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
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 )
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