0% found this document useful (0 votes)
10 views1 page

Lecture 8 Notes

Uploaded by

wilkinsonsm4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Lecture 8 Notes

Uploaded by

wilkinsonsm4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Quick Sort

Need to create two piles in which none of the latter pile are smaller than the former pile.
Do this by finding the median of all the numbers in the set.
Asking is x less than or greater than the median.
If less: go to pile 1
Else: go to pile 2
Then we recursively sort pile 1
Then we recursively sort pile 2
Then we simply concatenate the two piles which are now in order!

Cons:
In the worst case for my pivot, the biggest or the smallest number in the set will be the pivot and it will
do “n^2” bits of work

The absolute worst for the algorithm is when it's sorting a list that is already sorted. It will not know
this and it will plod along on its way towards returning the sorted numbers it is programmed to return.

How to find the Pivot.


It hinges on finding one as close to the median as possible

You can brute force determine the median → runs slowly in practice.
You can take a sample of the numbers and calculate their median → better
Take a median of three numbers → the beginning number, the end, and the middle
If you are given an already sorted algorithm, the median is very close and so it runs fast.
His favorite way: Prefaces by saying how quick sort is great and only runs poorly in a few specific
cases. So how can we hedge against those few circumstances (i.e., how can we reduce the likelihood of
being given already sorted data???) We can add an algorithm in front of our sorting algorithm that
randomizes all the numbers we are given..... afterwards, you merely pick the pivot at random and it
works out very well.

How To Quick Sort


40:45 – 42:20

Finding the Pivot Point in Quick Sort


42:33 – 46:00

What is the complexity of Quick Sort?


46:00 – 48:07

Quick Sort vs Merge Sort

You might also like