Download presentation
Presentation is loading. Please wait.
Published byDorthy Stevenson Modified over 9 years ago
1
Chapter 11 Sorting
2
Selection Sort
3
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
4
每回合處理 a[i … n-1] ,而 i 從 0 到 n-2 Select min ,與 a[ i ] swap 1. 令 a[ i ] 為 min 2. 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 ]); }
5
/** * @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
6
/** * 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)
7
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); }
8
Insertion Sort
9
3142 0 1n-2n-1 index 42 3 1 第一回合 第二回合 第 n-1 回合 1 3 2 4 1 3 4 2 1234
10
每回合處理 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; }
11
/** * @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
12
/* 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)
13
/** * 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)
14
Bubble Sort
15
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
16
每回合處理 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; }
17
/** * @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
18
/* 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)
19
/** * 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)
20
Quick Sort
21
快速排序法 Quick Sort Quicksort example pivot i j result 令 a[left] 為 pivot ( 即 3) a index 0 1 2 n-2 n-1 leftright 38142 令 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) 38142 a[left] 和 a[j] 互換 12348 leftright rightleft 如果 a 為空或 left>=right 則結束 12348
22
Quick Sort Algorithm quickSort(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); }
23
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 pivot int pivot = array[low]; //make left pivot int 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 parts if (low < j){quickSort(array, low, j);} if (high > i){quickSort(array, i, high);} }
24
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; }
25
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); }
98
max comparisons n n / 2 n / 2 n / 4 n / 4 … n / 4 n / 4 n / 4 n / 4n n / 2 n / 2n nn
99
For merge sort, worstTime(n) is O(n log n) THERE FORE averageTime(n) is linear-logarithmic in n.
103
/** * 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
104
/** * 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)
105
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.
108
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);
116
So we now call: sort1 (x, 0, 12); sort1 (x, 12, 8);
118
int v = x[m]; // Establish Invariant: 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--); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.