Transcript .ppt

Announcements
• P2 is due today
• Prelim on Monday in class-- Review
Session tomorrow -- Bring Your Questions!
• No quiz today. :-)
CS100
Lecture 12
1
Today’s Topics
• Review
• Sort
– Selection Sort
– Insertion Sort
– Quicksort
• Recursive version
– A brief introduction to recursion -- we’ll spend a whole
lecture on recursion later on in the course
• Next week: Iterative version
CS100
Lecture 12
2
Review
•
•
•
•
Linear Search
Binary Search
How fast is binary search?
How does binary search work?
CS100
Lecture 12
3
What’s selection sort?
Selection sort works by, at each step, placing the next smallest value into position:
Start
4 8 3 7
2
1
After step 1
1 8 3 7
2
4
After step 2
1 2 3 7
8
4
After step 3
1 2 3 7
8
4
After step 4
1 2 3 4
8
7
After step 5
1 2 3 4
7
8
At each step, the boldfaced value is the one to put in its place next; it gets
swapped with the value that’s in the array element where it belongs.
CS100
Lecture 12
4
Show me the algorithm!
static public void selection Sort(int [ ] b) {
int k= 0;
//inv: b[0..k-1] is sorted, b[0..k-1] <= b[k..]:
while (k < b.length-1) {
// Set j so that b[j] is the minimum of b[k..b.length-1]
int j= k; int h= k+1;
// inv.:b[j] is minimum of b[k..h-1]
while (h<b.length) {
if (b[h] < b[j]) j= h;
h= h+1; }
// Swap b[k] with b[j]
int t= b[k]; b[k]= b[j]; b[j]= t;
k= k+1;}
CS100
Lecture 12
5
How fast is selection sort?
• How fast is selectionSort? Want a general idea of
its speed that is independent of the computer on
which it executes. Don’t give time in seconds or
milliseconds.
• The operation that is performed the most is the
array comparison b[h] < b[j]. We take the number
of such comparisons performed as a measure of
speed. Abbreviate b.length as n . . .
CS100
Lecture 12
6
Selection sort analysis
Iteration
of outer loop
0
1
2
No. times array comparison performed
during this iteration of outer loop
n-1
n-2
n-3
...
last
1
So number of comparisons is
1 + 2 + 3 + … +(n-2) + (n-1) = n * (n-1) / 2 = n2/2 - n/2
As n gets large, the term n2 dominates. We say the number if
comparisons is proportional to n2 and that this is a quadratic
algorithm.
CS100
Lecture 12
7
Insertion sort
• Intuitively: think about when you have been
dealt a hand of cards
• Look at each item and insert it into its
proper place in what you’ve already sorted
so far
• Thus, the first part of the array is always
sorted, and the last part is unsorted.
CS100
Lecture 12
8
Insertion sort algorithm
k= 0;
while (k < b.length-1) {
// Place b[k] in its sorted position in b[0..k]
int temp= b[k]; //Save b[k] in temp
h= k;
// Inv: b[0..k] is sorted except for position b[h], temp <= b[h+1..k]
while (h != 0) && b[h-1]>=temp) {
b[h]= b[h-1];
h= h-1;
}
// Note: {b[0..h-1] <= temp <= b[h+1..k]}
b[h]= temp;
}
k= k+1;
Lecture 12
9
} CS100
Partition
• We are going to develop a sorting algorithm called quicksort. An
important piece of it is an algorithm called partition, which starts
with an array segment that looks like this (x is the initial value in
b[h]):
h h+1
b x
(0)
k
?
• and permutes its elements so that it looks like this:
h
b <= x
(1)
j
x
k
>x
• In rearranging the array in this fashion, the order of the final
values of b[h+1..j-1] and b[j+1..k] doesn’t matter
CS100
Lecture 12
10
Partition algorithm
B[h..k] contains at least 2 elements. Using the name x for the original
value in b[h], permute b[h..k] so that it looks as below and return j
h
b <= x
j
x
k
>x
public int partition (int [ ] b, int h, int k ) {
int i= h; int j= k;
while (i <= j) {
if (b[i] <= x)
i= i+1;
else if (b[j] > x) j= j-1;
else {//Swap b[i] and b[j]
int t= b[i]; b[i]= b[j]; b[j]= t;}
}
// Swap b[h] and b[j]
int s= b[i]; b[i]= b[j]; b[j]= s;
return j; }
CS100
Lecture 12
11
Recursive Quicksort
• Suppose we want to sort b[h..k]. Let’s use algorithm
partition to make it look like this, where x is the value
h
j
k
initially in b[h]: b <= x
x
>x
• What remains to be done in order to sort b[h..k]? Two
things:
– Sort b[h..j-1]
– Sort b[j+1..k]
• We present on the next slide a recursive version of
quicksort --a version that calls itself. We don’t expect
you to fully comprehend it, because recursion, the idea
of a method calling itself is new to you. Later, we
present a non-recursive version.
CS100
Lecture 12
12
Recursive Quicksort Algorithm
// Sort b[h..k]
public static void quicksort(int[ ] b, int h, int k) {
if ( (k-h) == 0) return; // one element in array, so sorted
if (h+1-k == 0) { // b[h..k] has exactly 2 elements
if (b[h] <= b[k]) return;
// Swap b[h] and b[k] int t= b[h]; b[h]= b[k]; b[k]= t;
return; }
int j= partition(b,h,k);
// b[h..k] looks like
h
j
k
b <= x
x
>x
// Sort b[h..j-1]
quicksort(b,h,j-1); // recursive call
// Sort b[j+1..k]
quicksort(b,j+1,k); // recursive call
}
CS100
Lecture 12
13
What’s this look like?
quicksort ( b, 0, 12);
0
12
b 3 6 8 2 7 1 9 4 6 8 7 5 4
start:
frame
b
h 0
k 12
j
h
j
k
After partition b 2 1 3 8 7 6 9 4 6 8 7 5 4
frame
b
h 0
k 12
j 2
At this point, the call sort(b,h,j-1); has to be executed.
CS100
Lecture 12
14
Remember how method calls work
• Even with recursion, if we follow the steps
for method invocation, everything works
out
–
–
–
–
–
CS100
1. Draw a frame for the call (place it where?)
2. Write in the parameters and local variables
3. Assign arguments to parameters
4. Execute method body
5. Erase the frame for the call.
Lecture 12
15
The frames. . .
• Just after the second frame has been constructed and the arguments
have been assigned to the parameters. We call the first one frame0
and the new one frame1. h
j
k
b
Executing method
body using frame1
frame0
b
h 0
k 12
j 2
frame1
b
h 0
k 1
j
b
CS100
2 1 3 8 7 6 9 4 6 8 7 5 4
h
j
k
1 2 3 8 7 6 9 4 6 8 7 5 4
frame0
b
h 0
k 12
j 2
frame1
b
h 0
k 1
j
Lecture 12
16
Then what?
frame1 is now deleted, resulting in
b
frame0
h
j
k
1 2 3 8 7 6 9 4 6 8 7 5 4
b
h 0
k 12
j 2
The call quicksort(b,h,j-1); is complete. Now, the call
quicksort(b,j+1,k); is executed. Thus, another frame will be
constructed, to be erased when the call is completed; when the call
completes, the situation is:
b
frame0
CS100
h
j
k
1 2 3 4 4 5 6 6 7 7 8 8 9
b
h 0
k 12
Lecture 12
j 2
17
Thoughts on Recursion
You can see that the recursive calls of quicksort execute correctly by
executing the algorithm yourself, using carefully the rules for
executing method calls.
When trying to understand a method with a recursive call or write a
recursive call yourself, don’t go through the exercise of executing it.
Instead, do what we did in quicksort. In the situation
h
b <= x
j
x
k
>x
we see that the array can be sorted simply by sorting the two array
segments b[h..j-1] and b[j+1..k]. We can sort these two segments by
calling any sorting method we wish, including the one that we are
currently writing. If we call the method we are currently writing,
we are using a recursive call.
In worst case, quicksort make on the order of n2 array comparisons. In
the average case, n * log(n).
CS100
Lecture 12
18