Lecture 8 Notes
Lecture 8 Notes
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.
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.