CS202 Fall 2012 Lecture 11/27 Sorting - ece.uic.edu file11/27/12 1 1 CS202 Fall 2012 Lecture 11/27...
Transcript of CS202 Fall 2012 Lecture 11/27 Sorting - ece.uic.edu file11/27/12 1 1 CS202 Fall 2012 Lecture 11/27...
11/27/12
1
1
CS202 Fall 2012 Lecture 11/27
Sorting
Prof. Tanya Berger-Wolf
2
Lecture outline
bogo sort
bubble sort
selection sort
insertion sort
merge sort
quick sort
the quick select problem
Sorting
Sorting = putting objects in order in array/list one of the fundamental problems in computer science comparison-based sorting: must determine order through comparison operations on the input data: <, >, compareTo, …
15 2 8 1 17 10 12 5
0 1 2 3 4 5 6 7
4
1. Bogo sort
11/27/12
2
5
Bogo sort bogo sort: orders a list of values by repetitively shuffling them and checking if they are sorted
more specifically: scan the list, seeing if it is sorted if not, shuffle the values in the list and repeat
This sorting algorithm has terrible performance! Can we deduce its runtime?
6
Bogo sort code
public static void bogoSort(int[] a) { while (!isSorted(a)) { shuffle(a); } }
// Returns true if array a's elements // are in sorted order. public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i+1]) { return false; } }
return true; }
7
Bogo sort code, helpers
// Shuffles an array of ints by randomly swapping each // element with an element ahead of it in the array. public static void shuffle(int[] a) { for (int i = 0; i < a.length - 1; i++) { // pick random number in [i+1, a.length-1] inclusive int range = a.length-1 - (i + 1) + 1; int j = (int)(Math.random() * range + (i + 1)); swap(a, i, j); } }
// Swaps a[i] with a[j]. private static void swap(int[] a, int i, int j) { if (i == j) return;
int temp = a[i]; a[i] = a[j]; a[j] = temp; }
8
Bogo sort runtime
How long should we expect bogo sort to take? related to probability of shuffling into sorted order assuming shuffling code is fair, probability of having sorted equals
1 / (number of permutations of n elements)
bogo sort takes roughly factorial time to run – note that if array is initially sorted, bogo finishes quickly!
it should be clear that this is not satisfactory...
11/27/12
3
9
2. Bubble sort
10
Bubble sort bubble sort: orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary
more specifically: scan the list, exchanging adjacent elements if they are not in
relative order; this bubbles the highest value to the top scan the list again, bubbling up the second highest value repeat until all elements have been placed in their proper order
11
Traverse a collection of elements Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons
and swapping
5 12 35 42 77 101
1 2 3 4 5 6 Swap 42 77
"Bubbling" largest element
12
"Bubbling" largest element
Traverse a collection of elements Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons
and swapping
5 12 35 77 42 101
1 2 3 4 5 6 Swap 35 77
11/27/12
4
13
"Bubbling" largest element
Traverse a collection of elements Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons
and swapping
5 12 77 35 42 101
1 2 3 4 5 6 Swap 12 77
14
"Bubbling" largest element
Traverse a collection of elements Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons
and swapping
5 77 12 35 42 101
1 2 3 4 5 6
No need to swap
15
"Bubbling" largest element
Traverse a collection of elements Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons
and swapping
5 77 12 35 42 101
1 2 3 4 5 6
Swap 5 101
16
"Bubbling" largest element
Traverse a collection of elements Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons
and swapping
77 12 35 42 5
1 2 3 4 5 6
101
Largest value correctly placed
11/27/12
5
17
Bubble sort code
public static void bubbleSort(int[] a) { for (int i = 0; i < a.length; i++) { for (int j = 1; j < a.length - i; j++) { // swap adjacent out-of-order elements if (a[j-1] > a[j]) { swap(a, j-1, j); } } } }
18
Bubble sort runtime
Running time (# comparisons) for input size n:
number of actual swaps performed depends on the data; out-of-order data performs many swaps
19
3. Selection sort
20
Selection sort
selection sort: orders a list of values by repetitively putting a particular value into its final position
more specifically: find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places
11/27/12
6
21
Selection sort example
22
Index 0 1 2 3 4 5 6 7
Value 27 63 1 72 64 58 14 9
1st pass 1 63 27 72 64 58 14 9
2nd pass 1 9 27 72 64 58 14 63
3rd pass 1 9 14 72 64 58 27 63
…
Selection sort example 2
23
Selection sort code
public static void selectionSort(int[] a) { for (int i = 0; i < a.length; i++) { // find index of smallest element int min = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } }
// swap smallest element with a[i] swap(a, i, min); } }
24
Selection sort runtime
Running time for input size n: in practice, a bit faster than bubble sort. Why?
11/27/12
7
25
4. Insertion sort
26
Insertion sort
insertion sort: orders a list of values by repetitively inserting a particular value into a sorted subset of the list
more specifically: consider the first item to be a sorted sublist of length 1 insert the second item into the sorted sublist, shifting the first
item if needed insert the third item into the sorted sublist, shifting the other
items as needed repeat until all values have been inserted into their proper positions
27
Insertion sort
Simple sorting algorithm. n-1 passes over the array At the end of pass i, the elements that occupied A[0]…A[i] originally
are still in those spots and in sorted order.
2 8 15 1 17 10 12 5
0 1 2 3 4 5 6 7
1 2 8 15 17 10 12 5
0 1 2 3 4 5 6 7
after pass 2
after pass 3
2 15 8 1 17 10 12 5
0 1 2 3 4 5 6 7
28
Insertion sort example
11/27/12
8
29
Names: Fred, Alice, David, Bill, and Carol
Insertion sort example 2
30
Insertion sort example 2
31
Insertion sort example 2
32
Worst-case for insertion sort
11/27/12
9
33
Insertion sort code
public static void insertionSort(int[] a) { for (int i = 1; i < a.length; i++) { int temp = a[i];
// slide elements down to make room for a[i] int j = i; while (j > 0 && a[j - 1] > temp) { a[j] = a[j - 1]; j--; }
a[j] = temp; } }
34
Insertion sort runtime
worst case: reverse-ordered elements in array.
best case: array is in sorted ascending order.
average case: each element is about halfway in order.
35
Comparing sorts
We've seen "simple" sorting algorithms so far, such as: selection sort insertion sort
They all use nested loops and perform approximately n2 comparisons They are relatively inefficient
comparisons swaps
selection n2/2 n
insertion worst: n2/2 best: n
worst: n2/2 best: 0
36
Average case analysis
Given an array A of elements, an inversion is an ordered pair (i, j) such that i < j, but A[i] > A[j]. (out of order elements) Assume no duplicate elements.
Theorem: The average number of inversions in an array of n distinct elements is n (n - 1) / 4. Corollary: Any algorithm that sorts by exchanging adjacent elements requires O(n2) time on average.
11/27/12
10
37
CS202 Fall 2012 Lecture
Sorting
Prof. Tanya Berger-Wolf
http://xkcd.com/584/ 38
5. Merge sort
39
Merge sort
merge sort: orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining Invented by John von Neumann in 1945
more specifically: divide the list into two roughly equal parts recursively divide each part in half, continuing until a part contains
only one element merge the two parts into one sorted list continue to merge parts as the recursion unfolds
This is a "divide and conquer" algorithm.
40
Merge sort idea: Divide the array into two halves. Recursively sort the two halves (using merge sort). Use merge to combine the two arrays.
sort sort merge(0, n/2, n-1)
mergeSort(0, n/2-1) mergeSort(n/2, n-1)
Merge sort
11/27/12
11
41
67 45 23 14 6 33 98 42
42
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
43
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
44
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98
11/27/12
12
45
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98
Merge
46
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98
23
Merge
47
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98
23 98
Merge
48
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
23 98
11/27/12
13
49
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
Merge
23 98
50
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
14
Merge
23 98
51
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
45
Merge
23 98 14
52
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
Merge
98 45 14 23
11/27/12
14
53
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
Merge
98 14
14
23 45
54
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
Merge
23 14
14 23
98 45
55
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
Merge
23 98 45 14
14 23 45
56
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
Merge
23 98 45 14
14 23 45 98
11/27/12
15
57
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
23 98 45 14
14 23 45 98
58
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6
23 98 45 14
14 23 45 98
59
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6
Merge
23 98 45 14
14 23 45 98
60
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6
6
Merge
23 98 45 14
14 23 45 98
11/27/12
16
61
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6
67
Merge
23 98 45 14 6
14 23 45 98
62
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
23 98 45 14 67 6
14 23 45 98
63
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6
14 23 45 98
64
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
33 23 98 45 14 67 6
14 23 45 98
11/27/12
17
65
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
42 23 98 45 14 67 6 33
14 23 45 98
66
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 23 45 98
67
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 6 42 33
14 23 45 98 6
67
68
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 6 33
14 23 45 98 6 33
67 42
11/27/12
18
69
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 6 42 33
14 23 45 98 6 33 42
67
70
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 23 45 98 6 33 42 67
71
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
23 45 98 33 42 67 14 6
72
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
23 45 98 6 42 67
6
14 33
11/27/12
19
73
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 45 98 6 42 67
6 14
23 33
74
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 23 98 6 42 67
6 14 23
45 33
75
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 23 98 6 33 67
6 14 23 33
45 42
76
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 23 98 6 33 42
6 14 23 33 42
45 67
11/27/12
20
77
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 23 45 6 33 42
6 14 23 33 42 45
98 67
78
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
79
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
Merge
23 98 45 14 67 6 42 33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
80
67 45 23 14 6 33 98 42
67 45 23 14 6 33 98 42
45 23 14 98
23 98 45 14
67 6 33 42
67 6 33 42
23 98 45 14 67 6 42 33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
11/27/12
21
81
67 45 23 14 6 33 98 42
6 14 23 33 42 45 67 98
82
13 6 21 18 9 4 8 20
0 7
4 6 8 9 13 18 20 21
0 7
13 6 21 18
0 3
9 4 8 20
4 7
6 13 18 21
0 3
4 8 9 20
4 7
13 6
0 1
21 18
2 3
9 4
4 5
8 20
6 7
6 13
0 1
18 21
2 3
4 9
4 5
8 20
6 7
13
0
6
1
21
2
18
3
9
4
4
5
8
6
20
7
Merge sort example 2
merge operation: Given two sorted arrays, merge operation produces a sorted array with all
the elements of the two arrays
A 6 13 18 21 B 4 8 9 20
C 4 6 8 9 13 18 20 21
Running time of merge: O(n), where n is the number of elements in the merged array. when merging two sorted parts of the same array, we'll need a temporary array
to store the merged whole
Merging two sorted arrays
84
Merge sort code
public static void mergeSort(int[] a) { int[] temp = new int[a.length]; mergeSort(a, temp, 0, a.length - 1); }
private static void mergeSort(int[] a, int[] temp, int left, int right) { if (left >= right) { // base case return; }
// sort the two halves int mid = (left + right) / 2; mergeSort(a, temp, left, mid); mergeSort(a, temp, mid + 1, right);
// merge the sorted halves into a sorted whole merge(a, temp, left, right); }
11/27/12
22
85
Merge code
private static void merge(int[] a, int[] temp, int left, int right) { int mid = (left + right) / 2; int count = right - left + 1;
int l = left; // counter indexes for L, R int r = mid + 1;
// main loop to copy the halves into the temp array for (int i = 0; i < count; i++) if (r > right) { // finished right; use left temp[i] = a[l++]; } else if (l > mid) { // finished left; use right temp[i] = a[r++]; } else if (a[l] < a[r]) { // left is smaller (better) temp[i] = a[l++]; } else { // right is smaller (better) temp[i] = a[r++]; }
// copy sorted temp array back into main array for (int i = 0; i < count; i++) { a[left + i] = temp[i]; } }
Merge sort runtime
General form: T(n) = aT(n/b) + f(n)
€
i=0
logbn
∑
€
aif( nbi )
Merge sort T(n) = 2T(n/2) + n
a = 2, b = 2, f(n) = n.
€
i=0
log2n
∑
€
2i ⋅ (n/2i)
€
= n log2n
87
6. Quick sort
88
Quick sort
quick sort: orders a list of values by partitioning the list around one element called a pivot, then sorting each partition invented by British computer scientist C.A.R. Hoare in 1960
more specifically: choose one element in the list to be the pivot (= partition element) organize the elements so that all elements less than the pivot are to
its left and all greater are to its right apply the quick sort algorithm (recursively) to both partitions
11/27/12
23
89
Quick sort, continued
For correctness, it's okay to choose any pivot.
For efficiency, one of following is best case, the other worst case: pivot partitions the list roughly in half pivot is greatest or least element in list
Which case above is best?
We will divide the work into two methods: quickSort – performs the recursive algorithm partition – rearranges the elements into two partitions
90
Quick sort pseudo-code
Let S be the input set.
1. If |S| = 0 or |S| = 1, then return.
2. Pick an element v in S. Call v the pivot.
3. Partition S – {v} into two disjoint groups: • S1 = {x ∈ S – {v} | x ≤ v} • S2 = {x ∈ S – {v} | x ≥ v}
4. Return { quicksort(S1), v, quicksort(S2) }
91
40
10 18
32 2
35 37
17 6
12
pick a pivot
6 10 12 2
17 18
40 37 32 35
partition
quicksort quicksort
18 10 12 17 6 2 40 37 35 32
combine
18 10 12 17 6 2 40 37 35 32
Quick sort illustrated
92
How to choose a pivot
first element bad if input is sorted or in reverse sorted order bad if input is nearly sorted variation: particular element (e.g. middle element)
random element even a malicious agent cannot arrange a bad input
median of three elements choose the median of the left, right, and center elements
11/27/12
24
93
Partitioning algorithm
The basic idea: 1. Move the pivot to the rightmost position. 2. Starting from the left, find an element ≥ pivot. Call the position i. 3. Starting from the right, find an element ≤ pivot. Call the position j. 4. Swap S[i] and S[j].
8 1 4 9 0 3 5 2 7 6
0 9
94
Quick sort code
public static void quickSort(int[] a) { quickSort(a, 0, a.length - 1); }
private static void quickSort(int[] a, int min, int max) { if (min >= max) { // base case; no need to sort return; }
// choose pivot -- we'll use the first element (might be bad!) int pivot = a[min]; swap(a, min, max); // move pivot to end
// partition the two sides of the array int middle = partition(a, min, max - 1, pivot);
// restore the pivot to its proper location swap(a, middle, max);
// recursively sort the left and right partitions quickSort(a, min, middle - 1); quickSort(a, middle + 1, max); }
95
Quick sort code, cont'd.
// partitions a with elements < pivot on left and // elements > pivot on right; // returns index of element that should be swapped with pivot private static int partition(int[] a, int min, int max, int pivot) { while (true) { // move markers i,j inward to find misplaced elements while (a[min] < pivot) { min++; } while (a[max] > pivot) { max--; }
if (min >= max) break;
swap(a, min, max); min++; max--; }
return min; }
96
CS202 Fall 2012 Lecture
Sorting
Prof. Tanya Berger-Wolf
11/27/12
25
97
9 17 3 12 8 7 21 1
0 7
1 17 3 12 8 7 21 9
0 7
1 7 3 12 8 17 21 9
0 7
1 7 3 8 12 17 21 9
0 7
1 7 3 8 9 17 21 12
0 7
pick pivot
i
i
j
j
j i swap S[i] with S[right]
"Median of three" pivot
98
Special cases
What happens when the array contains many duplicate elements?
What happens when the array is already sorted (or nearly sorted) to begin with?
Small arrays Quicksort is slower than insertion sort when is N is small (say, N ≤
20). Optimization: Make |A| ≤ 20 the base case and use insertion sort
algorithm on arrays of that size or smaller.
99
Quick sort runtime
Worst case: pivot is the smallest (or largest) element all the time. T(n) = T(n-1) + cn T(n-1) = T(n-2) + c(n-1) T(n-2) = T(n-3) + c(n-2) … T(2) = T(1) + 2c
Best case: pivot is the median T(n) = 2 T(n/2) + cn T(n) = O(n log n)
€
T(N) = T(1) + c ii=2
N
∑ = O(N2)
100
Quick sort runtime, cont'd.
Assume each of the sizes for S1 are equally likely. 0 ≤ |S1| ≤ N-1.
€
T(N) = 1N
[T(i) +i=0
N−1
∑ T(N - i -1)]⎛
⎝ ⎜ ⎜
⎞
⎠ ⎟ ⎟ + cN
= 2N
T(i)i=0
N−1
∑⎛
⎝ ⎜ ⎜
⎞
⎠ ⎟ ⎟ + cN
N T(N) = 2 T(i)i=0
N−1
∑⎛
⎝ ⎜ ⎜
⎞
⎠ ⎟ ⎟ + cN2
(N −1) T(N −1) = 2 T(i)i=0
N−2
∑ + c(N -1)2
11/27/12
26
101
€
N T(N) = (N + 1) T(N −1) + 2cN
€
T(N)N + 1
=T(N −1)
N+
2cN + 1
€
T(N -1)N
=T(N −2)
N -1+
2cN
€
T(N - 2)N −1
=T(N −3)
N - 2+
2cN −1
…
€
T(2)3
=T(1)2
+2c3
€
T(N)N + 1
=T(1)
2+ 2c 1
ii=3
N+1
∑
€
T(N) = O(N log N) ≈loge (N+1) – 3/2
divide equation by N(N+1)
Quick sort runtime, cont'd.
102
Quick sort runtime summary
O(n log n) on average. O(n2) worst case.
comparisons
merge O(n log n)
quick average: O(n log n) worst: O(n2)
103
Sorting practice problem
Consider the following array of int values.
[22, 11, 34, -5, 3, 40, 9, 16, 6]
(f) Write the contents of the array after all the partitioning of quick sort has finished (before any recursive calls).
Assume that the median of three elements (first, middle, and last) is chosen as the pivot.
104
Sorting practice problem
Consider the following array: [ 7, 17, 22, -1, 9, 6, 11, 35, -3]
Each of the following is a view of a sort-in-progress on the elements. Which sort is which? (If the algorithm is a multiple-loop algorithm, the array is shown
after a few of these loops have completed. If the algorithm is recursive, the array is shown after the recursive calls have finished on each sub-part of the array.)
Assume that the quick sort algorithm chooses the first element as its pivot at each pass.
(a) [-3, -1, 6, 17, 9, 22, 11, 35, 7] (b) [-1, 7, 17, 22, -3, 6, 9, 11, 35] (c) [ 9, 22, 17, -1, -3, 7, 6, 35, 11] (d) [-1, 7, 6, 9, 11, -3, 17, 22, 35] (e) [-3, 6, -1, 7, 9, 17, 11, 35, 22] (f) [-1, 7, 17, 22, 9, 6, 11, 35, -3]
11/27/12
27
105
Sorting practice problem
For the following questions, indicate which of the six sorting algorithms will successfully sort the elements in the least amount of time. The algorithm chosen should be the one that completes fastest,
without crashing. Assume that the quick sort algorithm chooses the first element as
its pivot at each pass. Assume stack overflow occurs on 5000+ stacked method calls. (a) array size 2000, random order (b) array size 500000, ascending order (c) array size 100000, descending order
– special constraint: no extra memory may be allocated! (O(1) storage)
(d) array size 1000000, random order (e) array size 10000, ascending order
– special constraint: no extra memory may be allocated! (O(1) storage)
106
Selection problem; quickSelect Recall: the Selection problem Find the kth smallest element in an array a
quickSelect(a, k): 1. If a.length = 1, then k=1 and return the element. 2. Pick a pivot v ∈ a. 3. Partition a – {v} into a1 (left side) and a2 (right side). • if k ≤ a1.length, then the kth smallest element must be in a1. So
return quickSelect(a1, k). • else if k = 1 + a1.length, return the pivot v. • Otherwise, the kth smallest element is in a2. Return quickSelect(a2,
k - a1.length - 1).
107
Quick select runtime
…
= O(N)
average case:
worst case: same as Quick sort, O(N2)