chapter 11 sorting. selection sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31...

133
Chapter 11 Sorting

Upload: dorthy-stevenson

Post on 18-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Chapter 11

Sorting

Page 2: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Selection Sort

Page 3: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

0 1 n-2 n-1index

2413 第 0 回合

第 1 回合

2413

2431

3421 第 n-2 回合

4321

min

min

min

Page 4: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

每回合處理 a[i … n-1] ,而 i 從 0 到 n-2Select min ,與 a[ i ] swap1. 令 a[ i ] 為 min2. If a[ j+1] < min ,then 令 a[ j+1] 為 min, 而 j 從 i

到 n-1

Select( int[n] a) { for( i = 0, i <= n-2, i++){ min = i ; for( j = i, j <= n-1, j++) if a[ j + 1] < a[min] min=j+1; swap( a[min], a[ i ]); }}

Page 5: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * @author 曾繼禾 * This class implement the selection sort. * Have these method : sort, swap, print. * Have parameter : array. *//*** Do the sorting job* @return The array have been sorted*/public int[] selectionSort(int[] array){

for(int i = 0; i < array.length; i++){

int min = i;for(int j = i; j < array.length; j++){

if(array[j] < array[min])min = j;

}array = swap(array, min, i);

}return array;

}

Selection.java

Page 6: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * Print the array of this object */public void print(){

for(int i = 0; i < array.length; i++)System.out.print(array[i] + " ");

System.out.println();}

/** * This methd is used to swap two position of value in one array * @param input Input array * @param a The first position to swap * @param b The second position to swap * @return Return the swaped array */public int[] swap(int[] input, int a, int b){

int temp = input[a];input[a] = input[b];input[b] = temp;return input;

}public int[] getArray() {

return array;}public void setArray(int[] array) {

this.array = array;}

}

Selection.java(continued)

Page 7: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Main.java/** * @author 曾繼禾 * * This is main class, using Selection object to do selection sort * * Have method : main */

/** * The main method, program start from here * @param args */public static void main(String[] args) {

int[] x = { 3, 1, 4, 2 };print(x);selectionSort(x);print(x);

}

Page 8: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Insertion Sort

Page 9: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

3 1 4 2

0 1 n-2 n-1index

4 23 1 第一回合

第二回合

第 n-1 回合

1 3 24

1 3 4 2

1 2 3 4

Page 10: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

每回合處理 a[i … n-1] ,而 i 從 1 到 n-1把 a[i] instert 到左邊排序位置(if a[j]<a[j-1] then swap else 不做 而 j 從 i 到 1)

InsertionSort(int[n] a){ for(i=1;i<=n-1;i++) for(j=I;j>=1;j--) if a[j]<a[j-1] swap(a[j],a[j-1]) else break; }void swap(int a,int b) { int temp; temp=a; a=b; b=temp; }

Page 11: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * @author 黃柏昇 * This class implement the Insertion sort. * Have these method : sort, swap, print. * Have parameter : array. *//** * Do the sorting job * @return The array have been sorted */public void insertionSort(int[] array){ for(int i = 1; i <= array.length-1; i++) {

for(int j = i; j >= 1; j--) { if(array[j] < array[j-1]) array = swap(array, array[j],array[j-1]); }

}return array;}

Insertion.java

Page 12: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/* Print the array of this object*/public void print(){for(int i = 0; i < array.length; i++)System.out.print(array[i] + " ");System.out.println();}

/** * This methd is used to swap two position of value in one array * @param input Input array * @param a The first position to swap * @param b The second position to swap * @return Return the swaped array */public int[] swap(int[] input, int a, int b){int temp = input[a];input[a] = input[b];input[b] = temp;return input;}public int[] getArray() {return array;}public void setArray(int[] array) {this.array = array;}

Insertion.java(continued)

Page 13: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * The main method, program start from here * @param args */public static void main(String[] args) {int[] x = { 3, 1, 4, 2 };

print(x);insertionSort(x);print(x);

}

Insertion.java(continued)

Page 14: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Bubble Sort

Page 15: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

3

1

4

2

3

1

4

2

1

3

2

1

2

index

0

1

2

n-1

第 n-1 回合 第 n-2 回合 第 1 回合

44

3

4 3 2 1

Page 16: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

每回合處理 a[0 … i] ,而 i 從 n-1 到 1(if a[j]<a[j+1] then swap 而 j 從 0 到 i-1)

BubbleSort(int[n] a){ for(i=n-1;i>=1;i--) for(j=0;j<=i-1;j++) if a[j]>a[j+1] swap(a[j],a[j+1]);}void swap(int a,int b) { int temp; temp=a; a=b; b=temp; }

Page 17: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * @author 黃柏昇 * This class implement the Bubble sort. * Have these method : sort, swap, print. * Have parameter : array. *//** * Do the sorting job * @return The array have been sorted */public void bubbleSort(int[] array){ for(int i = array.length-1; i >= 1; i--) {

for(int j = 0; j <= i-1; j++) {

if(array[j] > array[j+1]) array = swap(array, array[j],array[j+1]);

}

}return array;}

Bubble.java

Page 18: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/* Print the array of this object*/public void print(){for(int i = 0; i < array.length; i++)System.out.print(array[i] + " ");System.out.println();}

/** * This methd is used to swap two position of value in one array * @param input Input array * @param a The first position to swap * @param b The second position to swap * @return Return the swaped array */public int[] swap(int[] input, int a, int b){int temp = input[a];input[a] = input[b];input[b] = temp;return input;}public int[] getArray() {return array;}public void setArray(int[] array) {this.array = array;}

Bubble.java(continued)

Page 19: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * The main method, program start from here * @param args */public static void main(String[] args) {

int[] x = { 2, 4, 1, 3 };print(x);bubbleSort(x);print(x);

}

Bubble.java(continued)

Page 20: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Quick Sort

Page 21: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

快速排序法 Quick Sort• Quicksort example

pivot i j

result

令 a[left] 為 pivot ( 即 3)

a

index 0 1 2 n-2 n-1left right

3 8 1 4 2令 i 為 left+1, j 為 right

1. while a[i] < pivot, i 向右移2. while a[j] >= pivot, j 向左移3. if (i<j), a[i] 和 a[j] 互換repeat until (i>j)

quickSort(a,left,j-1)

quickSort(a,j+1,right)

3 8 1 4 2

a[left] 和 a[j] 互換1 2 3 4 8

left rightright left

如果 a 為空或 left>=right 則結束

1 2 3 4 8

Page 22: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Quick Sort AlgorithmquickSort(int[] a, int left, int right){

int pivot = a[left]; int i = left+1, j = right; do{ while( a[i] < pivot ){ i++; }

while( a[j] >= pivot ){ j++; } if ( i < j ){ swap(a[i],a[j]) }

}while( i <= j)

swap(a[left], a[j]); quickSort(a, left, j-1); quickSort(a, j+1, right); }

Page 23: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Quick Sort/** * @author 胡升瑞 * This class implement the Quick Sort. * Have these method : QuickSort, swap, print. * Have parameter : array. */public void quickSort(int[] array, int low, int high) {

if (array == null || array.length == 0){return;}if (low >= high){return;}

//pick the pivotint pivot = array[low];//make left < pivot and right > pivotint i = low, j = high;while (i <= j) {

while (array[i] < pivot) {i++;}while (array[j] > pivot) {j--;}

if (i <= j) {swap(array,i,j); i++; j--;}}}//recursively sort two sub partsif (low < j){quickSort(array, low, j);}

if (high > i){quickSort(array, i, high);}}

Page 24: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Quick Sort(cont.)

/*Print the array of this object*/public void print(int[] x) {

for (int a : x)System.out.print(a + " ");System.out.println();

}/** * This methd is used to swap two position of value in one array * @param input Input array * @param a The first position to swap * @param b The second position to swap * @return Return the swaped array */public int[] swap(int[] input, int a, int b){

int temp = input[a];input[a] = input[b];input[b] = temp;return input;

}

Page 25: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Quick Sort(cont.)/** * The main method, program start from here * @param args */public static void main(String[] args) {

int[] x = { 3, 8, 1, 4, 2 };print(x);

int low = 0;int high = x.length - 1;

quickSort(x, low, high);print(x);

}

Page 26: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Assumptions:

Comparison-based sorts: Sorting is accomplished by comparing elements to each other.

We will sort sequences only:

Array ArrayList LinkedList

Page 27: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For now, assume we want to sort

x [0], x [1], …, x [n – 1], an array of n int values into increasing order.

Here is the method specification and a test method for Insertion Sort, one of the sorts in this chapter:

Page 28: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * Sorts a specified array of int values into ascending order. * The worstTime(n) is O(n * n). * * @param x - the array to be sorted. * * @throws NullPointerException - if x is null. * */ public static void insertionSort (int[ ] x) @Test public void testSample() { int [ ] expected ={12, 16, 17, 32, 33, 40, 43, 44, 46, 46, 50, 55, 59, 61, 75, 80, 80, 81, 87, 95}; int [ ] actual = {59, 46, 32, 80, 46, 55, 50, 43, 44, 81, 12, 95, 17, 80, 75, 33, 40, 61, 16, 87}; Sorts.insertionSort (actual); assertArrayEquals (expected, actual); } // method testSample

Page 29: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Insertion sort: public static void insertionSort (int[ ] x) { for (int i = 1; i < x.length; i++)

{ // At this point, x [0] <= x [1] <= … <= x [i-1].

SIFT x [i] DOWN TO ITS PROPER PLACE IN x [0], x [1], … x [i – 1]. } // for } // insertionSort

Page 30: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Example: 80, 60, 90, 75, 55, 90, …

Page 31: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

80, 60, 90, 75, 55, 90, … 60, 80 (Sorted)

Page 32: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

60, 80, 90, 75, 55, 90, … 60, 80, 90 (Sorted)

Page 33: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

60, 80, 90, 75, 55, 90, … 60, 75, 80, 90 (Sorted)

Page 34: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

60, 75, 80, 90, 55, 90, … 55, 60, 75, 80, 90 (Sorted)

Page 35: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

55, 60, 75, 80, 90, 90, … 55, 60, 75, 80, 90, 90 (Sorted)

Page 36: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Sift x [i] down by comparing it to x [i-1], x [i-2], …, until the proper place for x [i] is found.

Page 37: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * Sorts a specified array of int values into ascending * order.

* The worstTime(n) is O(n * n). * * @param x – the array to be sorted. * */ public static void insertionSort (int[ ] x) { for (int i = 1; i < x.length; i++) for (int k = i; k > 0 && x [k - 1] > x [k]; k - -) swap (x, k, k -1); } // method insertionSort

Page 38: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

EXAMPLE, STARTING WITH i = 3:

60, 80, 90, 75, 55, 90 // i = 3; k = 3; swap

60, 80, 75, 90, 55, 90 // i = 3; k = 2; swap

60, 75, 80, 90, 55, 90 // i = 3; k = 1; no swap

60, 75, 80, 90, 55, 90 // i = 4; k = 4; swap

60, 75, 80, 55, 90, 90 // i = 4; k = 3; swap

AND SO ON

Page 39: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Worst case: x in descending order Let n = x.length. Total number of outer-loop iterations

= n-1 Total number of inner-loop iterations

= 1 + 2 + … + n-1

Page 40: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

n-1 1 + 2 + 3 + ... + n-2 + n-1 = i = n(n-1) / 2 i=1

Page 41: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

worstTime(n) maximum number of loop iterations = n-1 + n (n-1) / 2 worstTime(n) is quadratic in n.

Page 42: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

averageTime(n) average number of loop iterations n-1 + n (n-1) / 4 averageTime(n) is quadratic in n.

Page 43: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

In general, to estimate worstTime(n) or averageTime(n) for a nested loop, count the total number of iterations of the inner loop.

Page 44: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Exercise: Determine the exact number of inner-loop iterations (= number of swaps) in applying Insertion Sort to the following array of int values: 96, 86, 76, 56, 66

Page 45: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

What if we want a version of InsertionSort to sort an array of String elements into lexicographic order? public static void insertionSort (Object[ ] x) { for (int i = 1; i < x.length; i++) for (int k = i; k > 0 && x [k -1] > x [k]; k - -) swap (x, k, k -1); } // method insertionSort

Page 46: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

What if we want a version of InsertionSort to sort an array of String elements into lexicographic order? Illegal! public static void insertionSort (Object[ ] x) { for (int i = 1; i < x.length; i++) for (int k = i; k > 0 && x [k -1] > x [k]; k - -) swap (x, k, k -1); } // method insertionSort

Page 47: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

public static void insertionSort (Object[ ] x) { for (int i = 1; i < x.length; i++) for (int k = i; k > 0 && (((Comparable)x [k -1]).compareTo (x [k]) > 0);k- -) swap (x, k, k -1); } // method insertionSort

An example of a call to this method: insertionSort (words);

Page 48: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

The Comparable interface is appropriate when you want the elements sorted by the “natural” ordering, for example, String objects in lexicographic order.

Page 49: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

What if you want the elements sorted into an unnatural order? For example, Integer objects in decreasing order, or String objects sorted by the length of the string.

Page 50: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

public interface Comparator<T> { /**

* Compares two specified elements. * * @param element1 – one of the elements. * @param element2 – the other element. * * @return a negative integer, 0, or a positive * integer, depending on whether element1 * is less than, equal to, or greater than * element2. */ int compare (T element1, T element2);

} // interface Comparator

Page 51: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For example, to sort an array intArray of Integer elements into decreasing order: public class Decreasing implements Comparator<Integer> { public int compare (Integer i, Integer k) { return k.compareTo (i); } // method compare } // class Decreasing

Page 52: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

public static <T> void insertionSort (T[ ] x, Comparator<T> comp)

{ for (int i = 1; i < x.length; i++) for (int k = i; k > 0 &&

comp.compare (x [k -1], x [k]) > 0); k- -)

swap (x, k, k -1); } // method insertionSort

For an example of a call to this method: insertionSort (intArray, new Decreasing());

Page 53: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Now suppose you want to sort stringArray, an array of strings, in increasing order of the lengths of the strings, with lexicographic comparison of equal-length strings, so that “yes” < “here” < “true” < “maybe”

Page 54: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

public class ByLength implements Comparator<String> {

/** * Compares two specified String objects * lexicographically if they have the same length, and

* otherwise returns the difference in their lengths. * * @param s1 – one of the specified String objects. * @param s2 – the other specified String object. * * @return s1.compareTo (s2) if s1 and s2 have the * same length; otherwise, return

* s1.length() – s2.length(). *

*/

Page 55: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

public int compare (String s1, String s2) { int len1 = s1.length(), len2 = s2.length(); if (len1 == len2) return s1.compareTo (s2); return len1 – len2; } // method compare } // class ByLength

Page 56: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

insertionSort (stringArray, new ByLength());

The insertionSort method is unchanged!

Page 57: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

The Comparator interface allows you tocompare elements in a class any way youwant, even if you cannot modify the class.

For example, you cannot modify the Integeror String classes.

Page 58: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Exercise: Determine the ordering of “Yes”, “here”, “true”, “maybe”, and “yes” by the version of compare In the above ByLength class. Note: ‘Y’ < ‘y’.

Page 59: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

How Fast Can We Sort?

Page 60: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Given n elements to be sorted, a decision tree is a binary tree in which each non-leaf represents a comparison between two elements and each leaf represents a sorted sequence of the n elements. Left branch: Yes

Right branch: No

Page 61: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Example: Apply Insertion Sort to a1, a2, a3. a1 <= a2? a2 <= a3? a1 <= a3? a1 a2 a3 a1 <= a3? a2 a1 a3 a2 <= a3? a1 a3 a2 a3 a1 a2 a2 a3 a1 a3 a2 a1

Page 62: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

A decision tree has one leaf for eachpermutation of the n elements to be sorted.

Page 63: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

The number of permutations of n distinctelements is ?

Page 64: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

n!

Page 65: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

So a decision tree to sort n elements musthave n! leaves.

Page 66: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

By the Binary Tree Theorem, for any non-empty tree t, leaves(t) <= 2 height(t)

Page 67: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Since n! = leaves(t), we must have

n! <= 2 height (t)

which implies that log2(n!) <= height(t)

Page 68: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

In the context of a decision tree, height(t)represents the maximum number ofcomparisons needed to sort the nelements.

Page 69: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

So log2(n!) <= the maximum numberof comparisons to sort n elements.

Therefore,

worstTime(n) >= log2(n!)

Page 70: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

By concept exercise 11.7,

log2(n!) >= n/2 log2(n/2)

Page 71: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Then worstTime(n) >= n/2 log2(n/2)

Page 72: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

So worstTime(n) is (n log n) for any comparison-based sort. What can we say about averageTime(n)?

Page 73: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

averageTime(n) >= average number of comparisons

= total number of comparisons / n!

In a decision tree, what is the total number ofcomparisons equal to?

Hint: The length of each path from the root to a leafequals the number of comparisons in that path.

Page 74: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

The total number of comparisons is equalto the sum of all root-to-leaf path lengths.

Page 75: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

E(t), the external path length of tree t, is the sum of all root-to-leaf path lengths in t. So the average number of comparisons is

E(t) / n!

Page 76: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

The External Path Length Theorem states that if a binary tree t has k > 0 leaves, E(t) >= (k / 2)floor (log2(k))

Page 77: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

In a decision tree, the number of leaves is n!. so, by the External Path Length Theorem, E(t) >= (n! / 2) floor (log2(n!))

Page 78: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Then we get the following: averageTime(n) >= average # comparisons = E(t) / n!

>= (n! / 2) floor (log2(n!)) / n! = (1 / 2) floor (log2(n!)) >= (1 / 4) (log2(n!)) >= (n / 8) (log2(n / 2))

Page 79: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For any comparison-based sort,averageTime(n) is (n log n).

Page 80: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Exercise: Suppose, for some comparison-based sort method, worstTime(n) is O(n log n). True or false: 1. averageTime(n) is linear logarithmic in

n. 2. averageTime(n) is O(n log n).

3. averageTime(n) is (n log n).

Page 81: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Fast Sorts

Merge Sort

Page 82: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Given an array x of objects, keep splitting into subarrays until the size of a subarray is less than 7. Apply Insertion Sort to that subarray and then merge the subarrays back together.

Page 83: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For example, suppose x.length = 25.

Here is an outline based on size:

Page 84: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

25 Split

12 Split 13 Split 6 6 6 7 Split Ins.sort Ins.sort Ins.sort

3 4 Merge Ins.sort Ins.sort 12 Merge 7 Merge 13

Merge 25

Page 85: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

To simplify the merging, we’ll use an auxiliary array. Suppose we want to merge two sorted subarrays of 5 elements each: 20 30 44 71 95 15 17 28 33 88 15, the smaller of the two, is copied to the auxiliary array and the right index is incremented:

Page 86: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

20 30 44 71 95 15 17 28 33 88 15 (in the auxiliary array) 17, the smaller of the two, is appended to the auxiliary array and the right index is again incremented:

Page 87: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

20 30 44 71 95 15 17 28 33 88

15 17 (in the auxiliary array)

20, the smaller of the two, is copied to the auxiliaryarray and the left index is incremented:

Page 88: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

20 30 44 71 95 15 17 28 33 88

15 17 20 (in the auxiliary array)

28, the smaller of the two, is copied to the auxiliaryarray and the right index is incremented. And so on.

Page 89: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

In the worst case, how many comparisons are needed to merge two sorted subarrays of k elements each?

Page 90: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * Sorts a specified array of objects according to the * compareTo method in the specified class of elements. * The worstTime(n) is O(n log n). * * @param a – the array of objects to be sorted. * */ public static void sort(Object[ ] a)

Page 91: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

public static void sort(Object[ ] a) { Object aux[ ] = (Object[ ])a.clone(); mergeSort(aux, a, 0, a.length); }

Page 92: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/** * Sorts, by the Comparable interface, a specified range of a * specified array into the same range of another specified array. * The worstTime(k) is O(k log k), where k is the

* size of the subarray. *

* @param src – the specified array whose elements are to be * sorted into another specified array. * @param dest – the specified array whose subarray is to be

* sorted. * @param low: the smallest index in the range to be sorted.

* @param high: 1 + the largest index in the range to be * sorted. */ private static void mergeSort (Object src[ ], Object dest[ ], int low, int high)

Page 93: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

aux 59 46 32 80 46 55 87 43 44 81

mergeSort (aux, a, 0, 10)

a 59 46 32 80 46 55 87 43 44 81

mergeSort (a, aux, 0, 5) mergeSort (a , aux, 5, 10) Insertion Sort Insertion Sort

aux 32 46 46 59 80 43 44 55 81 87

merge

a 32 43 44 46 46 55 59 80 81 87

Page 94: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

aux 59 46 32 80 46 55 87 43 44 81 95 12 17 80 75 33 40 61 16 50

mergeSort (aux, a, 0, 20)

a 59 46 32 80 46 55 87 43 44 81 95 12 17 80 75 33 40 61 16 50

mergeSort (a, aux, 0, 10) mergeSort (a, aux, 10, 20)

aux 59 46 32 80 46 55 87 43 44 81 95 12 17 80 75 33 40 61 16 50

mergeSort mergeSort mergeSort mergeSort(aux, a, 0, 5) (aux, a, 5, 10) (aux, a, 10, 15) (aux, a, 15, 20) Insertion Sort Insertion Sort Insertion Sort Insertion Sort

a 32 46 46 59 80 43 44 55 81 87 12 17 75 80 95 16 33 40 50 61

Page 95: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

a 32 46 46 59 80 43 44 55 81 87 12 17 75 80 95 16 33 40 50 61

merge merge

aux 32 43 44 46 46 55 59 80 81 84 12 16 17 33 40 50 61 75 80 95

merge

a 12 16 17 32 33 40 43 44 46 46 50 55 59 61 75 80 80 81 84 87 95

Page 96: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

private static void mergeSort (Object src[ ], Object dest[ ], int low, int high)

{ int length = high – low; // Use Insertion Sort for small subarrays. if (length < 7) { for (int i = low; i < high; i++) for (int j = i; j >low &&

((Comparable)dest[j-1]).compareTo(dest[j]) > 0; j--) swap (dest, j, j-1); return; } // if length < 7

Page 97: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

// Sort left and right halves of src into dest. int mid = (low + high) >> 1; mergeSort (dest, src, low, mid); mergeSort (dest, src, mid, high); // If left subarray less than right subarray, copy src to dest. if (((Comparable)src [mid-1]).compareTo (src [mid]) <= 0) { System.arraycopy (src, low, dest, low, length); return; } // Merge sorted subarrays in src into dest. for (int i = low, p = low, q = mid; i < high; i++) if (q>=high || (p<mid &&

((Comparable)src[p]).compareTo (src[q])<= 0)) dest [i] = src [p++]; else dest[i] = src[q++]; } // method mergeSort

Page 98: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

 

max comparisons n

n / 2  n / 2

n / 4 n / 4 n / 4 n / 4

n / 4 n / 4 n / 4 n / 4 n

n / 2  n / 2 n

n n

Page 99: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

 

 

For merge sort, worstTime(n) is O(n log n)

THERE FORE

averageTime(n) is linear-logarithmic in n.

Page 100: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Exercise: Show the steps, including the arguments in the calls to mergeSort, to merge sort the following:

aux 27 26 25 … 2 1 mergeSort (aux, a, 0, 27) a

Page 101: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Arrays.java also a version of Merge Sort that takes a Comparator parameter: public static <T> void sort(T[ ] a,Comparator<? super T> c)

An example of a call to this version: Arrays.sort (myArray, new ByLength());

Page 102: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Collections.java also has two versions of Merge Sort: One to sort List<T> in the natural order, and one to sort List<T> that has a second parameter of type Comparator. Collections.sort (myList); Collections.sort (myList, new Reverse());

Page 103: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

 

 

/** * Sorts a into ascending order. * The worstTime(n) is O(n * n), and averageTime(n) is * O(n log n). */ public static void sort (int[ ] a) { 

sort1(a, 0, a.length); } // method sort  

Quick Sort

Page 104: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

/**

* Sorts the array x, from index off (inclusive) to index

* off + len (exclusive), into ascending order.

*/

private static void sort1(int x[ ], int off, int len)

Page 105: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

If len < 7, use insertion sort.

Otherwise, partition the array x about a pivot element into two sub-arrays.

After partitioning, each element in the left sub-array will be less than or equal to the pivot and each element in the right sub-array will be greater than or equal to the pivot.

For now take the pivot to be the median of the first, middle and last elements.

Page 106: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

59 46 32 80 46 55 87 43 44 81 95 12 17 80 75 33 40 61 16 50 v = pivot = median of x [0], x [0 + 20 / 2], x [19]

= median of {59, 95, 50} = ?

Page 107: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

In a loop, we will partition x into a left subarray of items <= v and a right subarray of items >= v. We then recursively call sort1 for the left and right subarrays. Here are the basic loop and calls:

Page 108: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

int v = x[m]; // v is the pivot

int b = off, c = off + len - 1;while(true) {

while (x[b] < v) b++; while (x[c] > v) c--;

if (b > c) break;

swap(x, b++, c--);}if (c – off + 1 > 1)

sort1 (x, off, c - off + 1);if (off + len – b > 1)

sort1 (x, b, off + len - b);

Page 109: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

59 46 32 80 46 55 87 43 44 81 95 12 17 80 75 33 40 61 16 50 b c Increment b (zero times) until x [b] >= 59; decrement c (zero times) until x [c] =< 59. Then swap x [b] with x [c] and bump b and c.

Page 110: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

50 46 32 80 46 55 87 43 44 81 95 12 17 80 75 33 40 61 16 59 b c

Page 111: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

50 46 32 16 46 55 87 43 44 81 95 12 17 80 75 33 40 61 80 59 b c

Page 112: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

50 46 32 16 46 55 40 43 44 81 95 12 17 80 75 33 87 61 80 59 b c

Page 113: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

50 46 32 16 46 55 40 43 44 33 95 12 17 80 75 81 87 61 80 59 b c

Page 114: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

50 46 32 16 46 55 40 43 44 33 17 12 95 80 75 81 87 61 80 59 bc

Page 115: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

50 46 32 16 46 55 40 43 44 33 17 12 95 80 75 81 87 61 80 59 c b c = 11 and b = 12 Every element in x [0 … 11] <=59 Every element in x [12 … 19] >= 59

Page 116: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

So we now call:

sort1 (x, 0, 12);

sort1 (x, 12, 8);

Page 117: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For a refinement, elements equal to the pivotwill be moved to locations between c and b,so they are not involved in any furtherpartitioning.

During partitioning, elements equal to thepivot are stored at either end of the subarray,and then moved to the middle afterpartitioning.

Page 118: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

int v = x[m];// Establish Invariant: v* (<v)* (>v)* v*int a = off, b = a, c = off + len - 1, d = c;while(true) {

while (b <= c && x[b] <= v) {if (x[b] == v)

swap(x, a++, b);b++;

} while (c >= b && x[c] >= v) {

if (x[c] == v) swap(x, c, d--);

c--; } if (b > c)

break; swap(x, b++, c--);

}

Page 119: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

59 46 59 80 46 55 87 43 44 81 95 12 17 80 75 33 40 59 16 50 b c

Page 120: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

59 59 46 50 46 55 16 43 44 40 33 12 17 80 75 95 81 80 87 59 c b

Page 121: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

12 17 46 50 46 55 16 43 44 40 33 59 59 59 75 95 81 80 87 80

sort1 (x, 0, 11);sort1 (x, 14, 6);

Page 122: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For another refinement, suppose len, the size of the subarray to be sorted, is > 40.

To increase the likelihood that the pivot will be close to the median, split the subarray into 3 segments, and take the median of each segment. pivot = median of the 3 medians

Page 123: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For example, suppose we have len = 90. Then we split x up into subarrays x [0 … 29], x [30 … 59] and x [60 … 89].

element 55…87… 22 92…33…12 21…46…67 index 0 15 29 30 45 59 60 75 89

Page 124: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For example, suppose we have len = 90. Then we split x up into subarrays x [0 … 29], x [30 … 59] and x [60 … 89].

element 55…87… 22 92…33…12 21…46…67 index 0 15 29 30 45 59 60 75 89

55 33 46 46

Page 125: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

For the estimate of averageTime(n) and worstTime(n), we can imagine that Quick Sort creates a binary search tree. The root of the tree is the pivot. After the first partitioning, the pivot of the left subarray becomes the root of the left subtree, and so on.

Page 126: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Suppose x = {0, 1, 2, 3, … 98, 99} Already in order! 50

25 75 12 38 63 88 6 19 32 44 57 69 82 94 Best-Case Partitioning

Page 127: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Best-Case Partitioning:

log2(n/7) levels; at most n comparisons per level

Total number of comparisons

<= n log2n

Page 128: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Average-Case Partitioning:

Average height of a binary search tree: logarithmic in n

log2(n/7) levels; at most n comparisons

per level

Total number of comparisons n log2n

The averageTime(n): linear logarithmic in n.

Page 129: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Worst-Case Partitioning?

Page 130: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Suppose x = {1, 2, . . . , 18, 0, 38, 19, 20, 21, . . . , 37}

37 # of comparisons

1 38 39

0 35 37

3 36 35 2 33 33

5 34 31

Page 131: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Worst-Case Partitioning: # of comparisons n + (n-2) + (n-4) + … + 8

n/2

= Σ (2i) 2 * (n/2 * (n/2 + 1) / 2) i=4 n2 / 4

Page 132: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

The worstTime(n) is quadratic in n.

Page 133: Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 2 4 1 3 第 0 回合 第 1 回合 2 4 13 2 4 31 3 4 21 第 n-2 回合 4 3 21 min

Exercise: Suppose x starts out as 10, 9, 8, 7, 6, 16, 17, 18, 19, 20, 5, 4, 3, 2, 1, 11, 12, 13, 14, 15 the median of x [0], x [10], x [19] = 10. Just before the first swap of x[b] and x[c], b = 5 and c = 14. After all swaps, what does x contain? Don’t forget to move the pivot to the middle. What are the recursive calls to Quick Sort: sort1 (x, ?, ?); sort1 (x, ?, ?);