Beruflich Dokumente
Kultur Dokumente
<p p p
9/10/2003
CS2 - Quicksort
Partitioning
97 17 37
12 46 10 55 80 42 39
Pick pivot == 37
9/10/2003
CS2 - Quicksort
Partitioning
97 17 39
12 46 10 55 80 42 37
9/10/2003
CS2 - Quicksort
Partitioning
97 17 39
12 46 10 55 80 42 37
9/10/2003
CS2 - Quicksort
Partitioning
i j
97 17 39
12 46 10 55 80 42 37
9/10/2003
CS2 - Quicksort
Partitioning
i j
97 17 39
12 46 10 55 80 42 37
Step 3: move i left until value larger than the pivot is found
9/10/2003
CS2 - Quicksort
Partitioning
i j
97 17 39
12 46 10 55 80 42 37
Step 4: move j right until value less than the pivot is found
9/10/2003
CS2 - Quicksort
Partitioning
i j
10 17 39
12 46 97 55 80 42 37
9/10/2003
CS2 - Quicksort
Partitioning
i j
10 17 39
12 46 97 55 80 42 37
Step 6: move i left until value larger than the pivot is found
9/10/2003
CS2 - Quicksort
Partitioning
i j
10 17 39
12 46 97 55 80 42 37
Step 7: move j right until value less than the pivot is found
9/10/2003
CS2 - Quicksort
10
Partitioning
i j
10 17 12
39 46 97 55 80 42 37
9/10/2003
CS2 - Quicksort
11
Partitioning
i j
10 17 12
39 46 97 55 80 42 37
9/10/2003
CS2 - Quicksort
12
Partitioning
i j
10 17 12
37 46 97 55 80 42 39
9/10/2003
CS2 - Quicksort
13
Partitioning
pivot
10 17 12 < pivot
37 46 97 55 80 42 39 pivot
9/10/2003
CS2 - Quicksort
14
Partitioning
public static void partition( int numbers[], int lo, int hi ) { int i = lo + 1, j = hi, pivot = numbers[lo]; while ( i < j ) { while ( numbers[i] < pivot ) i++; while ( numbers[j] > pivot ) j++; if ( i <= j ) { int tmp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = tmp; } } numbers[0] = numbers[i]; numbers[i] = pivot; return i; }
9/10/2003 CS2 - Quicksort 15
Quicksort
The quicksort algorithm (S is the array of elements to be sorted):
1. If the length of S is 0 or 1, return 2. Pick any element p in S; this is the pivot 3. Partition S (by rearranging its elements) into two subsets: S1, containing all elements of S-p which are < p S2, containing all elements of S-p which are >= p 4. Return quicksort(S1) followed by p, followed by quicksort(S2)
Selecting the pivot wisely can significantly improve the performance of the algorithm
9/10/2003 CS2 - Quicksort 16
Analysis
n
n n/2 n/4 n/8 n/8 n/8 n/4 n/8 n/8 n/4 n/8 n/8 n/2 n/4 n/8
log2n
9/10/2003
CS2 - Quicksort
18
Analysis
n
Merging
Merging two sorted arrays into a single sorted array is straight forward
1. Set i and j to 0 2. While i < array1.length and j < array2.length 3. If array1[i]<array2[j] then copy array1[I] to the new array and increment i otherwise, copy array2[j] to the new array and increment j 4. Copy the rest of the non-empty array to the new array
9/10/2003
CS2 - Quicksort
20
10
Merging
i 2 3 8 j 3 4 6 7
k
9/10/2003 CS2 - Quicksort 21
Merging
i 2 3 8 j 3 4 6 7
k
9/10/2003 CS2 - Quicksort 22
11
Merging
i 2 3 8 j 3 4 6 7
k
9/10/2003 CS2 - Quicksort 23
Merging
i 2 3 8 3 j 4 6 7
k
9/10/2003 CS2 - Quicksort 24
12
Merging
i 2 3 8 3 j 4 6 7
k
9/10/2003 CS2 - Quicksort 25
Merging
i 2 3 8 3 4 j 6 7
k
9/10/2003 CS2 - Quicksort 26
13
Merging
i 2 3 8 3 4 6 j 7
k
9/10/2003 CS2 - Quicksort 27
Merging
i 2 3 8 3 4 6 7 j
k
9/10/2003 CS2 - Quicksort 28
14
Merging
i 2 3 8 3 4 6 7 j
k
9/10/2003 CS2 - Quicksort 29
Merge Sort
2 2 2 2 97 97 97 17 39 97 17 39 17 39 17 39 39
9/10/2003
12 46 10 55 80 42 37 12 12 12 12
CS2 - Quicksort
46 10 55 80 42 37 46 10 55 46 10 55 10 55 80 42 37 80 42 42 37 37
30
15
Merge Sort
2 2 2 2 97 97 97 17 39 97 17 39 17 39 17 12 12 12 46 10 55 80 42 37 46 10 55 80 42 37 46 10 55 46 10 55 10 55 80 42 37 80 37 42 42 37
31
12 39 39 12
9/10/2003
CS2 - Quicksort
Merge Sort
2 2 2 2 97 97 97 17 39 97 17 39 12 17 17 12 39 12 46 10 55 80 42 37 46 10 55 80 42 37 10 46 46 55 37 42 80 37 42 80 42 37
32
12 39 39 12
10 55 10 55
9/10/2003
CS2 - Quicksort
16
Merge Sort
2 2 2 2 97 97 97 17 39 12 17 39 12 17 17 97 39 12 46 10 55 80 42 37 10 37 42 46 55 80 10 46 46 55 37 42 80 37 42 80 42 37
33
12 39 39 12
10 55 10 55
9/10/2003
CS2 - Quicksort
Merge Sort
2 2 2 2 97 97 10 12 17 12 17 39 12 17 17 97 39 37 39 42 46 55 80 97 10 37 42 46 55 80 10 46 46 55 37 42 80 37 42 80 42 37
34
12 39 39 12
10 55 10 55
9/10/2003
CS2 - Quicksort
17
Analysis
n
n n/2 n/4 n/8 n/8 n/8 n/4 n/8 n/8 n/4 n/8 n/8 n/2 n/4 n/8
log2n
Complexity nlog2n
9/10/2003
CS2 - Quicksort
35
18