Merge Sort: Biostatistics 615/815
Merge Sort: Biostatistics 615/815
Biostatistics 615/815
Lecture 9
Project Assignments
z Project assignments sent out by e-mail
z Review Session
• I propose Thursday, October 19
z Mid-term Exam
• I propose Tuesday, October 24
Last Lecture: Quick Sort
z Choose a partitioning element …
z Optimizations
• Choice of median
• Threshold for brute-force methods
• Limiting depth of recursion
while (true)
{
while (isLess(a[up], part))
up++;
while (isLess(part, a[down]) && (up < down))
down--;
Exchange(a[up], a[stop]);
return up;
}
Non-Recursive Quick Sort
void quicksort(Item a[], int start, int stop)
{
int i, s = 0, stack[64];
stack[s++] = start;
stack[s++] = stop;
while (s > 0)
{
stop = stack[--s];
start = stack[--s];
if (start >= stop) continue;
i j
100,000 24 53 43
200,000 52 111 92
z How it works:
• Leaves the array containing the data unchanged
• Generates an array where position i records position of the
ith smallest item in the original data
Example:
Indexing with Insertion Sort
void sort(int index[], Item a[], int start, int stop)
{
int i, j;
z For MergeSort
• Sedgewick, Chapter 8