Sorting Algorithms 2
Sorting Algorithms 2
SORTING
Algorithms
By
Kapil Yadav
15 16 6 8 5
A
7
[
15 16 6 8 5 6 8 5 15 16
§ &ˢ
"" " & 5 £ " " °
£96
" ""
15 6 16 8 5 6 5 8 15 16
IS 6 8 16 5 6 5 8 15 16
IS 6 8 5 16 6 5 8 15 16
IS 8 5 16 6 5 8 15 16
§
6
6 IS 8 5 16 5 6 8 15 16
Pass
, g ,, g ,, 5 6 8 15 16
6 8 5 15 16 5 6 8 15 16
6 8 5 15 16 5 6 8 15 16
https://www.linkedin.com/in/kapilyadav22/
https://www.linkedin.com/in/kapilyadav22/
Further Optimization:
The Time Complexity of Bubble Sort can be optimized for Best Case.i.e When all the elements of array
are in sorted order, then there will be no swaps, So we can use it to identify whether our array is sorted
or not.
https://www.linkedin.com/in/kapilyadav22/
https://www.linkedin.com/in/kapilyadav22/
Insertion Sort:
5 4 10 I 6 2
{rt¥Unsorted_
temp
/
1.) 5 10 I 6 2
j
jj
j - -
☐ s to I 6 2
jtl
are [j+☐= temp ;
are [0 ] = 4;
2) 4
g. / to
I 6 2
are [j ] - area]
temp
←
µ
3) 4 5 10 I 6 2
;
☒µ ?
4 ' 62
js
=o
,
if it runs until
j -0 ,
means
ith
correct
position of current
4
5110 0ᵗʰ
6 2 element is at index .
↳ i
j ☐ → represents that current
jᵗʰ index
j
I
4
11
5 ?
,
6 2
before decreament
be same as
and
previous .
its value will
/
I 4 5 10 6 2
jtl
4) EEP
/
I 4 5 10
ji
4
1102
I 5
U
j
I 4 5 ☒ 10 2
jtl
5.)
/
4 56
1g
1 ? ?
1 4 56
j ☐#
4
6110
I 5 ☐
U
j
4 ☐
/
I 5 6 10
↳
j
6110
I ☐ 4 5
or
I 2 4 5 6 10
https://www.linkedin.com/in/kapilyadav22/
Practice Questions:
Ans.) 8 swaps.
7 4 10 8 3 I
0 I 2 3 4 5
}
7-
4 to 8 3
Pass 1 : -
. 11
/ ↑
° ' 2 3 4 5
¥
4 10
Min
Pass 3 :
-
I 3 10 8 41 #
0 I 2 3 4 5
→ -
7 8
Pass 4 : -
I 3 4 81 10
* -
min
0 I 2 3 4 5
1-
& 10
Pass 5 : -
I 3 4 7 10 $
Is
Selection Sort Requires N-1 Passes.
Total Comparisons = ( (N-1) * N ) /2 = O(n²) { Every Case},
Total Swaps = N-1 { Every Case}
Time Complexity = O(n²) {because comparisons are always n².}
Selection Sort speciality is No of swaps are N-1 in every case.
Quick Sort:
It is a Divide and Conquer Algorithm It is also a type of comparison based Sorting Algorithm.
The Partition Algorithm is the heart of Quick Sort Algorithm.
We choose an element as pivot and perform partition algorithm to the pivot algorithm.
The idea to put pivot element at it’s correct position, which means the left side elements of pivot
elements will be less than or equal to pivot element and the right side elements will be greater than
pivot element.
NOTE: Left and Right side elements need not to be in Sorted Order, to Sort them, we again call Quick
Sort for both the partitions.
It is considered to be inplace algorithm as it only used recursive space.
It is not Stable.
pivot
3 9 6 I 5 4 2
pivot pivot
2 I 9 5 4 6
I { } { 6 5 43 { }
pi¥
{ 4,53 E }
/
{ } {5 }
1.) When first element is selected as pivot:
3 9 6 I 5 4 2
{ I } { 6 3 5 49 }
{6 3 54 } { }
{ 3 } { 5,6 }
{ 5} { }
Time Complexity of Quick Sort:
Best Case = T(n/2) + T(n/2) + n, when after partition, there will be equal elements
on both sides.
Worst Case + T(n-1) + n, when after partition, the elements are skewed on one
side only.
Merge Sort:
It is another comparison based sorting algorithm.
It is based on the Divide and conquer technique.
It is Outplace algorithm as we need an additional array to store the temp
output of the sorted array.
It is Stable Sort.
29 1 5 43
29 1 54 3
29 1 54 3
"
2 g I 5 4 3
29 ¥ 45 3
1 29 345
1234 59
→ ◦
G)
→ 1- Cnn)
→ 1- ( n / 2)
◦ (n )
Recurence Relation :
1.) for Outplace:
)={ 047+21-6-2)+n
047
}
Tin it -
=
'
if n > I
TC =
Ocnlogn )
27 for in place :
÷?
"
"" "
(1) +2T¥
)+n%
◦ n > 1
TC = 042) .
Space complexity : -
input + extra
n
↓
N
logn n
=
Ofn ) .
→
Merge sort is advisable for larger size arrays for .
is
smaller
size ,
Insertion sort
preferable .
10 20 30 40 11 21 31 41
←
× → for sorted unequal ←e#sy→
=3
→
for sorted
equal -2%
→
elements
comparisons Moves
④ 4) =
4+4-1 ( 4,4 ) =
4+4
(3) %) n I
= -
C. g) =
✗ +
y -1
(E) ± ) -
n
(x ,y)
✗ +
g
TC -
Max ( comparisons ,
moves ]
=
n ( for equal sorted elements )
( for elements)
=
Yo 50 60 70 do 10 20 30
← →
✗ → ←
y
Comparison Monies
y)
⇐y) mink , ⇐ g) y
= = ✗+
⇐ E) : ⇐ E) n
= =
Heap Sort:
↳ 12J
NOTE: Array is better for complete and almost complete Binary Tree.
Linked List is better idea for any gap in Nodes.
⑤\⑧_②_⑧
for example : -
Using Array
: -
D. i, A C B
O 1 2 34 5 6 78 9 10 11 121314 IS
\ D 20 ]
100 A 300
200 \ C 400
I
300 \ B \
400
?⃝
MaxHeap: Root is maximum or equal to children at every level.
for ith max, Total comparison = i(i-1)/2 = O(i²).
In Bubble Sort, every pass is costly O(n), but in heap sort, every comparison cost is O(1).
6° 35
70
70 So 40 453/539 16 to 9 &
I 2 3 4 5 6 7 8 9 10
s/o 60
40
Liz]
5
parent of 10 = -
.
[
60 50
7° $ 40 456/039 16 10 9 35
US
39 16 5
I 2 3 4 6 7 8 9 10
it 5=[5-2]--2
parent of
to 9
-
60/35 70 60 40 45 SO 39 16 10 9 35
I 2 3 4 5 6 7 8 9 10
parent of 2 is
(2-2)--1
=
.
and element into
NOTE
Inserting Deleting maxheqp
:-
an ,
will
which
already contains n elements take
order
of login .
50-10-45
%✓ys3533162S3412t@
I 2 3 4 5 6 7 8 9
33
-4s
35
12
3-3
"
zg 3. y
45 10 3533 16 2534
12$50
, g I 2 3 4 5 6 7 8 9
10
1-2
§ 45
I
3335
2 3
10
4
16 25
5 6
34
7
12
8
So
45 3335 12 16 25 34 10 So
I 2 3 4 5 6 7 8 9
NOTE: To delete an element , we can simply put the deleted element at last place, and reduce the loop to one place (from 1-8).
As soon as we deleted the element, we will get the elements in ascending order ( because we are placing max element
at last place.)
Heap Sort: I 2 3 4 5
A IS 20 7- 9
/ 30
IS Insertion
20
2° 20
→ Insertion Insertion
→
→
IS 15 7 15 7-
30 20 15
%etim
→
Dg
20 15 7- 9
, 7
9 15 9
9
7 Deletion Deletion +
Deletion
→
I
→
7
g
For Insertion in a max heap or min heap, O(logn) times requires for an single element.
for n elements = O(nlogn).
for Deletion in a max or min heap, O(logn) times require for an single element.
for deletion of n elements = O(nlogn)
For heap sort, creation and deletion requires for n elements = O(2nlogn).
NOTE: This method takes O(nlogn) to create a max heap, but there is another method, which is heapify
method and it takes O(n) to create a Build Heap.
⑥
30
is
$ 17 20/30/15/20
3012%
to
} Deletion
TC =
0cm )
↓
+
Ocnlogn )
Build Max heap