SlideShare a Scribd company logo
5
Most read
13
Most read
16
Most read
Merge Sort and Quick Sort
CSC 391
2
Sorting
• Insertion sort
– Design approach:
– Sorts in place:
– Best case:
– Worst case:
• Bubble Sort
– Design approach:
– Sorts in place:
– Running time:
Yes
(n)
(n2)
incremental
Yes
(n2)
incremental
3
Sorting
• Selection sort
– Design approach:
– Sorts in place:
– Running time:
• Merge Sort
– Design approach:
– Sorts in place:
– Running time:
Yes
(n2)
incremental
No
Let’s see!!
divide and conquer
4
Divide-and-Conquer
• Divide the problem into a number of sub-problems
– Similar sub-problems of smaller size
• Conquer the sub-problems
– Solve the sub-problems recursively
– Sub-problem size small enough  solve the problems in
straightforward manner
• Combine the solutions of the sub-problems
– Obtain the solution for the original problem
5
Merge Sort Approach
• To sort an array A[p . . r]:
• 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 subsequences
6
Merge Sort
Alg.: MERGE-SORT(A, p, r)
if p < r Check for base case
then q ← (p + r)/2 Divide
MERGE-SORT(A, p, q) Conquer
MERGE-SORT(A, q + 1, r) Conquer
MERGE(A, p, q, r) Combine
• Initial call: MERGE-SORT(A, 1, n)
1 2 3 4 5 6 7 8
62317425
p rq
7
Example – n Power of 2
1 2 3 4 5 6 7 8
q = 462317425
1 2 3 4
7425
5 6 7 8
6231
1 2
25
3 4
74
5 6
31
7 8
62
1
5
2
2
3
4
4
7 1
6
3
7
2
8
6
5
Divide
8
Example – n Power of 2
1
5
2
2
3
4
4
7 1
6
3
7
2
8
6
5
1 2 3 4 5 6 7 8
76543221
1 2 3 4
7542
5 6 7 8
6321
1 2
52
3 4
74
5 6
31
7 8
62
Conquer
and
Merge
9
Example – n Not a Power of 2
62537416274
1 2 3 4 5 6 7 8 9 10 11
q = 6
416274
1 2 3 4 5 6
62537
7 8 9 10 11
q = 9q = 3
274
1 2 3
416
4 5 6
537
7 8 9
62
10 11
74
1 2
2
3
16
4 5
4
6
37
7 8
5
9
2
10
6
11
4
1
7
2
6
4
1
5
7
7
3
8
Divide
10
Example – n Not a Power of 2
77665443221
1 2 3 4 5 6 7 8 9 10 11
764421
1 2 3 4 5 6
76532
7 8 9 10 11
742
1 2 3
641
4 5 6
753
7 8 9
62
10 11
2
3
4
6
5
9
2
10
6
11
4
1
7
2
6
4
1
5
7
7
3
8
74
1 2
61
4 5
73
7 8
Conquer
and
Merge
11
Merge - Pseudocode
Alg.: MERGE(A, p, q, r)
1. Compute n1 and n2
2. Copy the first n1 elements into L[1
. . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]
3. L[n1 + 1] ← ; R[n2 + 1] ← 
4. i ← 1; j ← 1
5. for k ← p to r
6. do if L[ i ] ≤ R[ j ]
7. then A[k] ← L[ i ]
8. i ←i + 1
9. else A[k] ← R[ j ]
10. j ← j + 1
p q
7542
6321
rq + 1
L
R


1 2 3 4 5 6 7 8
63217542
p rq
n1 n2
Algorithm:
mergesort( int [] a, int left, int right)
{
if (right > left)
{ middle = left + (right - left)/2;
mergesort(a, left, middle);
mergesort(a, middle+1, right);
merge(a, left, middle, right); }
}
Assumption: N is a power of two. For N = 1: time is a constant (denoted by 1)
Otherwise, time to mergesort N elements = time to mergesort N/2 elements + time to merge
two arrays each N/2 elements.
Time to merge two arrays each N/2 elements is linear, i.e. N
Thus we have:
(1) T(1) = 1
(2) T(N) = 2T(N/2) + N
Next we will solve this recurrence relation. First we divide (2) by N:
(3) T(N) / N = T(N/2) / (N/2) + 1
Complexity of Merge Sort
N is a power of two, so we can write
(4) T(N/2) / (N/2) = T(N/4) / (N/4) +1
(5) T(N/4) / (N/4) = T(N/8) / (N/8) +1
(6) T(N/8) / (N/8) = T(N/16) / (N/16) +1
(7) ……
(8) T(2) / 2 = T(1) / 1 + 1
Now we add equations (3) through (8) : the sum of their left-hand sides will be equal to
the sum of their right-hand sides:
T(N) / N + T(N/2) / (N/2) + T(N/4) / (N/4) + … + T(2)/2 =
T(N/2) / (N/2) + T(N/4) / (N/4) + ….+ T(2) / 2 + T(1) / 1 + LogN
(LogN is the sum of 1s in the right-hand sides)
After crossing the equal term, we get
(9) T(N)/N = T(1)/1 + LogN
T(1) is 1, hence we obtain
(10) T(N) = N + NlogN = O(NlogN)
Hence the complexity of the MergeSort algorithm is O(NlogN).
Complexity of Merge Sort
14
Quicksort
• Sort an array A[p…r]
• Divide
– Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that
each element of A[p..q] is smaller than or equal to each element in
A[q+1..r]
– Need to find index q to partition the array
≤A[p…q] A[q+1…r]
15
Quicksort
• Conquer
– Recursively sort A[p..q] and A[q+1..r] using Quicksort
• Combine
– Trivial: the arrays are sorted in place
– No additional work is required to combine them
– The entire array is now sorted
A[p…q] A[q+1…r]≤
16
Recurrence
Alg.: QUICKSORT(A, p, r)
if p < r
then q  PARTITION(A, p, r)
QUICKSORT (A, p, q)
QUICKSORT (A, q+1, r)
Recurrence:
Initially: p=1, r=n
T(n) = T(q) + T(n – q) + n
17
Partitioning the Array
Alg. PARTITION (A, p, r)
1. x  A[p]
2. i  p – 1
3. j  r + 1
4. while TRUE
5. do repeat j  j – 1
6. until A[j] ≤ x
7. do repeat i  i + 1
8. until A[i] ≥ x
9. if i < j
10. then exchange A[i]  A[j]
11. else return j
Running time: (n)
n = r – p + 1
73146235
i j
A:
arap
ij=q
A:
A[p…q] A[q+1…r]≤
p r
Each element is
visited once!
Partition can be done in O(n) time, where n is the size of
the array. Let T(n) be the number of comparisons
required by Quicksort.
If the pivot ends up at position k, then we have
To determine best-, worst-, and average-case complexity
we need to determine the values of k that correspond
to these cases.
Analysis of quicksort
T(n) T(nk)  T(k 1)  n
Best-Case Complexity
The best case is clearly when the pivot always
partitions the array equally.
Intuitively, this would lead to a recursive depth of at
most lg n calls
We can actually prove this. In this case
– T(n) T(n/2)  T(n/2)  n  (n lg n)
Best Case Partitioning
• Best-case partitioning
– Partitioning produces two regions of size n/2
• Recurrence: q=n/2
T(n) = 2T(n/2) + (n)
T(n) = (nlgn) (Master theorem)
Average-Case Complexity
Average case is rather complex, but is where the
algorithm earns its name. The bottom line is: T(n) =
(nlgn)
Worst Case Partitioning
The worst-case behavior for quicksort occurs when the
partitioning routine produces one region with n - 1 elements and
one with only l element.
Let us assume that this
Unbalanced partitioning arises
at every step of the algorithm.
Since partitioning costs (n) time
and T(1) = (1), the recurrence for
the running time is
T(n) = T(n - 1) + (n).
To evaluate this recurrence, we observe that T(1) = (1) and then
iterate:
n
n - 1
n - 2
n - 3
2
1
1
1
1
1
1
n
n
n
n - 1
n - 2
3
2
(n2)
Best case: split in the middle — Θ( n log n)
Worst case: sorted array! — Θ( n2)
Average case: random arrays — Θ( n log n)
Worst Case Partitioning

More Related Content

PPTX
Waste management
Vivek Jain
 
PPTX
Counting Sort
Faiza Saleem
 
PPTX
Presentation on e learning management system
Hunain Asghar
 
PPTX
Hashing
Amar Jukuntla
 
PPTX
Bangladesh in international affairs
Jubayer Alam Shoikat
 
PPTX
cloud computing 5.pptx
Jatin673232
 
DOCX
Airline Reservation System Documentation
Sanjana Agarwal
 
PPTX
The Teaching Profession
Deah Galas
 
Waste management
Vivek Jain
 
Counting Sort
Faiza Saleem
 
Presentation on e learning management system
Hunain Asghar
 
Hashing
Amar Jukuntla
 
Bangladesh in international affairs
Jubayer Alam Shoikat
 
cloud computing 5.pptx
Jatin673232
 
Airline Reservation System Documentation
Sanjana Agarwal
 
The Teaching Profession
Deah Galas
 

What's hot (20)

PPTX
Sorting Algorithms
Pranay Neema
 
PPTX
Asymptotic Notation
Protap Mondal
 
PPTX
Merge sort algorithm
Shubham Dwivedi
 
PPT
Bubble sort
Manek Ar
 
PDF
Quick sort algorithn
Kumar
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPTX
Bubble Sort Algorithm Presentation
AhmedAlbutty
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PPTX
heap Sort Algorithm
Lemia Algmri
 
PPTX
Merge Sort
Nikhil Sonkamble
 
PDF
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
PPTX
Merge Sort vs Quick Sort presentation
maharajdey
 
PPTX
Asymptotic notations
Nikhil Sharma
 
PPTX
Insertion sort
almaqboli
 
PPTX
Quick sort
Dhruv Sabalpara
 
PPT
Time complexity
Katang Isip
 
PPTX
Binary search
AparnaKumari31
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PDF
Heap and heapsort
Amit Kumar Rathi
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Sorting Algorithms
Pranay Neema
 
Asymptotic Notation
Protap Mondal
 
Merge sort algorithm
Shubham Dwivedi
 
Bubble sort
Manek Ar
 
Quick sort algorithn
Kumar
 
Dinive conquer algorithm
Mohd Arif
 
Bubble Sort Algorithm Presentation
AhmedAlbutty
 
Divide and conquer
Dr Shashikant Athawale
 
heap Sort Algorithm
Lemia Algmri
 
Merge Sort
Nikhil Sonkamble
 
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Merge Sort vs Quick Sort presentation
maharajdey
 
Asymptotic notations
Nikhil Sharma
 
Insertion sort
almaqboli
 
Quick sort
Dhruv Sabalpara
 
Time complexity
Katang Isip
 
Binary search
AparnaKumari31
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Heap and heapsort
Amit Kumar Rathi
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Ad

Viewers also liked (13)

PPT
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
PPTX
Sorting
Ashim Lamichhane
 
PDF
Lecture 07 Data Structures - Basic Sorting
Haitham El-Ghareeb
 
PPT
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
PDF
Sorting
Gopi Saiteja
 
PDF
Data Structures & Algorithm design using C
Emertxe Information Technologies Pvt Ltd
 
PDF
Sorting
Zaid Shabbir
 
PDF
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
PPT
Complexity of Algorithm
Muhammad Muzammal
 
DOC
Time and space complexity
Ankit Katiyar
 
PPTX
Asymptotic Notations
Rishabh Soni
 
PPT
Introduction to data structures and Algorithm
Dhaval Kaneria
 
PDF
Sorting Algorithms
Mohammed Hussein
 
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Lecture 07 Data Structures - Basic Sorting
Haitham El-Ghareeb
 
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Sorting
Gopi Saiteja
 
Data Structures & Algorithm design using C
Emertxe Information Technologies Pvt Ltd
 
Sorting
Zaid Shabbir
 
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Complexity of Algorithm
Muhammad Muzammal
 
Time and space complexity
Ankit Katiyar
 
Asymptotic Notations
Rishabh Soni
 
Introduction to data structures and Algorithm
Dhaval Kaneria
 
Sorting Algorithms
Mohammed Hussein
 
Ad

Similar to Merge sort and quick sort (20)

PDF
Merge Sort
Juan Zamora, MSc. MBA
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PPT
Divide and conquer
Vikas Sharma
 
PPT
03 dc
Hira Gul
 
PPT
5.2 divide and conquer
Krish_ver2
 
PPT
Algorithm: Quick-Sort
Tareq Hasan
 
PPTX
Divide and Conquer in DAA concept. For B Tech CSE
RUHULAMINHAZARIKA
 
PPTX
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
PPT
Unit-2-Sorting (Merge+Quick+Heap+Binary Searach).ppt
sajalsinghal1512
 
PDF
Quicksort analysis
Premjeet Roy
 
PDF
Skiena algorithm 2007 lecture09 linear sorting
zukun
 
PPTX
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
PPT
Introduction
pilavare
 
PPT
Admission in india 2015
Edhole.com
 
PPT
Algorithm.ppt
Tareq Hasan
 
PPT
recurrence relations in analysis of algorithm
vidhyapm2
 
PPT
3-Chapter Three - Divide and Conquer.ppt
mershaabdisa
 
PDF
Skiena algorithm 2007 lecture08 quicksort
zukun
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
MergesortQuickSort.ppt
AliAhmad38278
 
Divide and conquer
Vikas Sharma
 
03 dc
Hira Gul
 
5.2 divide and conquer
Krish_ver2
 
Algorithm: Quick-Sort
Tareq Hasan
 
Divide and Conquer in DAA concept. For B Tech CSE
RUHULAMINHAZARIKA
 
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Unit-2-Sorting (Merge+Quick+Heap+Binary Searach).ppt
sajalsinghal1512
 
Quicksort analysis
Premjeet Roy
 
Skiena algorithm 2007 lecture09 linear sorting
zukun
 
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Introduction
pilavare
 
Admission in india 2015
Edhole.com
 
Algorithm.ppt
Tareq Hasan
 
recurrence relations in analysis of algorithm
vidhyapm2
 
3-Chapter Three - Divide and Conquer.ppt
mershaabdisa
 
Skiena algorithm 2007 lecture08 quicksort
zukun
 

More from Shakila Mahjabin (15)

PPTX
Computer processing
Shakila Mahjabin
 
PPTX
Arrays in CPP
Shakila Mahjabin
 
DOCX
CSC 433 Sample normalization SQL Question
Shakila Mahjabin
 
PPT
SQL : introduction
Shakila Mahjabin
 
PPT
Normalization
Shakila Mahjabin
 
PDF
Solution of Erds
Shakila Mahjabin
 
PPT
Entity Relationship Diagram
Shakila Mahjabin
 
PPT
Ch1- Introduction to dbms
Shakila Mahjabin
 
PPTX
Stack and queue
Shakila Mahjabin
 
PPTX
Algo analysis
Shakila Mahjabin
 
PDF
Codes on structures
Shakila Mahjabin
 
PDF
Arrays
Shakila Mahjabin
 
PDF
array, function, pointer, pattern matching
Shakila Mahjabin
 
PDF
String operation
Shakila Mahjabin
 
PDF
Data Structure Basics
Shakila Mahjabin
 
Computer processing
Shakila Mahjabin
 
Arrays in CPP
Shakila Mahjabin
 
CSC 433 Sample normalization SQL Question
Shakila Mahjabin
 
SQL : introduction
Shakila Mahjabin
 
Normalization
Shakila Mahjabin
 
Solution of Erds
Shakila Mahjabin
 
Entity Relationship Diagram
Shakila Mahjabin
 
Ch1- Introduction to dbms
Shakila Mahjabin
 
Stack and queue
Shakila Mahjabin
 
Algo analysis
Shakila Mahjabin
 
Codes on structures
Shakila Mahjabin
 
array, function, pointer, pattern matching
Shakila Mahjabin
 
String operation
Shakila Mahjabin
 
Data Structure Basics
Shakila Mahjabin
 

Recently uploaded (20)

PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 

Merge sort and quick sort

  • 1. Merge Sort and Quick Sort CSC 391
  • 2. 2 Sorting • Insertion sort – Design approach: – Sorts in place: – Best case: – Worst case: • Bubble Sort – Design approach: – Sorts in place: – Running time: Yes (n) (n2) incremental Yes (n2) incremental
  • 3. 3 Sorting • Selection sort – Design approach: – Sorts in place: – Running time: • Merge Sort – Design approach: – Sorts in place: – Running time: Yes (n2) incremental No Let’s see!! divide and conquer
  • 4. 4 Divide-and-Conquer • Divide the problem into a number of sub-problems – Similar sub-problems of smaller size • Conquer the sub-problems – Solve the sub-problems recursively – Sub-problem size small enough  solve the problems in straightforward manner • Combine the solutions of the sub-problems – Obtain the solution for the original problem
  • 5. 5 Merge Sort Approach • To sort an array A[p . . r]: • 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 subsequences
  • 6. 6 Merge Sort Alg.: MERGE-SORT(A, p, r) if p < r Check for base case then q ← (p + r)/2 Divide MERGE-SORT(A, p, q) Conquer MERGE-SORT(A, q + 1, r) Conquer MERGE(A, p, q, r) Combine • Initial call: MERGE-SORT(A, 1, n) 1 2 3 4 5 6 7 8 62317425 p rq
  • 7. 7 Example – n Power of 2 1 2 3 4 5 6 7 8 q = 462317425 1 2 3 4 7425 5 6 7 8 6231 1 2 25 3 4 74 5 6 31 7 8 62 1 5 2 2 3 4 4 7 1 6 3 7 2 8 6 5 Divide
  • 8. 8 Example – n Power of 2 1 5 2 2 3 4 4 7 1 6 3 7 2 8 6 5 1 2 3 4 5 6 7 8 76543221 1 2 3 4 7542 5 6 7 8 6321 1 2 52 3 4 74 5 6 31 7 8 62 Conquer and Merge
  • 9. 9 Example – n Not a Power of 2 62537416274 1 2 3 4 5 6 7 8 9 10 11 q = 6 416274 1 2 3 4 5 6 62537 7 8 9 10 11 q = 9q = 3 274 1 2 3 416 4 5 6 537 7 8 9 62 10 11 74 1 2 2 3 16 4 5 4 6 37 7 8 5 9 2 10 6 11 4 1 7 2 6 4 1 5 7 7 3 8 Divide
  • 10. 10 Example – n Not a Power of 2 77665443221 1 2 3 4 5 6 7 8 9 10 11 764421 1 2 3 4 5 6 76532 7 8 9 10 11 742 1 2 3 641 4 5 6 753 7 8 9 62 10 11 2 3 4 6 5 9 2 10 6 11 4 1 7 2 6 4 1 5 7 7 3 8 74 1 2 61 4 5 73 7 8 Conquer and Merge
  • 11. 11 Merge - Pseudocode Alg.: MERGE(A, p, q, r) 1. Compute n1 and n2 2. Copy the first n1 elements into L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1] 3. L[n1 + 1] ← ; R[n2 + 1] ←  4. i ← 1; j ← 1 5. for k ← p to r 6. do if L[ i ] ≤ R[ j ] 7. then A[k] ← L[ i ] 8. i ←i + 1 9. else A[k] ← R[ j ] 10. j ← j + 1 p q 7542 6321 rq + 1 L R   1 2 3 4 5 6 7 8 63217542 p rq n1 n2
  • 12. Algorithm: mergesort( int [] a, int left, int right) { if (right > left) { middle = left + (right - left)/2; mergesort(a, left, middle); mergesort(a, middle+1, right); merge(a, left, middle, right); } } Assumption: N is a power of two. For N = 1: time is a constant (denoted by 1) Otherwise, time to mergesort N elements = time to mergesort N/2 elements + time to merge two arrays each N/2 elements. Time to merge two arrays each N/2 elements is linear, i.e. N Thus we have: (1) T(1) = 1 (2) T(N) = 2T(N/2) + N Next we will solve this recurrence relation. First we divide (2) by N: (3) T(N) / N = T(N/2) / (N/2) + 1 Complexity of Merge Sort
  • 13. N is a power of two, so we can write (4) T(N/2) / (N/2) = T(N/4) / (N/4) +1 (5) T(N/4) / (N/4) = T(N/8) / (N/8) +1 (6) T(N/8) / (N/8) = T(N/16) / (N/16) +1 (7) …… (8) T(2) / 2 = T(1) / 1 + 1 Now we add equations (3) through (8) : the sum of their left-hand sides will be equal to the sum of their right-hand sides: T(N) / N + T(N/2) / (N/2) + T(N/4) / (N/4) + … + T(2)/2 = T(N/2) / (N/2) + T(N/4) / (N/4) + ….+ T(2) / 2 + T(1) / 1 + LogN (LogN is the sum of 1s in the right-hand sides) After crossing the equal term, we get (9) T(N)/N = T(1)/1 + LogN T(1) is 1, hence we obtain (10) T(N) = N + NlogN = O(NlogN) Hence the complexity of the MergeSort algorithm is O(NlogN). Complexity of Merge Sort
  • 14. 14 Quicksort • Sort an array A[p…r] • Divide – Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of A[p..q] is smaller than or equal to each element in A[q+1..r] – Need to find index q to partition the array ≤A[p…q] A[q+1…r]
  • 15. 15 Quicksort • Conquer – Recursively sort A[p..q] and A[q+1..r] using Quicksort • Combine – Trivial: the arrays are sorted in place – No additional work is required to combine them – The entire array is now sorted A[p…q] A[q+1…r]≤
  • 16. 16 Recurrence Alg.: QUICKSORT(A, p, r) if p < r then q  PARTITION(A, p, r) QUICKSORT (A, p, q) QUICKSORT (A, q+1, r) Recurrence: Initially: p=1, r=n T(n) = T(q) + T(n – q) + n
  • 17. 17 Partitioning the Array Alg. PARTITION (A, p, r) 1. x  A[p] 2. i  p – 1 3. j  r + 1 4. while TRUE 5. do repeat j  j – 1 6. until A[j] ≤ x 7. do repeat i  i + 1 8. until A[i] ≥ x 9. if i < j 10. then exchange A[i]  A[j] 11. else return j Running time: (n) n = r – p + 1 73146235 i j A: arap ij=q A: A[p…q] A[q+1…r]≤ p r Each element is visited once!
  • 18. Partition can be done in O(n) time, where n is the size of the array. Let T(n) be the number of comparisons required by Quicksort. If the pivot ends up at position k, then we have To determine best-, worst-, and average-case complexity we need to determine the values of k that correspond to these cases. Analysis of quicksort T(n) T(nk)  T(k 1)  n
  • 19. Best-Case Complexity The best case is clearly when the pivot always partitions the array equally. Intuitively, this would lead to a recursive depth of at most lg n calls We can actually prove this. In this case – T(n) T(n/2)  T(n/2)  n  (n lg n)
  • 20. Best Case Partitioning • Best-case partitioning – Partitioning produces two regions of size n/2 • Recurrence: q=n/2 T(n) = 2T(n/2) + (n) T(n) = (nlgn) (Master theorem)
  • 21. Average-Case Complexity Average case is rather complex, but is where the algorithm earns its name. The bottom line is: T(n) = (nlgn)
  • 22. Worst Case Partitioning The worst-case behavior for quicksort occurs when the partitioning routine produces one region with n - 1 elements and one with only l element. Let us assume that this Unbalanced partitioning arises at every step of the algorithm. Since partitioning costs (n) time and T(1) = (1), the recurrence for the running time is T(n) = T(n - 1) + (n). To evaluate this recurrence, we observe that T(1) = (1) and then iterate: n n - 1 n - 2 n - 3 2 1 1 1 1 1 1 n n n n - 1 n - 2 3 2 (n2)
  • 23. Best case: split in the middle — Θ( n log n) Worst case: sorted array! — Θ( n2) Average case: random arrays — Θ( n log n) Worst Case Partitioning