CS100-QuickSortAndReview.pptx

advertisement
What does this function do when
given this list {44, 3, 17, 1, 16, 6} and
the pivotIndex 5?
public int pivotList(int[] data, int pivotIndex) {
int pivot = data[pivotIndex];
int pivotPosition = 0;
swap(data,pivotIndex, 0);
for(int i = 1; i < data.length; i++) {
if(data[i] < pivot) {
//move the item we want to swap just to the right
//of the pivot
swap(data,pivotPosition + 1, i);
//swap the item we want to swap and the pivot
swap(data, pivotPosition, pivotPosition+1);
pivotPosition++;
}
}
return pivotPosition;
}
One More Algorithm
& Review for the Exam
Quicksort
• Another recursive sort
• The basic idea:
– Select a random element (call it the pivot)
– “sort” the list into 2 sections, those less than the
pivot and those greater than the pivot
– Recursively call the sort on those two sections
• O(n log n)
• An important detail: the pivot must be “good”
in that it divides the list approximately in half
Question Types for the Final
• Algorithms (a la Midterm 1)
• Classes, Interfaces, and
Inheritance (similar to
problem 4 on Midterm 1)
• Equals and
Hashcode/Comparable
(similar to problem 5 or 6
on Midterm 1)
• Big O (includes stuff from
Midterm 1, plus Recurrence
Relations on midterm 2)
• Recursion (a la Midterm 1)
• Huffman (No Coding) and
maybe something about
Trees (No Coding)
• Linked List
• Recursive Backtracking (it'll be
one of the 2 player problems)
• Trees (Coding)
• Stacks Queues and Priority
Queues
• Sorting
• Graph Questions (2 of them)
13 Problems altogether…10 Coding problems
You will have 3 hours. Get here early to watch/listen to music videos!
Download