Quicksort
Dr. Ra’Fat A. AL-msie’deen
Chapter 4
Mutah University
Faculty of IT, Department of Software Engineering
Outlines: Quicksort
• Quicksort: Advantages and Disadvantages.
• Quicksort:
o Quicksort Function.
o Partition Function.
o Choice Of Pivot:
 Rightmost or Leftmost.
 Randomly.
 Median-of-Three Rule.
o Partitioning using Additional Array.
o In-Place Partitioning.
o Runtime of Quicksort (Worst, Best, Average).
• Randomized Quicksort.
2
Quicksort
• Quicksort pros:
o Sorts in place. “rearrange the elements where they are”.
o Sorts θ(n lg n) in the average case.
o Very efficient in practice.
• Quicksort cons:
o Sorts θ(n2) in the worst case.
o not stable:
 does not preserve the relative order of elements with equal
keys.
 Sorting algorithm is stable if 2 records with the same key stay
in original order.
o But in practice, it’s quick.
o And the worst case doesn’t happen often … sorted.
3
Quicksort
• Another divide-and-conquer algorithm:
• Divide: A[p…r] is partitioned (rearranged) into two nonempty
subarrays A[p…q-1] and A[q+1…r] such that each element of
A[p…q-1] is less than or equal to each element of A[q+1…r].
Index q is computed here, called pivot.
• Conquer: two subarrays are sorted by recursive calls to
quicksort.
• Combine: unlike merge sort, no work needed since the
subarrays are sorted in place already.
4
Quicksort
• The basic algorithm to sort an array A consists of the following
four easy steps:
1. If the number of elements in A is 0 or 1, then return.
2. Pick any element v in A. This is called the pivot.
3. Partition A-{v} (the remaining elements in A) into two
disjoint groups:
o A1 = {x ∈ A-{v} | x ≤ v}, and
o A2 = {x ∈ A-{v} | x ≥ v}
4. return:
o {quicksort(A1) followed by v followed by quicksort(A2)}
5
A1 x ≤ v v A2 x ≥ v
Quicksort
• Small instance has n ≤ 1.
o Every small instance is a sorted instance.
• To sort a large instance:
o select a pivot element from out of the n elements.
• Partition the n elements into 3 groups left, middle and right.
o The middle group contains only the pivot element.
o All elements in the left group are ≤ pivot.
o All elements in the right group are ≥ pivot.
• Sort left and right groups recursively.
• Answer is sorted left group, followed by middle group followed
by sorted right group.
6
Quicksort - Example
Use 6 as the pivot
Sort left and right groups recursively
7
6 2 8 5 11 10 4 1 9 7 3
2 5 4 1 3 6 7 9 10 11 8
Quicksort - Code
QUICKSORT(A, p, r)
1 if p < r
2 q = PARTITION(A, p, r)
3 QUICKSORT(A, p, q - 1)
4 QUICKSORT(A, q + 1, r)
o To sort an entire array A, the initial call is QUICKSORT(A, 1,
A:length).
o Initial call is Quicksort(A, 1, n), where n is the length of A.
8
Partition
• Clearly, all the action takes place in the partition() function.
o Rearranges the subarray in place.
o End result:
 Two subarrays.
 All values in first subarray ≤ all values in second.
o Returns the index of the “pivot” element separating the
two subarrays.
9
Partition - Code
• Partitioning the array: The key to the algorithm is the PARTITION
procedure, which rearranges the subarray A[p … r] in place.
PARTITION(A, p, r)
1 x = A[r]
2 i = p - 1
3 for j = p to r - 1
4 if A[j] ≤ x
5 i = i + 1
6 exchange A[i] with A[j]
7 exchange A[i + 1] with A[r]
8 return i + 1
10
Partition - Code
• The four regions maintained by the procedure PARTITION on a
subarray A[p … r].
o The values in A[p ... i] are all less than or equal to x,
o the values in A[i + 1 ... j - 1] are all greater than x,
o and A[r] = x.
o The subarray A[j ... r - 1] can take on any values.
11
Partition - Code
 The two cases for one
iteration of procedure
PARTITION.
1. (a) If A[j] > x, the only
action is to increment j ,
which maintains the loop
invariant.
2. (b) If A[j] ≤ x, index i is
incremented, A[i] and
A[j] are swapped, and
then j is incremented.
Again, the loop invariant
is maintained.
12
Partition Example
• A = {2, 8, 7, 1, 3, 5, 6, 4}
13
Partition Example - Explanation
• Lightly shaded elements are in the first partition with values ≤ x
(pivot).
• Heavily shaded elements are in the second partition with values ≥ x
(pivot).
• The unshaded elements have not yet been put in one of the first
two partitions.
• The final white element is the pivot.
14
Partition Example - Explanation
• (a) The initial array and variable settings. None of the
elements have been placed in either of the first two
partitions.
• (b) The value 2 is “swapped with itself” and put in the
partition of smaller values.
15
Partition Example - Explanation
• (c)–(d) The values 8 and 7 are added to the partition of larger
values.
• (e) The values 1 and 8 are swapped, and the smaller partition
grows.
16
Partition Example - Explanation
• (f) The values 3 and 7 are swapped, and the smaller partition
grows.
• (g)–(h) The larger partition grows to include 5 and 6, and the
loop terminates.
17
Partition Example - Explanation
• (i) In lines 7–8, the pivot element is swapped so that it lies
between the two partitions.
18
PARTITION(A, p, r)
1 x = A[r]
2 i = p - 1
3 for j = p to r - 1
4 if A[j] ≤ x
5 i = i + 1
6 exchange A[i] with A[j]
7 exchange A[i + 1] with A[r]
8 return i + 1
Example: On an 8-element subarray.
19[The index j disappears because it is no longer needed once the for loop is exited.]
Choice Of Pivot
• Pivot is rightmost element in list that is to be sorted.
o When sorting A[6:20], use A[20] as the pivot.
o Textbook implementation does this.
• Randomly select one of the elements to be sorted as the
pivot.
o When sorting A[6:20], generate a random number r in the
range [6, 20].
o Use A[r] as the pivot.
20
Choice Of Pivot
• Median-of-Three rule - from the leftmost, middle, and
rightmost elements of the list to be sorted, select the one
with median key as the pivot.
o When sorting A[6:20], examine A[6], A[13] ((6+20)/2), and
A[20].
o Select the element with median (i.e., middle) key.
o If A[6].key = 30, A[13].key = 2, and A[20].key = 10, A[20]
becomes the pivot.
o If A[6].key = 3, A[13].key = 2, and A[20].key = 10, A[6]
becomes the pivot.
21
Choice Of Pivot
• If A[6].key = 30, A[13].key = 25, and A[20].key = 10, A[13]
becomes the pivot.
• When the pivot is picked at random or when the median-of-
three rule is used, we can use the quicksort code of the
textbook provided we first swap the rightmost element and
the chosen pivot.
22
swap
pivot
Partitioning Into Three Groups
• Sort A = [6, 2, 8, 5, 11, 10, 4, 1, 9, 7, 3].
• Leftmost element (6) is the pivot.
• When another array B is available:
o Scan A from left to right (omit the pivot in this scan),
placing elements ≤ pivot at the left end of B and the
remaining elements at the right end of B.
o The pivot is placed at the remaining position of the B.
23
Partitioning Example Using Additional Array
Sort left and right groups recursively.
24
6 2 8 5 11 10 4 1 9 7 3a
b
2 85 11104 1 973 6
In-place Partitioning
1. Find leftmost element (bigElement) > pivot.
2. Find rightmost element (smallElement) < pivot.
3. Swap bigElement and smallElement provided
bigElement is to the left of smallElement.
4. Repeat.
25
6 2 8 5 11 10 4 1 9 7 3a
6
6 2 3 5 11 10 4 1 9 7 8a
6
pivot
In-place Partitioning
26
X : pivot
0 1 n-1
swap
the first element
greater than pivot
the first element
smaller than pivot
In-Place Partitioning Example
27
6 2 8 5 11 10 4 1 9 7 3a 6 8 3
6 2 3 5 11 10 4 1 9 7 8a 6 11 1
6 2 3 5 1 10 4 11 9 7 8a 6 10 4
6 2 3 5 1 4 10 11 9 7 8a 6 104
bigElement is not to left of smallElement, terminate process.
Swap pivot and smallElement.
4 2 3 5 1 4 11 9 7 8a 6 106
Runtime of Quicksort
 Worst Case Partition:
o The running time of quicksort depends on whether the
partitioning is balanced or not.
• Best Case Partition:
o When the partitioning procedure produces two regions of
size n/2, we get the a balanced partition with best case
performance: T(n) = 2T(n/2) + Θ(n) = Θ(n lg n).
• Average Case Partition:
o Average complexity is also Θ(n lg n).
28
Best Case Partitioning
• A recursion tree for quicksort in which partition always
balances the two sides of the partition equally (the best case).
The resulting running time is Θ(n lg n).
29
Randomized Quicksort
• An algorithm is randomized if its behavior is determined not
only by the input but also by values produced by a random-
number generator.
• Exchange A[r] with an element chosen at random from A[p…r]
in Partition.
• This ensures that the pivot element is equally likely to be any
of input elements.
30
Randomized Quicksort
RANDOMIZED-PARTITION(A, p, r)
1 i = RANDOM(p, r)
2 exchange A[r] with A[i]
3 return PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, r)
1 if p < r
2 q = RANDOMIZED-PARTITION(A, p, r)
3 RANDOMIZED-QUICKSORT(A, p, q - 1)
4 RANDOMIZED-QUICKSORT(A, q + 1, r)
31
Review: Analyzing Quicksort
• What will be the worst case for the algorithm?
o Partition is always unbalanced.
• What will be the best case for the algorithm?
o Partition is balanced.
• Will any particular input elicit the worst case?
o Yes: Already-sorted input.
32
Quicksort
Dr. Ra’Fat A. AL-msie’deen
Chapter 4
Mutah University
Faculty of IT, Department of Software Engineering

More Related Content

PPTX
Algorithms - "heap sort"
PDF
Heapsort quick sort
PPT
Algorithm: priority queue
PDF
PDF
LEC4-DS ALGO.pdf
PPT
Heaps & priority queues
PPT
Algorithm
Algorithms - "heap sort"
Heapsort quick sort
Algorithm: priority queue
LEC4-DS ALGO.pdf
Heaps & priority queues
Algorithm

What's hot (20)

PPTX
Unit 4 queue
PPT
Counting Sort Lowerbound
PPTX
Counting sort
PPTX
Arrays.pptx
PDF
Scientific Computing with Python - NumPy | WeiYuan
DOCX
Best,worst,average case .17581556 045
PPT
Heaps & Adaptable priority Queues
PDF
Array linear data_structure_2 (1)
PPTX
Quick and Heap Sort with examples
PDF
Linked list
PPT
Priority queues
PPTX
Sorting
PPTX
Presentation
PPT
Arrays Data Structure
PDF
Intoduction to numpy
PPTX
Unit 7 sorting
PPTX
Array operations
PPT
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
PPTX
.Net Collection Classes Deep Dive - Rocksolid Tour 2013
PPTX
My lectures circular queue
Unit 4 queue
Counting Sort Lowerbound
Counting sort
Arrays.pptx
Scientific Computing with Python - NumPy | WeiYuan
Best,worst,average case .17581556 045
Heaps & Adaptable priority Queues
Array linear data_structure_2 (1)
Quick and Heap Sort with examples
Linked list
Priority queues
Sorting
Presentation
Arrays Data Structure
Intoduction to numpy
Unit 7 sorting
Array operations
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
.Net Collection Classes Deep Dive - Rocksolid Tour 2013
My lectures circular queue
Ad

Similar to Algorithms - "quicksort" (20)

PPT
3.8 quick sort
PPTX
CSE680-07QuickSort.pptx
PPTX
Quick sort.pptx
PPTX
quick sort by deepak.pptx
PPT
s4_quick_sort.ppt
PPTX
Introduction to Algorithms
PPTX
09 QUICK SORT Design and Analysis of algorithms
PPT
Quick sort Algorithm Discussion And Analysis
PDF
Skiena algorithm 2007 lecture08 quicksort
PPTX
Quick sort
PDF
Analysis and design of algorithms part2
PPTX
Sortings .pptx
PDF
10 algos de tri
PDF
Class13_Quicksort_Algorithm.pdf
PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
PPT
PPT
Data Structures 6
PPTX
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
PDF
Chapter 8 advanced sorting and hashing for print
3.8 quick sort
CSE680-07QuickSort.pptx
Quick sort.pptx
quick sort by deepak.pptx
s4_quick_sort.ppt
Introduction to Algorithms
09 QUICK SORT Design and Analysis of algorithms
Quick sort Algorithm Discussion And Analysis
Skiena algorithm 2007 lecture08 quicksort
Quick sort
Analysis and design of algorithms part2
Sortings .pptx
10 algos de tri
Class13_Quicksort_Algorithm.pdf
SORT AND SEARCH ARRAY WITH WITH C++.pptx
Data Structures 6
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Chapter 8 advanced sorting and hashing for print
Ad

More from Ra'Fat Al-Msie'deen (20)

PDF
Smart City: Definitions, Architectures, Development Life Cycle, Technologies,...
PDF
ScaMaha: A Tool for Parsing, Analyzing, and Visualizing Object-Oriented Softw...
PDF
ScaMaha: A Tool for Parsing, Analyzing, and Visualizing Object-Oriented Softw...
PDF
Software evolution understanding: Automatic extraction of software identifier...
PDF
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
PDF
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
PDF
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
PDF
Supporting software documentation with source code summarization
PDF
SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
PDF
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
PDF
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
PDF
Automatic Labeling of the Object-oriented Source Code: The Lotus Approach
PDF
Constructing a software requirements specification and design for electronic ...
PDF
Detecting commonality and variability in use-case diagram variants
PDF
Naming the Identified Feature Implementation Blocks from Software Source Code
PPTX
Application architectures - Software Architecture and Design
PPTX
Planning and writing your documents - Software documentation
PPTX
Requirements management planning & Requirements change management
PPTX
Requirements change - requirements engineering
PPTX
Requirements validation - requirements engineering
Smart City: Definitions, Architectures, Development Life Cycle, Technologies,...
ScaMaha: A Tool for Parsing, Analyzing, and Visualizing Object-Oriented Softw...
ScaMaha: A Tool for Parsing, Analyzing, and Visualizing Object-Oriented Softw...
Software evolution understanding: Automatic extraction of software identifier...
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
Supporting software documentation with source code summarization
SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
Automatic Labeling of the Object-oriented Source Code: The Lotus Approach
Constructing a software requirements specification and design for electronic ...
Detecting commonality and variability in use-case diagram variants
Naming the Identified Feature Implementation Blocks from Software Source Code
Application architectures - Software Architecture and Design
Planning and writing your documents - Software documentation
Requirements management planning & Requirements change management
Requirements change - requirements engineering
Requirements validation - requirements engineering

Recently uploaded (20)

PPTX
Education and Perspectives of Education.pptx
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Hazard Identification & Risk Assessment .pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
International_Financial_Reporting_Standa.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
Empowerment Technology for Senior High School Guide
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Education and Perspectives of Education.pptx
Complications of Minimal Access-Surgery.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
Hazard Identification & Risk Assessment .pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
International_Financial_Reporting_Standa.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Introduction to pro and eukaryotes and differences.pptx
B.Sc. DS Unit 2 Software Engineering.pptx
Empowerment Technology for Senior High School Guide
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)

Algorithms - "quicksort"

  • 1. Quicksort Dr. Ra’Fat A. AL-msie’deen Chapter 4 Mutah University Faculty of IT, Department of Software Engineering
  • 2. Outlines: Quicksort • Quicksort: Advantages and Disadvantages. • Quicksort: o Quicksort Function. o Partition Function. o Choice Of Pivot:  Rightmost or Leftmost.  Randomly.  Median-of-Three Rule. o Partitioning using Additional Array. o In-Place Partitioning. o Runtime of Quicksort (Worst, Best, Average). • Randomized Quicksort. 2
  • 3. Quicksort • Quicksort pros: o Sorts in place. “rearrange the elements where they are”. o Sorts θ(n lg n) in the average case. o Very efficient in practice. • Quicksort cons: o Sorts θ(n2) in the worst case. o not stable:  does not preserve the relative order of elements with equal keys.  Sorting algorithm is stable if 2 records with the same key stay in original order. o But in practice, it’s quick. o And the worst case doesn’t happen often … sorted. 3
  • 4. Quicksort • Another divide-and-conquer algorithm: • Divide: A[p…r] is partitioned (rearranged) into two nonempty subarrays A[p…q-1] and A[q+1…r] such that each element of A[p…q-1] is less than or equal to each element of A[q+1…r]. Index q is computed here, called pivot. • Conquer: two subarrays are sorted by recursive calls to quicksort. • Combine: unlike merge sort, no work needed since the subarrays are sorted in place already. 4
  • 5. Quicksort • The basic algorithm to sort an array A consists of the following four easy steps: 1. If the number of elements in A is 0 or 1, then return. 2. Pick any element v in A. This is called the pivot. 3. Partition A-{v} (the remaining elements in A) into two disjoint groups: o A1 = {x ∈ A-{v} | x ≤ v}, and o A2 = {x ∈ A-{v} | x ≥ v} 4. return: o {quicksort(A1) followed by v followed by quicksort(A2)} 5 A1 x ≤ v v A2 x ≥ v
  • 6. Quicksort • Small instance has n ≤ 1. o Every small instance is a sorted instance. • To sort a large instance: o select a pivot element from out of the n elements. • Partition the n elements into 3 groups left, middle and right. o The middle group contains only the pivot element. o All elements in the left group are ≤ pivot. o All elements in the right group are ≥ pivot. • Sort left and right groups recursively. • Answer is sorted left group, followed by middle group followed by sorted right group. 6
  • 7. Quicksort - Example Use 6 as the pivot Sort left and right groups recursively 7 6 2 8 5 11 10 4 1 9 7 3 2 5 4 1 3 6 7 9 10 11 8
  • 8. Quicksort - Code QUICKSORT(A, p, r) 1 if p < r 2 q = PARTITION(A, p, r) 3 QUICKSORT(A, p, q - 1) 4 QUICKSORT(A, q + 1, r) o To sort an entire array A, the initial call is QUICKSORT(A, 1, A:length). o Initial call is Quicksort(A, 1, n), where n is the length of A. 8
  • 9. Partition • Clearly, all the action takes place in the partition() function. o Rearranges the subarray in place. o End result:  Two subarrays.  All values in first subarray ≤ all values in second. o Returns the index of the “pivot” element separating the two subarrays. 9
  • 10. Partition - Code • Partitioning the array: The key to the algorithm is the PARTITION procedure, which rearranges the subarray A[p … r] in place. PARTITION(A, p, r) 1 x = A[r] 2 i = p - 1 3 for j = p to r - 1 4 if A[j] ≤ x 5 i = i + 1 6 exchange A[i] with A[j] 7 exchange A[i + 1] with A[r] 8 return i + 1 10
  • 11. Partition - Code • The four regions maintained by the procedure PARTITION on a subarray A[p … r]. o The values in A[p ... i] are all less than or equal to x, o the values in A[i + 1 ... j - 1] are all greater than x, o and A[r] = x. o The subarray A[j ... r - 1] can take on any values. 11
  • 12. Partition - Code  The two cases for one iteration of procedure PARTITION. 1. (a) If A[j] > x, the only action is to increment j , which maintains the loop invariant. 2. (b) If A[j] ≤ x, index i is incremented, A[i] and A[j] are swapped, and then j is incremented. Again, the loop invariant is maintained. 12
  • 13. Partition Example • A = {2, 8, 7, 1, 3, 5, 6, 4} 13
  • 14. Partition Example - Explanation • Lightly shaded elements are in the first partition with values ≤ x (pivot). • Heavily shaded elements are in the second partition with values ≥ x (pivot). • The unshaded elements have not yet been put in one of the first two partitions. • The final white element is the pivot. 14
  • 15. Partition Example - Explanation • (a) The initial array and variable settings. None of the elements have been placed in either of the first two partitions. • (b) The value 2 is “swapped with itself” and put in the partition of smaller values. 15
  • 16. Partition Example - Explanation • (c)–(d) The values 8 and 7 are added to the partition of larger values. • (e) The values 1 and 8 are swapped, and the smaller partition grows. 16
  • 17. Partition Example - Explanation • (f) The values 3 and 7 are swapped, and the smaller partition grows. • (g)–(h) The larger partition grows to include 5 and 6, and the loop terminates. 17
  • 18. Partition Example - Explanation • (i) In lines 7–8, the pivot element is swapped so that it lies between the two partitions. 18 PARTITION(A, p, r) 1 x = A[r] 2 i = p - 1 3 for j = p to r - 1 4 if A[j] ≤ x 5 i = i + 1 6 exchange A[i] with A[j] 7 exchange A[i + 1] with A[r] 8 return i + 1
  • 19. Example: On an 8-element subarray. 19[The index j disappears because it is no longer needed once the for loop is exited.]
  • 20. Choice Of Pivot • Pivot is rightmost element in list that is to be sorted. o When sorting A[6:20], use A[20] as the pivot. o Textbook implementation does this. • Randomly select one of the elements to be sorted as the pivot. o When sorting A[6:20], generate a random number r in the range [6, 20]. o Use A[r] as the pivot. 20
  • 21. Choice Of Pivot • Median-of-Three rule - from the leftmost, middle, and rightmost elements of the list to be sorted, select the one with median key as the pivot. o When sorting A[6:20], examine A[6], A[13] ((6+20)/2), and A[20]. o Select the element with median (i.e., middle) key. o If A[6].key = 30, A[13].key = 2, and A[20].key = 10, A[20] becomes the pivot. o If A[6].key = 3, A[13].key = 2, and A[20].key = 10, A[6] becomes the pivot. 21
  • 22. Choice Of Pivot • If A[6].key = 30, A[13].key = 25, and A[20].key = 10, A[13] becomes the pivot. • When the pivot is picked at random or when the median-of- three rule is used, we can use the quicksort code of the textbook provided we first swap the rightmost element and the chosen pivot. 22 swap pivot
  • 23. Partitioning Into Three Groups • Sort A = [6, 2, 8, 5, 11, 10, 4, 1, 9, 7, 3]. • Leftmost element (6) is the pivot. • When another array B is available: o Scan A from left to right (omit the pivot in this scan), placing elements ≤ pivot at the left end of B and the remaining elements at the right end of B. o The pivot is placed at the remaining position of the B. 23
  • 24. Partitioning Example Using Additional Array Sort left and right groups recursively. 24 6 2 8 5 11 10 4 1 9 7 3a b 2 85 11104 1 973 6
  • 25. In-place Partitioning 1. Find leftmost element (bigElement) > pivot. 2. Find rightmost element (smallElement) < pivot. 3. Swap bigElement and smallElement provided bigElement is to the left of smallElement. 4. Repeat. 25 6 2 8 5 11 10 4 1 9 7 3a 6 6 2 3 5 11 10 4 1 9 7 8a 6 pivot
  • 26. In-place Partitioning 26 X : pivot 0 1 n-1 swap the first element greater than pivot the first element smaller than pivot
  • 27. In-Place Partitioning Example 27 6 2 8 5 11 10 4 1 9 7 3a 6 8 3 6 2 3 5 11 10 4 1 9 7 8a 6 11 1 6 2 3 5 1 10 4 11 9 7 8a 6 10 4 6 2 3 5 1 4 10 11 9 7 8a 6 104 bigElement is not to left of smallElement, terminate process. Swap pivot and smallElement. 4 2 3 5 1 4 11 9 7 8a 6 106
  • 28. Runtime of Quicksort  Worst Case Partition: o The running time of quicksort depends on whether the partitioning is balanced or not. • Best Case Partition: o When the partitioning procedure produces two regions of size n/2, we get the a balanced partition with best case performance: T(n) = 2T(n/2) + Θ(n) = Θ(n lg n). • Average Case Partition: o Average complexity is also Θ(n lg n). 28
  • 29. Best Case Partitioning • A recursion tree for quicksort in which partition always balances the two sides of the partition equally (the best case). The resulting running time is Θ(n lg n). 29
  • 30. Randomized Quicksort • An algorithm is randomized if its behavior is determined not only by the input but also by values produced by a random- number generator. • Exchange A[r] with an element chosen at random from A[p…r] in Partition. • This ensures that the pivot element is equally likely to be any of input elements. 30
  • 31. Randomized Quicksort RANDOMIZED-PARTITION(A, p, r) 1 i = RANDOM(p, r) 2 exchange A[r] with A[i] 3 return PARTITION(A, p, r) RANDOMIZED-QUICKSORT(A, p, r) 1 if p < r 2 q = RANDOMIZED-PARTITION(A, p, r) 3 RANDOMIZED-QUICKSORT(A, p, q - 1) 4 RANDOMIZED-QUICKSORT(A, q + 1, r) 31
  • 32. Review: Analyzing Quicksort • What will be the worst case for the algorithm? o Partition is always unbalanced. • What will be the best case for the algorithm? o Partition is balanced. • Will any particular input elicit the worst case? o Yes: Already-sorted input. 32
  • 33. Quicksort Dr. Ra’Fat A. AL-msie’deen Chapter 4 Mutah University Faculty of IT, Department of Software Engineering