Professor Abdul-Quader
Sorting
Finish presentations today
Sorting problem: given an array of integers, re-arrange the elements to be in increasing order.
CS2:
Remember these?
Recursive algorithm:
procedure mergesort(array, tmpArray, left, right):
if (left >= right) return
mid = (left + right) / 2
mergesort(array, tmpArray, left, mid)
mergesort(array, tmpArray, mid + 1, right)
merge(array, tmpArray, left, mid + 1, right)
How do we implement the merge
algorithm? (This is why we have a tmpArray
)
A sorting algorithm is called stable if whenever multiple elements of the input array are equal, they are left in their original order. Is the merge sort algorithm stable?
Why is “stability” important? Suppose we have a list of Employees. Each employee has a startDate and a job title.
sortByStartDate(employees)
sortByJobTitle(employees)
What does the employees list look like after this?
Recall: we have seen other sorting algorithms which use data structures.
Are these algorithms stable?
Video:
Pick a pivot. Partition the array: the left part should only contain elements smaller than the pivot. Then you know exactly where the pivot should be, and then recursively quicksort the left and right parts.
procedure quicksort(list, start, end):
if (start >= end) return
pivot = list[end]
pivotIndex = partition(list, start, end, pivot)
quicksort(list, start, pivotIndex)
quicksort(list, pivotIndex + 1, end)
Let’s write pseudocode for partition together.
(Stability / space / worst case)
Suppose we have \(16\) GB of data to sort on disk. We can store, in memory, \(4\) GB of data at a time. How can we sort all of this data? In particular, we cannot possibly hope to keep it all in memory, so the goal is to store the data on disk in sorted order.
Due April 21 (two weeks). Three parts:
Given (as input) a list of course rosters (each line is list of student IDs), form a graph.
Output a schedule for the courses (assign each course a time slot).
Getting started: