SlideShare a Scribd company logo
Merge Sort
 Merge Sort
 Merge sort with n input size of array
 Divide:
 Divide the n-element sequence to be
sorted into two subsequences of n/2
elements each
 Conquer
 Sort the subsequences recursively using merge sort
 When the size of the sequences is 1 there is nothing more
to do
 Combine
 Merge the two sorted sub sequences
Dr.Tamilarasan.S (18CS42) 4/2/2025 49
Merge Sort
 Merge Sort
 The array is A[O .. n - 1]
 Dividing into two halves
 A[0 .. n/2 -1] and A[n/2 .. n -1]
 Sorting each of them recursively
 Merging the two smaller sorted arrays into a single sorted
one.
Dr.Tamilarasan.S (18CS42) 4/2/2025 50
Merge Sort
 Merge Sort
ALGORITHM Merge-sort(A[0 .. n - 1])
//Sorts array A[O .. n - 1] by recursive merge-sort
//Input: An array A[O .. n - 1] of orderable elements
//Output: Array A[O .. n - 1] sorted in non-decreasing
order If n > 1
Copy A[0 .. (n/2) -1] to B[0 .. n/2 -1]
copy A[(n/2) .. n -1] to C[0 .. (n/2) -1]
Merge-sort(B[0 .. (n/2) - 1])
Merge-sort(C[0 .. (n/2) -1])
Merge(B, C, A)
Dr.Tamilarasan.S (18CS42) 4/2/2025 51
Merge Sort
 Merge Sort
ALGORITHM Merge(B[0 .. p- 1], C[0 .. q -1], A[0 .. p + q -1])
 //Merges two sorted arrays into one sorted array
 //Input: Arrays B[O .. p -1] and C[O .. q -1] both sorted
 //Output: Sorted array A[O .. p + q -1] of the elements of Band C
i ← 0; j ← 0; k ← 0
While i < p and j < q do
if B[i] ≤ S C[j]
A[k] ← B[i]; i ← i + 1
else A[k] ← C[j]; j ← j +
1 k← k+1
If i = p
copy C[j .. q -1] to A[k .. p
+ q -1]
else copy B[i .. p -1] to
A[k .. p + q -1]
Dr.Tamilarasan.S (18CS42) 4/2/2025 52
Merge Sort
 Merge Sort
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9
8 3
7 1 5 4
2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Dr.Tamilarasan.S (18CS42) 4/2/2025 53
Merge Sort
8 3
3 8
 Merge Sort
A[i] A[j]
If (A[i] ≤ A[j])
{
temp[k] ←
A[i] i ← i + 1
k ← k + 1
}
Else
{
temp[k] ← A[j]
j ← j + 1
k ← k + 1
While(i ≤ mid)
{
temp[k] ←
A[i] i ← i + 1
k ← k + 1
}
While(j ≤ high)
{
temp[k] ←
A[j]
j ← j + 1
k ← k + 1
}
Dr.Tamilarasan.S (18CS42) 4/2/2025 54
Merge Sort
 Merge Sort
 Algorithm Analysis
 Merge sort
with n input
size of array
 Basic
Operation:
 Comparison
 Two recursive
calls are made
 Combine two
sub-list
Dr.Tamilarasan.S (18CS42) 4/2/2025 55
Merge Sort
+
Where n > 1 and T(1) = 0
 Master Method
T(n) = T(n/2) + T(n/2) +
cn
T(n) = 2T(n/2) + cn
T(1) = 0
(1)
(2)
 Merge Sort
 Algorithm Analysis
 T(n) = T(n/2)
Time taken by left sub
list to get sorted
T(n/2)
+
Time taken by right
sub list to get sorted
for combining
cn
Time taken
two sub lists
Dr.Tamilarasan.S (18CS42) 4/2/2025 56
Merge Sort
⦿
Dr.Tamilarasan.S (18CS42) 4/2/2025 57
QUICKSORT
 Follows the divide-and-conquer paradigm.
 Divide: Partition (separate) the array into two (possibly empty) sub-arrays
 Conquer: Sort the two sub-arrays by recursive calls to quicksort
 Combine: The sub-arrays are sorted in place – no work is needed
to combine them.
A[0] …………. A[m - 1] A[m] A[m + 1] …….. A[n – 1]
These elements are less than
A[m]
These elements are greater than
A[m]
Mid value
Dr.Tamilarasan.S (18CSL47) 4/2/2025 58
QUICKSORT
Pivot
i
⦿ Example:
⦿ Sort the following Random numbers
 50, 30, 10, 90, 80, 20, 40, 70
◾ Let consider
 a array;
 p is a Pivot value
 p=a[low];
 i=low+1;
j=high
; Low
Dr.Tamilarasan.S (18CSL47) 4/2/2025 59
50 30 10 90 80 20 40 70
High
j
QUICKSORT
⦿ Step: 2
⦿ If a[i] ≤ Pivot, i will
increment
⦿ 30 ≤ 50, then increment i
Pivot
⦿ Step:3
i
Low
50 30 10 90 80 20 40 70
High
j
i
⦿ If a[i] ≤ Pivot, i will
increment
⦿ 10 ≤ 50, then increment i
Dr.Tamilarasan.S (18CSL47) 4/2/2025 60
j
QUICKSORT
⦿ If a[i] ≤ Pivot, i will
increment
⦿ 90 ≤ 50, then increment i will stop
⦿ Step:5
⦿ If a[j]
⦿ 70 > 50, then decrement j
⦿ Step: 4
Pivot i
Low
50 30 10 90 80 20 40 70
High
j
i
> Pivot, j will decrement
Dr.Tamilarasan.S (18CSL47) 4/2/2025 61
j
LAB-4 QUICKSORT
⦿ 40 > 50, then j decrement will stop
⦿ Swap a[i] and a[j] (swap 90 and 40)
⦿ Step:7
 If a[i] < a[low] and a[j] > a[low] then start continue incrementing i and
decrementing j, until the false conditions are obtained 40 < 50
increment i and 90 > 50 decrement j
⦿ Step: 6
Pivot
⦿ If a[j] > Pivot, j will
decrement
i
Low
50 30 10 90 80 20 40 70
High
j
i j
Low
Dr.Tamilarasan.S (18CSL47) 4/2/2025 62
50 30 10 40 80 20 90 70
High
QUICKSORT
a[i] < Pivot (20 < 50) and a[j] > Pivot (80 > 50) continue increment i and
decrement j.
⦿ Step: 8
Pivot
⦿ If a[i] ≤ Pivot, i will increment
⦿ 80 > 50, then i increment will stop then a[j] > pivot decrement j, 20 > 50 so
stop decrement j
⦿ Swap a[i] and a[j] (swap 80 and 20)
⦿ Step:9
Low
50 30 10 40 80 20 90 70
High
i j
i j
Low
Dr.Tamilarasan.S (18CSL47) 4/2/2025 63
50 30 10 40 20 80 90 70
High
QUICKSORT
⦿ Step: 10
⦿ Step: 11
Pivot i, j
Low
50 30 10 40 20 80 90 70
High
j
i
⦿ If a[i] < a[low] and j has crossed i. that is j < i, then swap a[low] or Pivot
and a[j]. ( swap 50 and 20).
Low
50 30 10 40 20 80 90 70
High
Low
20 30 10 40 50 80 90 70
High
Left sub list Right sub list
Pivot is
Shifted at its
position
Dr.Tamilarasan.S (18CSL47) 4/2/2025 64
QUICKSORT
⚫ If a[i] ≤ Pivot then start increment I
⚫ 30 ≤ 20 hen stop increment I
⚫ Then if a[j] > Pivot then decrement j
⚫ 40 > 20 then start decrement j
20
Dr.Tamilarasan.S (18CSL47) 4/2/2025 65
30 10 40
⦿ Step: 12
Consider left sub list
Low High
Pivot i j
QUICKSORT
⚫ Then if a[j] > Pivot then decrement j
⚫ 10 > 20 then stop decrement j
⚫ Swap a[i] and a[j]
20 30 10 40
⦿ Step: 13
Consider left sub list
Low High
Pivot i j
Low High
20 10 30 40
Pivot i j
Dr.Tamilarasan.S (18CSL47) 4/2/2025 66
QUICKSORT
⚫ Then if a[i] ≤ Pivot then increment i
⚫ 10 ≤ 20 then increment I
⚫ a[j] > Pivot, 30 > 20 then
decrement j
20 10 30 40
⦿ Step: 14
Consider left sub list
Low High
Pivot i j
Low High
20 10 30 40
Pivot i, j
Dr.Tamilarasan.S (18CSL47) 4/2/2025 67
QUICKSORT
⚫ a[j] > Pivot, 30 > 20 then
decrement j
⚫ a[j] > Pivot, 10 > 20 then stop decrement j, here j crossed i , swap
a[j] and Pivot (swap 10 and 20)
⦿ Step: 15
Consider left sub list
Low High
20 10 30 40
Pivot i, j
Low High
20 10 30 40
Pivot j i
Dr.Tamilarasan.S (18CSL47) 4/2/2025 68
QUICKSORT
Here a[i] < Pivot (70 < 80) then increment i
⦿ Step: 17
 Sorted left sub list
Low High
10 20 30 40
80
Dr.Tamilarasan.S (18CSL47) 4/2/2025 69
70 90
Apply right sub tree
Low High
Pivot i j
QUICKSORT
Now swap Pivot and a[j] (Swap 80 and 90)
80 70 90
⦿ Step: 18
◾ Apply right sub tree
Low
High
Pivot i, j
80 70 90
Here, a[j] > Pivot (90 > 80) so decrement j
Low High
Pivot i
j
70 80
Dr.Tamilarasan.S (18CSL47) 4/2/2025 70
90
Low High
QUICKSORT
⦿ Step: 19
◾ Combine
Dr.Tamilarasan.S (18CSL47) 4/2/2025 71
10 20 30 40 50 70 80 90
QUICKSORT
Quick Sort
 It is based on the divide-and conquer approach.
 it rearranges elements of a given array A(0 .. n - 1] with respect to
partition (s).
 All the elements before positions are less than
A[s]
 All the elements after positions are greater than A[s]
 A[0] ... A[s -1] A[s] A[s + 1] ... A[n -1]
all are <
A(s]
Dr.Tamilarasan.S (18CSL47) 4/2/2025 72
all are > A(s]
QUICKSORT
Quick Sort
 It is a process of partition of problem.
 To achieve Partition, we need to choose an element from the
array called Pivot.
 The first element in the array is pivot point
 Partition can be done by double Scan approach
Dr.Tamilarasan.S (18CSL47) 4/2/2025 73
QUICKSORT
Quick Sort
 Algorithm
ALGORITHM Quicksort(A[l … r])
//Sorts a sub-array by quicksort
//Input: A sub-array A[L … r] of A[0 … n -1], defined by its left
//and right indices l and r
//Output: Sub-array A[l .. r] sorted in nondecreasing order
if (l < r)
s ←Partition(A[l .. r]) // s
is a split position Quicksort(A[l .. s- 1])
Quicksort(A[s + l…r])
Dr.Tamilarasan.S (18CSL47) 4/2/2025 74
QUICKSORT
Quick Sort
ALGORITHM Partition(A[l ... r])
//Partitions a sub array by Hoare’s algorithm, using the first element as a pivot
//Input: Sub array of array A[0..n − 1], defined by its left and right indices l and r
(l<r)
//Output: Partition of A[l…r], with the split position returned as this function’s value
p := A[l]
i ←l+1; j ←r;
repeat
repeat i ←i + 1 until A[i] ≥ p
repeat j ←j − 1 until A[j ] ≤ p
swap(A[i], A[j ])
until i ≥ j
Dr.Tamilarasan.S (18CSL47) 4/2/2025 75
//undo last swap when i ≥ j
swap(A[i],
A[j ])
swap( p, A[j ])
return j
QUICKSORT
Quick Sort
 Three situations
 Case - 1
◾ If scanning indices i and j have not crossed
◾ (i < j)
 exchange A[i] and A[j] and resume the scans by incrementing
i and decrementing j, respectively
P All are ≤ P ≥ P ………… ≤ P All are ≥P
i j
Dr.Tamilarasan.S (18CSL47) 4/2/2025 76
QUICKSORT
Quick Sort
 Three
situations
 Case - 2
◾ If scanning indices i and j have crossed
◾ (i > j) then
 exchange A[low] and A[j] and
resume
the scans by
incrementing i and decrementing j, respectively
P All are ≤ P ≥ P ≤ P All are ≥P
i
j
Dr.Tamilarasan.S (18CSL47) 4/2/2025 77
QUICKSORT
Quick Sort
 Three situations
 Case - 3
◾ If scanning indices i and j stop while pointing to the same
element
◾ (i = j) then
 The array partitioned, with the split positions = i = j:
P All are ≤ P = P All are
≥P
j
Dr.Tamilarasan.S (18CSL47) 4/2/2025 78
i =
QUICKSORT
Quick Sort-Algorithm Analysis
Best case (Split in the Middle)
Recurrence relation for quick sort
C(n) = C(n / 2) +
C(n / 2) + n
C(1) = 0
Time required to
sort left sub array
Time required to
sort right sub array
Time required for
partitioning the
sub array
Dr.Tamilarasan.S (18CSL47) 4/2/2025 79
QUICKSORT
Quick Sort-Algorithm Analysis
Best case (Split in the Middle)
Recurrence relation for quick sort
C(n)
C(n)
= C(n / 2)
= 2C(n / 2)
+ C(n / 2) + n
+ n ---------------------(1)
C(1) = 0 (2)
Dr.Tamilarasan.S (18CSL47) 4/2/2025 80
Quick Sort
⦿
Dr.Tamilarasan.S (18CS42) 4/2/2025 81
Quick Sort
⦿
Dr.Tamilarasan.S (18CS42) 4/2/2025 82
Quick Sort
⦿
Dr.Tamilarasan.S (18CS42) 4/2/2025 83
Quick Sort
⦿
Dr.Tamilarasan.S (18CS42) 4/2/2025 84
Quick Sort
⦿
C(1) = 0
Dr.Tamilarasan.S (18CS42) 4/2/2025 85
Quick Sort
 Quick Sort - Worst Case Time Complexity
Dr.Tamilarasan.S (18CS42) 4/2/2025 86
Quick Sort
⦿
Dr.Tamilarasan.S (18CS42) 4/2/2025 87
Quick Sort
⦿
Dr.Tamilarasan.S (18CS42) 4/2/2025 88
Ad

Recommended

Data Structure Sorting
Data Structure Sorting
Muhazzab Chouhadry
 
Quick sort.pptx
Quick sort.pptx
Anbarasan Radhakrishnan R
 
s4_quick_sort.ppt
s4_quick_sort.ppt
AliAhmad38278
 
Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
Sorting
Sorting
Shaista Qadir
 
Divide-and-conquer
Divide-and-conquer
Mrunal Patil
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - Notes
Omprakash Chauhan
 
quicksort (1).ppt
quicksort (1).ppt
Balasubramanian699229
 
mergesSort/mnt/data/Merge_Sort_Seminary_Presentation.pptx.ppt
mergesSort/mnt/data/Merge_Sort_Seminary_Presentation.pptx.ppt
545nithi
 
lecture14.pptJH5TVHY6B5TH JNR TN U5YNB JYM
lecture14.pptJH5TVHY6B5TH JNR TN U5YNB JYM
denveramoson
 
quick_sort_with_explanationandImplmentation.ppt
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
sorting algorithms presentation for understanding
sorting algorithms presentation for understanding
rafaelhidalgo005
 
Quick sort
Quick sort
Uma mohan
 
09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Introduction to Algorithms
Introduction to Algorithms
pppepito86
 
Static Models of Continuous Variables
Static Models of Continuous Variables
Economic Research Forum
 
Searching and Sorting Techniques (DSA).pptx
Searching and Sorting Techniques (DSA).pptx
sahadevbkbiet2023
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
Jinho Choi
 
0.5 Rational Expressions
0.5 Rational Expressions
smiller5
 
Quick sort
Quick sort
Jehat Hassan
 
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
denveramoson
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
Sarvesh Rawat
 
Calc Techniques for Boards Exam Purposes
Calc Techniques for Boards Exam Purposes
McdarylLleno
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
Quick sort
Quick sort
Afaq Mansoor Khan
 
1.6 Rational Expressions
1.6 Rational Expressions
smiller5
 
Matrices & Determinants
Matrices & Determinants
Ishant Jain
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 

More Related Content

Similar to MERGE and Quick Sort algorithm explain ppt (20)

mergesSort/mnt/data/Merge_Sort_Seminary_Presentation.pptx.ppt
mergesSort/mnt/data/Merge_Sort_Seminary_Presentation.pptx.ppt
545nithi
 
lecture14.pptJH5TVHY6B5TH JNR TN U5YNB JYM
lecture14.pptJH5TVHY6B5TH JNR TN U5YNB JYM
denveramoson
 
quick_sort_with_explanationandImplmentation.ppt
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
sorting algorithms presentation for understanding
sorting algorithms presentation for understanding
rafaelhidalgo005
 
Quick sort
Quick sort
Uma mohan
 
09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Introduction to Algorithms
Introduction to Algorithms
pppepito86
 
Static Models of Continuous Variables
Static Models of Continuous Variables
Economic Research Forum
 
Searching and Sorting Techniques (DSA).pptx
Searching and Sorting Techniques (DSA).pptx
sahadevbkbiet2023
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
Jinho Choi
 
0.5 Rational Expressions
0.5 Rational Expressions
smiller5
 
Quick sort
Quick sort
Jehat Hassan
 
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
denveramoson
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
Sarvesh Rawat
 
Calc Techniques for Boards Exam Purposes
Calc Techniques for Boards Exam Purposes
McdarylLleno
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
Quick sort
Quick sort
Afaq Mansoor Khan
 
1.6 Rational Expressions
1.6 Rational Expressions
smiller5
 
Matrices & Determinants
Matrices & Determinants
Ishant Jain
 
mergesSort/mnt/data/Merge_Sort_Seminary_Presentation.pptx.ppt
mergesSort/mnt/data/Merge_Sort_Seminary_Presentation.pptx.ppt
545nithi
 
lecture14.pptJH5TVHY6B5TH JNR TN U5YNB JYM
lecture14.pptJH5TVHY6B5TH JNR TN U5YNB JYM
denveramoson
 
quick_sort_with_explanationandImplmentation.ppt
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
sorting algorithms presentation for understanding
sorting algorithms presentation for understanding
rafaelhidalgo005
 
09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Introduction to Algorithms
Introduction to Algorithms
pppepito86
 
Searching and Sorting Techniques (DSA).pptx
Searching and Sorting Techniques (DSA).pptx
sahadevbkbiet2023
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
Jinho Choi
 
0.5 Rational Expressions
0.5 Rational Expressions
smiller5
 
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
8.05.Merge_sort.pptxCUYGYUKTGUIJBJKGKUYGFKJBNVKUYV87VYFHHGVTFGU
denveramoson
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
Sarvesh Rawat
 
Calc Techniques for Boards Exam Purposes
Calc Techniques for Boards Exam Purposes
McdarylLleno
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
1.6 Rational Expressions
1.6 Rational Expressions
smiller5
 
Matrices & Determinants
Matrices & Determinants
Ishant Jain
 

Recently uploaded (20)

Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Cadastral Maps
Cadastral Maps
Google
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Cadastral Maps
Cadastral Maps
Google
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Ad

MERGE and Quick Sort algorithm explain ppt

  • 1. Merge Sort  Merge Sort  Merge sort with n input size of array  Divide:  Divide the n-element sequence to be sorted into two subsequences of n/2 elements each  Conquer  Sort the subsequences recursively using merge sort  When the size of the sequences is 1 there is nothing more to do  Combine  Merge the two sorted sub sequences Dr.Tamilarasan.S (18CS42) 4/2/2025 49
  • 2. Merge Sort  Merge Sort  The array is A[O .. n - 1]  Dividing into two halves  A[0 .. n/2 -1] and A[n/2 .. n -1]  Sorting each of them recursively  Merging the two smaller sorted arrays into a single sorted one. Dr.Tamilarasan.S (18CS42) 4/2/2025 50
  • 3. Merge Sort  Merge Sort ALGORITHM Merge-sort(A[0 .. n - 1]) //Sorts array A[O .. n - 1] by recursive merge-sort //Input: An array A[O .. n - 1] of orderable elements //Output: Array A[O .. n - 1] sorted in non-decreasing order If n > 1 Copy A[0 .. (n/2) -1] to B[0 .. n/2 -1] copy A[(n/2) .. n -1] to C[0 .. (n/2) -1] Merge-sort(B[0 .. (n/2) - 1]) Merge-sort(C[0 .. (n/2) -1]) Merge(B, C, A) Dr.Tamilarasan.S (18CS42) 4/2/2025 51
  • 4. Merge Sort  Merge Sort ALGORITHM Merge(B[0 .. p- 1], C[0 .. q -1], A[0 .. p + q -1])  //Merges two sorted arrays into one sorted array  //Input: Arrays B[O .. p -1] and C[O .. q -1] both sorted  //Output: Sorted array A[O .. p + q -1] of the elements of Band C i ← 0; j ← 0; k ← 0 While i < p and j < q do if B[i] ≤ S C[j] A[k] ← B[i]; i ← i + 1 else A[k] ← C[j]; j ← j + 1 k← k+1 If i = p copy C[j .. q -1] to A[k .. p + q -1] else copy B[i .. p -1] to A[k .. p + q -1] Dr.Tamilarasan.S (18CS42) 4/2/2025 52
  • 5. Merge Sort  Merge Sort 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 8 3 2 9 8 3 7 1 5 4 2 9 7 1 5 4 3 8 2 9 1 7 4 5 2 3 8 9 1 4 5 7 1 2 3 4 5 7 8 9 Dr.Tamilarasan.S (18CS42) 4/2/2025 53
  • 6. Merge Sort 8 3 3 8  Merge Sort A[i] A[j] If (A[i] ≤ A[j]) { temp[k] ← A[i] i ← i + 1 k ← k + 1 } Else { temp[k] ← A[j] j ← j + 1 k ← k + 1 While(i ≤ mid) { temp[k] ← A[i] i ← i + 1 k ← k + 1 } While(j ≤ high) { temp[k] ← A[j] j ← j + 1 k ← k + 1 } Dr.Tamilarasan.S (18CS42) 4/2/2025 54
  • 7. Merge Sort  Merge Sort  Algorithm Analysis  Merge sort with n input size of array  Basic Operation:  Comparison  Two recursive calls are made  Combine two sub-list Dr.Tamilarasan.S (18CS42) 4/2/2025 55
  • 8. Merge Sort + Where n > 1 and T(1) = 0  Master Method T(n) = T(n/2) + T(n/2) + cn T(n) = 2T(n/2) + cn T(1) = 0 (1) (2)  Merge Sort  Algorithm Analysis  T(n) = T(n/2) Time taken by left sub list to get sorted T(n/2) + Time taken by right sub list to get sorted for combining cn Time taken two sub lists Dr.Tamilarasan.S (18CS42) 4/2/2025 56
  • 10. QUICKSORT  Follows the divide-and-conquer paradigm.  Divide: Partition (separate) the array into two (possibly empty) sub-arrays  Conquer: Sort the two sub-arrays by recursive calls to quicksort  Combine: The sub-arrays are sorted in place – no work is needed to combine them. A[0] …………. A[m - 1] A[m] A[m + 1] …….. A[n – 1] These elements are less than A[m] These elements are greater than A[m] Mid value Dr.Tamilarasan.S (18CSL47) 4/2/2025 58
  • 11. QUICKSORT Pivot i ⦿ Example: ⦿ Sort the following Random numbers  50, 30, 10, 90, 80, 20, 40, 70 ◾ Let consider  a array;  p is a Pivot value  p=a[low];  i=low+1; j=high ; Low Dr.Tamilarasan.S (18CSL47) 4/2/2025 59 50 30 10 90 80 20 40 70 High j
  • 12. QUICKSORT ⦿ Step: 2 ⦿ If a[i] ≤ Pivot, i will increment ⦿ 30 ≤ 50, then increment i Pivot ⦿ Step:3 i Low 50 30 10 90 80 20 40 70 High j i ⦿ If a[i] ≤ Pivot, i will increment ⦿ 10 ≤ 50, then increment i Dr.Tamilarasan.S (18CSL47) 4/2/2025 60 j
  • 13. QUICKSORT ⦿ If a[i] ≤ Pivot, i will increment ⦿ 90 ≤ 50, then increment i will stop ⦿ Step:5 ⦿ If a[j] ⦿ 70 > 50, then decrement j ⦿ Step: 4 Pivot i Low 50 30 10 90 80 20 40 70 High j i > Pivot, j will decrement Dr.Tamilarasan.S (18CSL47) 4/2/2025 61 j
  • 14. LAB-4 QUICKSORT ⦿ 40 > 50, then j decrement will stop ⦿ Swap a[i] and a[j] (swap 90 and 40) ⦿ Step:7  If a[i] < a[low] and a[j] > a[low] then start continue incrementing i and decrementing j, until the false conditions are obtained 40 < 50 increment i and 90 > 50 decrement j ⦿ Step: 6 Pivot ⦿ If a[j] > Pivot, j will decrement i Low 50 30 10 90 80 20 40 70 High j i j Low Dr.Tamilarasan.S (18CSL47) 4/2/2025 62 50 30 10 40 80 20 90 70 High
  • 15. QUICKSORT a[i] < Pivot (20 < 50) and a[j] > Pivot (80 > 50) continue increment i and decrement j. ⦿ Step: 8 Pivot ⦿ If a[i] ≤ Pivot, i will increment ⦿ 80 > 50, then i increment will stop then a[j] > pivot decrement j, 20 > 50 so stop decrement j ⦿ Swap a[i] and a[j] (swap 80 and 20) ⦿ Step:9 Low 50 30 10 40 80 20 90 70 High i j i j Low Dr.Tamilarasan.S (18CSL47) 4/2/2025 63 50 30 10 40 20 80 90 70 High
  • 16. QUICKSORT ⦿ Step: 10 ⦿ Step: 11 Pivot i, j Low 50 30 10 40 20 80 90 70 High j i ⦿ If a[i] < a[low] and j has crossed i. that is j < i, then swap a[low] or Pivot and a[j]. ( swap 50 and 20). Low 50 30 10 40 20 80 90 70 High Low 20 30 10 40 50 80 90 70 High Left sub list Right sub list Pivot is Shifted at its position Dr.Tamilarasan.S (18CSL47) 4/2/2025 64
  • 17. QUICKSORT ⚫ If a[i] ≤ Pivot then start increment I ⚫ 30 ≤ 20 hen stop increment I ⚫ Then if a[j] > Pivot then decrement j ⚫ 40 > 20 then start decrement j 20 Dr.Tamilarasan.S (18CSL47) 4/2/2025 65 30 10 40 ⦿ Step: 12 Consider left sub list Low High Pivot i j
  • 18. QUICKSORT ⚫ Then if a[j] > Pivot then decrement j ⚫ 10 > 20 then stop decrement j ⚫ Swap a[i] and a[j] 20 30 10 40 ⦿ Step: 13 Consider left sub list Low High Pivot i j Low High 20 10 30 40 Pivot i j Dr.Tamilarasan.S (18CSL47) 4/2/2025 66
  • 19. QUICKSORT ⚫ Then if a[i] ≤ Pivot then increment i ⚫ 10 ≤ 20 then increment I ⚫ a[j] > Pivot, 30 > 20 then decrement j 20 10 30 40 ⦿ Step: 14 Consider left sub list Low High Pivot i j Low High 20 10 30 40 Pivot i, j Dr.Tamilarasan.S (18CSL47) 4/2/2025 67
  • 20. QUICKSORT ⚫ a[j] > Pivot, 30 > 20 then decrement j ⚫ a[j] > Pivot, 10 > 20 then stop decrement j, here j crossed i , swap a[j] and Pivot (swap 10 and 20) ⦿ Step: 15 Consider left sub list Low High 20 10 30 40 Pivot i, j Low High 20 10 30 40 Pivot j i Dr.Tamilarasan.S (18CSL47) 4/2/2025 68
  • 21. QUICKSORT Here a[i] < Pivot (70 < 80) then increment i ⦿ Step: 17  Sorted left sub list Low High 10 20 30 40 80 Dr.Tamilarasan.S (18CSL47) 4/2/2025 69 70 90 Apply right sub tree Low High Pivot i j
  • 22. QUICKSORT Now swap Pivot and a[j] (Swap 80 and 90) 80 70 90 ⦿ Step: 18 ◾ Apply right sub tree Low High Pivot i, j 80 70 90 Here, a[j] > Pivot (90 > 80) so decrement j Low High Pivot i j 70 80 Dr.Tamilarasan.S (18CSL47) 4/2/2025 70 90 Low High
  • 23. QUICKSORT ⦿ Step: 19 ◾ Combine Dr.Tamilarasan.S (18CSL47) 4/2/2025 71 10 20 30 40 50 70 80 90
  • 24. QUICKSORT Quick Sort  It is based on the divide-and conquer approach.  it rearranges elements of a given array A(0 .. n - 1] with respect to partition (s).  All the elements before positions are less than A[s]  All the elements after positions are greater than A[s]  A[0] ... A[s -1] A[s] A[s + 1] ... A[n -1] all are < A(s] Dr.Tamilarasan.S (18CSL47) 4/2/2025 72 all are > A(s]
  • 25. QUICKSORT Quick Sort  It is a process of partition of problem.  To achieve Partition, we need to choose an element from the array called Pivot.  The first element in the array is pivot point  Partition can be done by double Scan approach Dr.Tamilarasan.S (18CSL47) 4/2/2025 73
  • 26. QUICKSORT Quick Sort  Algorithm ALGORITHM Quicksort(A[l … r]) //Sorts a sub-array by quicksort //Input: A sub-array A[L … r] of A[0 … n -1], defined by its left //and right indices l and r //Output: Sub-array A[l .. r] sorted in nondecreasing order if (l < r) s ←Partition(A[l .. r]) // s is a split position Quicksort(A[l .. s- 1]) Quicksort(A[s + l…r]) Dr.Tamilarasan.S (18CSL47) 4/2/2025 74
  • 27. QUICKSORT Quick Sort ALGORITHM Partition(A[l ... r]) //Partitions a sub array by Hoare’s algorithm, using the first element as a pivot //Input: Sub array of array A[0..n − 1], defined by its left and right indices l and r (l<r) //Output: Partition of A[l…r], with the split position returned as this function’s value p := A[l] i ←l+1; j ←r; repeat repeat i ←i + 1 until A[i] ≥ p repeat j ←j − 1 until A[j ] ≤ p swap(A[i], A[j ]) until i ≥ j Dr.Tamilarasan.S (18CSL47) 4/2/2025 75 //undo last swap when i ≥ j swap(A[i], A[j ]) swap( p, A[j ]) return j
  • 28. QUICKSORT Quick Sort  Three situations  Case - 1 ◾ If scanning indices i and j have not crossed ◾ (i < j)  exchange A[i] and A[j] and resume the scans by incrementing i and decrementing j, respectively P All are ≤ P ≥ P ………… ≤ P All are ≥P i j Dr.Tamilarasan.S (18CSL47) 4/2/2025 76
  • 29. QUICKSORT Quick Sort  Three situations  Case - 2 ◾ If scanning indices i and j have crossed ◾ (i > j) then  exchange A[low] and A[j] and resume the scans by incrementing i and decrementing j, respectively P All are ≤ P ≥ P ≤ P All are ≥P i j Dr.Tamilarasan.S (18CSL47) 4/2/2025 77
  • 30. QUICKSORT Quick Sort  Three situations  Case - 3 ◾ If scanning indices i and j stop while pointing to the same element ◾ (i = j) then  The array partitioned, with the split positions = i = j: P All are ≤ P = P All are ≥P j Dr.Tamilarasan.S (18CSL47) 4/2/2025 78 i =
  • 31. QUICKSORT Quick Sort-Algorithm Analysis Best case (Split in the Middle) Recurrence relation for quick sort C(n) = C(n / 2) + C(n / 2) + n C(1) = 0 Time required to sort left sub array Time required to sort right sub array Time required for partitioning the sub array Dr.Tamilarasan.S (18CSL47) 4/2/2025 79
  • 32. QUICKSORT Quick Sort-Algorithm Analysis Best case (Split in the Middle) Recurrence relation for quick sort C(n) C(n) = C(n / 2) = 2C(n / 2) + C(n / 2) + n + n ---------------------(1) C(1) = 0 (2) Dr.Tamilarasan.S (18CSL47) 4/2/2025 80
  • 37. Quick Sort ⦿ C(1) = 0 Dr.Tamilarasan.S (18CS42) 4/2/2025 85
  • 38. Quick Sort  Quick Sort - Worst Case Time Complexity Dr.Tamilarasan.S (18CS42) 4/2/2025 86