Algorithms and OOD (CSC 207 2014F) : EBoards

CSC207.01 2014F, Class 29: Quicksort

• Same lab partners.

Overview

• Preliminaries.
• Upcoming Work.
• Extra Credit.
• Efficiency in insertion sort.
• Questions.
• A quick introduction to Quicksort.
• Key ideas from Quicksort.
• Lab.

Preliminaries

• Office hours
• No office hours next week.
• Have an awesome break. I hope you have a chance to rest and relax.
• SEPC says: "Homework in Sam's 207 requires more knowledge than is available in the readings, so folks have to look elsewhere."
• Why aren't you asking for the extra credit when you find something useful? (Or do you never find anything useful.)

Upcoming Work

• Exam 1 hardcopy due now.
• Required epilogue due tonight.
• Reading for Monday after break:
• JavaFX (will probably appear at about midnight on October 26).

Extra Credit

• CS Table Today (Day PDR): Debrief on GHC

Peer Support

• Ajuna's Radio show Mondays at 8pm. Listen to African music
• Charlie's Friday Night "War in Animated Film" ExCo.
• Cook for ISO Food Bazaar. (You need to submit recipes in order to do this.)
• Eat at ISO Food Bazaar.
• Football at Saturday.

Recap

• Two O(n^2) sorting algorithms
• Insertion sort - Partition array into sorted/unsorted, repeatedly insert the first thing from unsorted into sorted.
• Selection sort - Partition array into sorted/unsorted + everything in sorted is less than or equal to everything in unsorted; swap smallest unsorted thing to front
• Detour: How you implement insertion sort may matter
• If you swap, rather than just shift and insert, you're probbably doing three times as much work as you need to.
• Sam's version took SEVEN TIMES as long as a carefully implemented one
• These are slow (quadratic), can we do faster?
• Yes! Merge sort
• Split the array in half, sort each half (recursively), and mash together
• How do we split the array in half? Right down the middle
• How do we mash the sorted halves together? We iterate through the two halves, repeatedly grabbing the smallest.
• Requires extra space! (At least an array of size n.)
• Can we do other efficient sorts using divide and conquer?
• Without using extra space?
• Another way to split: Rearrange so that we have "smalL" elements on the left and large on the right.
• Recurse on each half.
• And "mash together" is done.
• Finding the median is hard. So guess!
• If we pick badly, we have an O(n^2) algorithm
• If we pick well, we have an O(nlogn) algorithm
• This is called Quicksort
• Can we do better than O(nlogn)?
• Bucket sort is an O(n) algorithm, requires that you have only a small number of different values
• Some highly-tuned mergesort algorithms seem to be among the fastest O(nlogn) and only O(n) extra space.
• Heap sort is O(nlogn) and does not need more than a little extra space