Sorting Algorithms
Sorting Algorithms
Sorting Algorithms
Sorting Algorithms
Chandrabose Aravindan
<AravindanC@ssn.edu.in>
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 : ] ) )
de f i n s e r t ( obj , seq ) :
i f ( l e n ( s e q ) == 0 ) :
return [ obj ]
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
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
(
d n≤1
T (n) =
T (n − 1) + c n>1
(
d n≤1
T (n) =
T (n − 1) + c n>1
Time complexity is Θ(n)
(
d n≤1
T (n) =
T (n − 1) + cn n>1
(
d n≤1
T (n) =
T (n − 1) + cn n>1
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
n−1 i−1
T (n) = 1
XX
i=1 j=0
n−1 i−1
T (n) = 1
XX
i=1 j=0
n−1
= (i − 1) − 0 + 1
X
i=1
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 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−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
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 )
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 : ] )
)
[72] [12]
When the recursions return, at each internal node the two sorted
sequences are merged to create a unified sorted sequences
When the recursions return, at each internal node the two sorted
sequences are merged to create a unified sorted sequences
[72] [12]
(
d n≤1
T (n) =
2T (n/2) + n n>1
(
d n≤1
T (n) =
2T (n/2) + n n>1
Time complexity is O(n log n)
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]
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]
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]
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]
(
d n≤1
T (n) =
T (i) + T (n − i − 1) + cn n>1
(
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 (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
(
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
1 n−1
T (n) = T (i) + T (n − i − 1) + cn
X
n i=0
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
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)
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