SlideShare a Scribd company logo
1 of 38
David Luebke 1 8/29/2022
CS 332: Algorithms
Quicksort
David Luebke 2 8/29/2022
Homework 2
 Assigned today, due next Wednesday
 Will be on web page shortly after class
 Go over now
David Luebke 3 8/29/2022
Review: Quicksort
 Sorts in place
 Sorts O(n lg n) in the average case
 Sorts O(n2) in the worst case
 But in practice, it’s quick
 And the worst case doesn’t happen often (but more
on this later…)
David Luebke 4 8/29/2022
Quicksort
 Another divide-and-conquer algorithm
 The array A[p..r] is partitioned into two non-empty
subarrays A[p..q] and A[q+1..r]
 Invariant: All elements in A[p..q] are less than all
elements in A[q+1..r]
 The subarrays are recursively sorted by calls to
quicksort
 Unlike merge sort, no combining step: two
subarrays form an already-sorted array
David Luebke 5 8/29/2022
Quicksort Code
Quicksort(A, p, r)
{
if (p < r)
{
q = Partition(A, p, r);
Quicksort(A, p, q);
Quicksort(A, q+1, r);
}
}
David Luebke 6 8/29/2022
Partition
 Clearly, all the action takes place in the
partition() function
 Rearranges the subarray in place
 End result:
 Two subarrays
 All values in first subarray  all values in second
 Returns the index of the “pivot” element
separating the two subarrays
 How do you suppose we implement this?
David Luebke 7 8/29/2022
Partition In Words
 Partition(A, p, r):
 Select an element to act as the “pivot” (which?)
 Grow two regions, A[p..i] and A[j..r]
 All elements in A[p..i] <= pivot
 All elements in A[j..r] >= pivot
 Increment i until A[i] >= pivot
 Decrement j until A[j] <= pivot
 Swap A[i] and A[j]
 Repeat until i >= j
 Return j
Note: slightly different from
book’s partition()
David Luebke 8 8/29/2022
Partition Code
Partition(A, p, r)
x = A[p];
i = p - 1;
j = r + 1;
while (TRUE)
repeat
j--;
until A[j] <= x;
repeat
i++;
until A[i] >= x;
if (i < j)
Swap(A, i, j);
else
return j;
Illustrate on
A = {5, 3, 2, 6, 4, 1, 3, 7};
What is the running time of
partition()?
David Luebke 9 8/29/2022
Partition Code
Partition(A, p, r)
x = A[p];
i = p - 1;
j = r + 1;
while (TRUE)
repeat
j--;
until A[j] <= x;
repeat
i++;
until A[i] >= x;
if (i < j)
Swap(A, i, j);
else
return j;
partition() runs in O(n) time
David Luebke 10 8/29/2022
Analyzing Quicksort
 What will be the worst case for the algorithm?
 Partition is always unbalanced
 What will be the best case for the algorithm?
 Partition is perfectly balanced
 Which is more likely?
 The latter, by far, except...
 Will any particular input elicit the worst case?
 Yes: Already-sorted input
David Luebke 11 8/29/2022
Analyzing Quicksort
 In the worst case:
T(1) = (1)
T(n) = T(n - 1) + (n)
 Works out to
T(n) = (n2)
David Luebke 12 8/29/2022
Analyzing Quicksort
 In the best case:
T(n) = 2T(n/2) + (n)
 What does this work out to?
T(n) = (n lg n)
David Luebke 13 8/29/2022
Improving Quicksort
 The real liability of quicksort is that it runs in
O(n2) on already-sorted input
 Book discusses two solutions:
 Randomize the input array, OR
 Pick a random pivot element
 How will these solve the problem?
 By insuring that no particular input can be chosen
to make quicksort run in O(n2) time
David Luebke 14 8/29/2022
Analyzing Quicksort: Average Case
 Assuming random input, average-case running
time is much closer to O(n lg n) than O(n2)
 First, a more intuitive explanation/example:
 Suppose that partition() always produces a 9-to-1
split. This looks quite unbalanced!
 The recurrence is thus:
T(n) = T(9n/10) + T(n/10) + n
 How deep will the recursion go? (draw it)
Use n instead of O(n)
for convenience (how?)
David Luebke 15 8/29/2022
Analyzing Quicksort: Average Case
 Intuitively, a real-life run of quicksort will
produce a mix of “bad” and “good” splits
 Randomly distributed among the recursion tree
 Pretend for intuition that they alternate between
best-case (n/2 : n/2) and worst-case (n-1 : 1)
 What happens if we bad-split root node, then good-
split the resulting size (n-1) node?
David Luebke 16 8/29/2022
Analyzing Quicksort: Average Case
 Intuitively, a real-life run of quicksort will
produce a mix of “bad” and “good” splits
 Randomly distributed among the recursion tree
 Pretend for intuition that they alternate between
best-case (n/2 : n/2) and worst-case (n-1 : 1)
 What happens if we bad-split root node, then good-
split the resulting size (n-1) node?
 We fail English
David Luebke 17 8/29/2022
Analyzing Quicksort: Average Case
 Intuitively, a real-life run of quicksort will
produce a mix of “bad” and “good” splits
 Randomly distributed among the recursion tree
 Pretend for intuition that they alternate between
best-case (n/2 : n/2) and worst-case (n-1 : 1)
 What happens if we bad-split root node, then
good-split the resulting size (n-1) node?
 We end up with three subarrays, size 1, (n-1)/2, (n-1)/2
 Combined cost of splits = n + n -1 = 2n -1 = O(n)
 No worse than if we had good-split the root node!
David Luebke 18 8/29/2022
Analyzing Quicksort: Average Case
 Intuitively, the O(n) cost of a bad split
(or 2 or 3 bad splits) can be absorbed
into the O(n) cost of each good split
 Thus running time of alternating bad and good
splits is still O(n lg n), with slightly higher
constants
 How can we be more rigorous?
David Luebke 19 8/29/2022
Analyzing Quicksort: Average Case
 For simplicity, assume:
 All inputs distinct (no repeats)
 Slightly different partition() procedure
 partition around a random element, which is not
included in subarrays
 all splits (0:n-1, 1:n-2, 2:n-3, … , n-1:0) equally likely
 What is the probability of a particular split
happening?
 Answer: 1/n
David Luebke 20 8/29/2022
Analyzing Quicksort: Average Case
 So partition generates splits
(0:n-1, 1:n-2, 2:n-3, … , n-2:1, n-1:0)
each with probability 1/n
 If T(n) is the expected running time,
 What is each term under the summation for?
 What is the (n) term for?
     
   









1
0
1
1 n
k
n
k
n
T
k
T
n
n
T
David Luebke 21 8/29/2022
Analyzing Quicksort: Average Case
 So…
 Note: this is just like the book’s recurrence (p166),
except that the summation starts with k=0
 We’ll take care of that in a second
     
   
   















1
0
1
0
2
1
1
n
k
n
k
n
k
T
n
n
k
n
T
k
T
n
n
T
Write it on
the board
David Luebke 22 8/29/2022
Analyzing Quicksort: Average Case
 We can solve this recurrence using the dreaded
substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 Substitute it in for some value < n
 Prove that it follows for n
David Luebke 23 8/29/2022
Analyzing Quicksort: Average Case
 We can solve this recurrence using the dreaded
substitution method
 Guess the answer
 What’s the answer?
 Assume that the inductive hypothesis holds
 Substitute it in for some value < n
 Prove that it follows for n
David Luebke 24 8/29/2022
Analyzing Quicksort: Average Case
 We can solve this recurrence using the dreaded
substitution method
 Guess the answer
 T(n) = O(n lg n)
 Assume that the inductive hypothesis holds
 Substitute it in for some value < n
 Prove that it follows for n
David Luebke 25 8/29/2022
Analyzing Quicksort: Average Case
 We can solve this recurrence using the dreaded
substitution method
 Guess the answer
 T(n) = O(n lg n)
 Assume that the inductive hypothesis holds
 What’s the inductive hypothesis?
 Substitute it in for some value < n
 Prove that it follows for n
David Luebke 26 8/29/2022
Analyzing Quicksort: Average Case
 We can solve this recurrence using the dreaded
substitution method
 Guess the answer
 T(n) = O(n lg n)
 Assume that the inductive hypothesis holds
 T(n)  an lg n + b for some constants a and b
 Substitute it in for some value < n
 Prove that it follows for n
David Luebke 27 8/29/2022
Analyzing Quicksort: Average Case
 We can solve this recurrence using the dreaded
substitution method
 Guess the answer
 T(n) = O(n lg n)
 Assume that the inductive hypothesis holds
 T(n)  an lg n + b for some constants a and b
 Substitute it in for some value < n
 What value?
 Prove that it follows for n
David Luebke 28 8/29/2022
Analyzing Quicksort: Average Case
 We can solve this recurrence using the dreaded
substitution method
 Guess the answer
 T(n) = O(n lg n)
 Assume that the inductive hypothesis holds
 T(n)  an lg n + b for some constants a and b
 Substitute it in for some value < n
 The value k in the recurrence
 Prove that it follows for n
David Luebke 29 8/29/2022
Analyzing Quicksort: Average Case
 We can solve this recurrence using the dreaded
substitution method
 Guess the answer
 T(n) = O(n lg n)
 Assume that the inductive hypothesis holds
 T(n)  an lg n + b for some constants a and b
 Substitute it in for some value < n
 The value k in the recurrence
 Prove that it follows for n
 Grind through it…
David Luebke 30 8/29/2022
Note: leaving the same
recurrence as the book
What are we doing here?
Analyzing Quicksort: Average Case
     
   
   
   
   










































1
1
1
1
1
1
1
0
1
0
lg
2
2
lg
2
lg
2
lg
2
2
n
k
n
k
n
k
n
k
n
k
n
b
k
ak
n
n
n
b
b
k
ak
n
n
b
k
ak
b
n
n
b
k
ak
n
n
k
T
n
n
T The recurrence to be solved
What are we doing here?
What are we doing here?
Plug in inductive hypothesis
Expand out the k=0 case
2b/n is just a constant,
so fold it into (n)
David Luebke 31 8/29/2022
What are we doing here?
What are we doing here?
Evaluate the summation:
b+b+…+b = b (n-1)
The recurrence to be solved
Since n-1<n, 2b(n-1)/n < 2b
Analyzing Quicksort: Average Case
     
 
 
 
n
b
k
k
n
a
n
n
n
b
k
k
n
a
n
b
n
k
ak
n
n
b
k
ak
n
n
T
n
k
n
k
n
k
n
k
n
k
































2
lg
2
)
1
(
2
lg
2
2
lg
2
lg
2
1
1
1
1
1
1
1
1
1
1
What are we doing here?
Distribute the summation
This summation gets its own set of slides later
David Luebke 32 8/29/2022
How did we do this?
Pick a large enough that
an/4 dominates (n)+b
What are we doing here?
Remember, our goal is to get
T(n)  an lg n + b
What the hell?
We’ll prove this later
What are we doing here?
Distribute the (2a/n) term
The recurrence to be solved
Analyzing Quicksort: Average Case
   
 
 
 
b
n
an
n
a
b
n
b
n
an
n
b
n
a
n
an
n
b
n
n
n
n
a
n
b
k
k
n
a
n
T
n
k

































 


lg
4
lg
2
4
lg
2
8
1
lg
2
1
2
2
lg
2
2
2
1
1
David Luebke 33 8/29/2022
Analyzing Quicksort: Average Case
 So T(n)  an lg n + b for certain a and b
 Thus the induction holds
 Thus T(n) = O(n lg n)
 Thus quicksort runs in O(n lg n) time on average
(phew!)
 Oh yeah, the summation…
David Luebke 34 8/29/2022
What are we doing here?
The lg k in the second term
is bounded by lg n
Tightly Bounding
The Key Summation
 
 
 
 
 
 



























1
2
1
2
1
1
2
1
2
1
1
2
1
2
1
1
1
lg
lg
lg
lg
lg
lg
lg
n
n
k
n
k
n
n
k
n
k
n
n
k
n
k
n
k
k
n
k
k
n
k
k
k
k
k
k
k
k
k
What are we doing here?
Move the lg n outside the
summation
What are we doing here?
Split the summation for a
tighter bound
David Luebke 35 8/29/2022
The summation bound so far
Tightly Bounding
The Key Summation
 
 
 
 
 
 
 
 
 
 
 





































1
2
1
2
1
1
2
1
2
1
1
2
1
2
1
1
2
1
2
1
1
1
lg
1
lg
lg
1
lg
lg
2
lg
lg
lg
lg
n
n
k
n
k
n
n
k
n
k
n
n
k
n
k
n
n
k
n
k
n
k
k
n
k
n
k
n
n
k
k
n
n
k
k
n
k
k
k
k
What are we doing here?
The lg k in the first term is
bounded by lg n/2
What are we doing here?
lg n/2 = lg n - 1
What are we doing here?
Move (lg n - 1) outside the
summation
David Luebke 36 8/29/2022
The summation bound so far
Tightly Bounding
The Key Summation
 
 
 
   
 
 
   

































 









1
2
1
1
2
1
1
1
1
2
1
2
1
1
2
1
1
2
1
2
1
1
1
2
)
(
1
lg
lg
lg
lg
lg
1
lg
lg
n
k
n
k
n
k
n
n
k
n
k
n
k
n
n
k
n
k
n
k
k
n
n
n
k
k
n
k
n
k
k
n
k
n
k
n
k
k
What are we doing here?
Distribute the (lg n - 1)
What are we doing here?
The summations overlap in
range; combine them
What are we doing here?
The Guassian series
David Luebke 37 8/29/2022
The summation bound so far
Tightly Bounding
The Key Summation
   
 
 
 
 
  4
8
1
lg
lg
2
1
1
2
2
2
1
lg
1
2
1
lg
1
2
1
lg
2
)
(
1
lg
2
2
1
2
1
1
2
1
1
1
n
n
n
n
n
n
n
n
n
n
n
k
n
n
n
k
n
n
n
k
k
n
k
n
k
n
k





























 










What are we doing here?
Rearrange first term, place
upper bound on second
What are we doing?
X Guassian series
What are we doing?
Multiply it
all out
David Luebke 38 8/29/2022
Tightly Bounding
The Key Summation
 
!
!
Done!
2
when
8
1
lg
2
1
4
8
1
lg
lg
2
1
lg
2
2
2
2
1
1










n
n
n
n
n
n
n
n
n
n
k
k
n
k

More Related Content

Similar to lecture7.ppt

Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
 
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVEChapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVEnszakir
 
1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the to1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the tofariyaPatel
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
Practical and Worst-Case Efficient Apportionment
Practical and Worst-Case Efficient ApportionmentPractical and Worst-Case Efficient Apportionment
Practical and Worst-Case Efficient ApportionmentRaphael Reitzig
 
Some history of quantum groups
Some history of quantum groupsSome history of quantum groups
Some history of quantum groupsDaniel Tubbenhauer
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutionssubhashchandra197
 
Stochastic modelling and quasi-random numbers
Stochastic modelling and quasi-random numbersStochastic modelling and quasi-random numbers
Stochastic modelling and quasi-random numbersOlivier Teytaud
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingzukun
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 

Similar to lecture7.ppt (20)

Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVEChapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
 
1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the to1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the to
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Practical and Worst-Case Efficient Apportionment
Practical and Worst-Case Efficient ApportionmentPractical and Worst-Case Efficient Apportionment
Practical and Worst-Case Efficient Apportionment
 
Some history of quantum groups
Some history of quantum groupsSome history of quantum groups
Some history of quantum groups
 
Lec10
Lec10Lec10
Lec10
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Stochastic Process Exam Help
Stochastic Process Exam HelpStochastic Process Exam Help
Stochastic Process Exam Help
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Stochastic modelling and quasi-random numbers
Stochastic modelling and quasi-random numbersStochastic modelling and quasi-random numbers
Stochastic modelling and quasi-random numbers
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Stochastic Process Assignment Help
Stochastic Process Assignment HelpStochastic Process Assignment Help
Stochastic Process Assignment Help
 
Lesson 7: The Derivative
Lesson 7: The DerivativeLesson 7: The Derivative
Lesson 7: The Derivative
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracing
 
Crt
CrtCrt
Crt
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
03 dc
03 dc03 dc
03 dc
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 

Recently uploaded

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 

Recently uploaded (20)

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 

lecture7.ppt

  • 1. David Luebke 1 8/29/2022 CS 332: Algorithms Quicksort
  • 2. David Luebke 2 8/29/2022 Homework 2  Assigned today, due next Wednesday  Will be on web page shortly after class  Go over now
  • 3. David Luebke 3 8/29/2022 Review: Quicksort  Sorts in place  Sorts O(n lg n) in the average case  Sorts O(n2) in the worst case  But in practice, it’s quick  And the worst case doesn’t happen often (but more on this later…)
  • 4. David Luebke 4 8/29/2022 Quicksort  Another divide-and-conquer algorithm  The array A[p..r] is partitioned into two non-empty subarrays A[p..q] and A[q+1..r]  Invariant: All elements in A[p..q] are less than all elements in A[q+1..r]  The subarrays are recursively sorted by calls to quicksort  Unlike merge sort, no combining step: two subarrays form an already-sorted array
  • 5. David Luebke 5 8/29/2022 Quicksort Code Quicksort(A, p, r) { if (p < r) { q = Partition(A, p, r); Quicksort(A, p, q); Quicksort(A, q+1, r); } }
  • 6. David Luebke 6 8/29/2022 Partition  Clearly, all the action takes place in the partition() function  Rearranges the subarray in place  End result:  Two subarrays  All values in first subarray  all values in second  Returns the index of the “pivot” element separating the two subarrays  How do you suppose we implement this?
  • 7. David Luebke 7 8/29/2022 Partition In Words  Partition(A, p, r):  Select an element to act as the “pivot” (which?)  Grow two regions, A[p..i] and A[j..r]  All elements in A[p..i] <= pivot  All elements in A[j..r] >= pivot  Increment i until A[i] >= pivot  Decrement j until A[j] <= pivot  Swap A[i] and A[j]  Repeat until i >= j  Return j Note: slightly different from book’s partition()
  • 8. David Luebke 8 8/29/2022 Partition Code Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; Illustrate on A = {5, 3, 2, 6, 4, 1, 3, 7}; What is the running time of partition()?
  • 9. David Luebke 9 8/29/2022 Partition Code Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; partition() runs in O(n) time
  • 10. David Luebke 10 8/29/2022 Analyzing Quicksort  What will be the worst case for the algorithm?  Partition is always unbalanced  What will be the best case for the algorithm?  Partition is perfectly balanced  Which is more likely?  The latter, by far, except...  Will any particular input elicit the worst case?  Yes: Already-sorted input
  • 11. David Luebke 11 8/29/2022 Analyzing Quicksort  In the worst case: T(1) = (1) T(n) = T(n - 1) + (n)  Works out to T(n) = (n2)
  • 12. David Luebke 12 8/29/2022 Analyzing Quicksort  In the best case: T(n) = 2T(n/2) + (n)  What does this work out to? T(n) = (n lg n)
  • 13. David Luebke 13 8/29/2022 Improving Quicksort  The real liability of quicksort is that it runs in O(n2) on already-sorted input  Book discusses two solutions:  Randomize the input array, OR  Pick a random pivot element  How will these solve the problem?  By insuring that no particular input can be chosen to make quicksort run in O(n2) time
  • 14. David Luebke 14 8/29/2022 Analyzing Quicksort: Average Case  Assuming random input, average-case running time is much closer to O(n lg n) than O(n2)  First, a more intuitive explanation/example:  Suppose that partition() always produces a 9-to-1 split. This looks quite unbalanced!  The recurrence is thus: T(n) = T(9n/10) + T(n/10) + n  How deep will the recursion go? (draw it) Use n instead of O(n) for convenience (how?)
  • 15. David Luebke 15 8/29/2022 Analyzing Quicksort: Average Case  Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits  Randomly distributed among the recursion tree  Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1)  What happens if we bad-split root node, then good- split the resulting size (n-1) node?
  • 16. David Luebke 16 8/29/2022 Analyzing Quicksort: Average Case  Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits  Randomly distributed among the recursion tree  Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1)  What happens if we bad-split root node, then good- split the resulting size (n-1) node?  We fail English
  • 17. David Luebke 17 8/29/2022 Analyzing Quicksort: Average Case  Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits  Randomly distributed among the recursion tree  Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1)  What happens if we bad-split root node, then good-split the resulting size (n-1) node?  We end up with three subarrays, size 1, (n-1)/2, (n-1)/2  Combined cost of splits = n + n -1 = 2n -1 = O(n)  No worse than if we had good-split the root node!
  • 18. David Luebke 18 8/29/2022 Analyzing Quicksort: Average Case  Intuitively, the O(n) cost of a bad split (or 2 or 3 bad splits) can be absorbed into the O(n) cost of each good split  Thus running time of alternating bad and good splits is still O(n lg n), with slightly higher constants  How can we be more rigorous?
  • 19. David Luebke 19 8/29/2022 Analyzing Quicksort: Average Case  For simplicity, assume:  All inputs distinct (no repeats)  Slightly different partition() procedure  partition around a random element, which is not included in subarrays  all splits (0:n-1, 1:n-2, 2:n-3, … , n-1:0) equally likely  What is the probability of a particular split happening?  Answer: 1/n
  • 20. David Luebke 20 8/29/2022 Analyzing Quicksort: Average Case  So partition generates splits (0:n-1, 1:n-2, 2:n-3, … , n-2:1, n-1:0) each with probability 1/n  If T(n) is the expected running time,  What is each term under the summation for?  What is the (n) term for?                    1 0 1 1 n k n k n T k T n n T
  • 21. David Luebke 21 8/29/2022 Analyzing Quicksort: Average Case  So…  Note: this is just like the book’s recurrence (p166), except that the summation starts with k=0  We’ll take care of that in a second                              1 0 1 0 2 1 1 n k n k n k T n n k n T k T n n T Write it on the board
  • 22. David Luebke 22 8/29/2022 Analyzing Quicksort: Average Case  We can solve this recurrence using the dreaded substitution method  Guess the answer  Assume that the inductive hypothesis holds  Substitute it in for some value < n  Prove that it follows for n
  • 23. David Luebke 23 8/29/2022 Analyzing Quicksort: Average Case  We can solve this recurrence using the dreaded substitution method  Guess the answer  What’s the answer?  Assume that the inductive hypothesis holds  Substitute it in for some value < n  Prove that it follows for n
  • 24. David Luebke 24 8/29/2022 Analyzing Quicksort: Average Case  We can solve this recurrence using the dreaded substitution method  Guess the answer  T(n) = O(n lg n)  Assume that the inductive hypothesis holds  Substitute it in for some value < n  Prove that it follows for n
  • 25. David Luebke 25 8/29/2022 Analyzing Quicksort: Average Case  We can solve this recurrence using the dreaded substitution method  Guess the answer  T(n) = O(n lg n)  Assume that the inductive hypothesis holds  What’s the inductive hypothesis?  Substitute it in for some value < n  Prove that it follows for n
  • 26. David Luebke 26 8/29/2022 Analyzing Quicksort: Average Case  We can solve this recurrence using the dreaded substitution method  Guess the answer  T(n) = O(n lg n)  Assume that the inductive hypothesis holds  T(n)  an lg n + b for some constants a and b  Substitute it in for some value < n  Prove that it follows for n
  • 27. David Luebke 27 8/29/2022 Analyzing Quicksort: Average Case  We can solve this recurrence using the dreaded substitution method  Guess the answer  T(n) = O(n lg n)  Assume that the inductive hypothesis holds  T(n)  an lg n + b for some constants a and b  Substitute it in for some value < n  What value?  Prove that it follows for n
  • 28. David Luebke 28 8/29/2022 Analyzing Quicksort: Average Case  We can solve this recurrence using the dreaded substitution method  Guess the answer  T(n) = O(n lg n)  Assume that the inductive hypothesis holds  T(n)  an lg n + b for some constants a and b  Substitute it in for some value < n  The value k in the recurrence  Prove that it follows for n
  • 29. David Luebke 29 8/29/2022 Analyzing Quicksort: Average Case  We can solve this recurrence using the dreaded substitution method  Guess the answer  T(n) = O(n lg n)  Assume that the inductive hypothesis holds  T(n)  an lg n + b for some constants a and b  Substitute it in for some value < n  The value k in the recurrence  Prove that it follows for n  Grind through it…
  • 30. David Luebke 30 8/29/2022 Note: leaving the same recurrence as the book What are we doing here? Analyzing Quicksort: Average Case                                                                 1 1 1 1 1 1 1 0 1 0 lg 2 2 lg 2 lg 2 lg 2 2 n k n k n k n k n k n b k ak n n n b b k ak n n b k ak b n n b k ak n n k T n n T The recurrence to be solved What are we doing here? What are we doing here? Plug in inductive hypothesis Expand out the k=0 case 2b/n is just a constant, so fold it into (n)
  • 31. David Luebke 31 8/29/2022 What are we doing here? What are we doing here? Evaluate the summation: b+b+…+b = b (n-1) The recurrence to be solved Since n-1<n, 2b(n-1)/n < 2b Analyzing Quicksort: Average Case             n b k k n a n n n b k k n a n b n k ak n n b k ak n n T n k n k n k n k n k                                 2 lg 2 ) 1 ( 2 lg 2 2 lg 2 lg 2 1 1 1 1 1 1 1 1 1 1 What are we doing here? Distribute the summation This summation gets its own set of slides later
  • 32. David Luebke 32 8/29/2022 How did we do this? Pick a large enough that an/4 dominates (n)+b What are we doing here? Remember, our goal is to get T(n)  an lg n + b What the hell? We’ll prove this later What are we doing here? Distribute the (2a/n) term The recurrence to be solved Analyzing Quicksort: Average Case           b n an n a b n b n an n b n a n an n b n n n n a n b k k n a n T n k                                      lg 4 lg 2 4 lg 2 8 1 lg 2 1 2 2 lg 2 2 2 1 1
  • 33. David Luebke 33 8/29/2022 Analyzing Quicksort: Average Case  So T(n)  an lg n + b for certain a and b  Thus the induction holds  Thus T(n) = O(n lg n)  Thus quicksort runs in O(n lg n) time on average (phew!)  Oh yeah, the summation…
  • 34. David Luebke 34 8/29/2022 What are we doing here? The lg k in the second term is bounded by lg n Tightly Bounding The Key Summation                                        1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 1 lg lg lg lg lg lg lg n n k n k n n k n k n n k n k n k k n k k n k k k k k k k k k What are we doing here? Move the lg n outside the summation What are we doing here? Split the summation for a tighter bound
  • 35. David Luebke 35 8/29/2022 The summation bound so far Tightly Bounding The Key Summation                                                            1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 1 lg 1 lg lg 1 lg lg 2 lg lg lg lg n n k n k n n k n k n n k n k n n k n k n k k n k n k n n k k n n k k n k k k k What are we doing here? The lg k in the first term is bounded by lg n/2 What are we doing here? lg n/2 = lg n - 1 What are we doing here? Move (lg n - 1) outside the summation
  • 36. David Luebke 36 8/29/2022 The summation bound so far Tightly Bounding The Key Summation                                                               1 2 1 1 2 1 1 1 1 2 1 2 1 1 2 1 1 2 1 2 1 1 1 2 ) ( 1 lg lg lg lg lg 1 lg lg n k n k n k n n k n k n k n n k n k n k k n n n k k n k n k k n k n k n k k What are we doing here? Distribute the (lg n - 1) What are we doing here? The summations overlap in range; combine them What are we doing here? The Guassian series
  • 37. David Luebke 37 8/29/2022 The summation bound so far Tightly Bounding The Key Summation               4 8 1 lg lg 2 1 1 2 2 2 1 lg 1 2 1 lg 1 2 1 lg 2 ) ( 1 lg 2 2 1 2 1 1 2 1 1 1 n n n n n n n n n n n k n n n k n n n k k n k n k n k                                          What are we doing here? Rearrange first term, place upper bound on second What are we doing? X Guassian series What are we doing? Multiply it all out
  • 38. David Luebke 38 8/29/2022 Tightly Bounding The Key Summation   ! ! Done! 2 when 8 1 lg 2 1 4 8 1 lg lg 2 1 lg 2 2 2 2 1 1           n n n n n n n n n n k k n k