1 / 135

Quick Sort Algorithm

Learn the basic idea of the Quick Sort algorithm and how to implement it in Java. Compare it to Bubble Sort and Selection Sort.

bsimms
Download Presentation

Quick Sort Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11 Sorting

  2. Quick Sort

  3. Basic Idea 假設 n elements 的中間值 在正中間 叫 pivot (樞紐) 如何使pivot以左的都小於pivot 而pivot以右的都大pivot? 我是李二二 我是張一二 凡我跑過 凡我跑過 皆小於 pivot皆大於 pivot pivot swap 50 n/2 elements > pivot n/2 elements < pivot 下一回合 n/4 elements 直到 n = 1 時間 O(log n)

  4. Big O issue • 上頁假設 n elements 的中間值剛好位於正中間 但這是不大可能的 • 實務上 可任取三個值 以其中間值為pivot 或取最左值為 pivot(如下頁演算法) 最差情況是 取了最小值為 pivot 則問題從 n變 n-1而非 n/2 則需 O(n) 時間

  5. 快速排序法 Quick Sort 如果 a 為空或 left>=right 則結束 left right 令 a[left] 為pivot樞紐(即3) pivot • Quicksort example j i n-2 2 0 index 1 n-1 令 i 為left+1, j為right a 3 8 1 4 2 1. while a[i] < pivot, i向右移 2. while a[j] >= pivot, j向左移 3. if (i<j), a[i] 和 a[j] 互換 3 8 1 4 2 repeat until (i>j) left right right left a[left] 和 a[j] 互換 1 2 3 4 8 quickSort(a, left, j-1) quickSort(a, j+1, right) result 1 2 3 4 8

  6. 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); }// end of quickSort()

  7. 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 and right > 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);} }

  8. 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; }

  9. 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); }

  10. Bubble Sort

  11. 2 2 4 4 n-1 index 4 4 2 3 n-2 1 1 3 2 1 3 3 1 1 0 第1回合 a[1]到定位 第n-1回合 a[n-1]到定位 最大氣泡 最小氣泡 4 3 2 1

  12. 每回合處理 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]); } // end of bubbleSort() swap(int a, int b) { int temp; temp=a; a=b; b=temp; } // end of swap()

  13. Bubble.java /** * @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 */ publicvoid bubbleSort(int[] array) { for(inti = array.length-1; i >= 1; i--) { for(intj = 0; j <= i-1; j++) { if(array[j] > array[j+1]) array = swap(array, array[j],array[j+1]); } } returnarray; }

  14. Bubble.java(continued) /* Print the array of this object*/ publicvoid print() { for(inti = 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 */ publicint[] swap(int[] input, inta, intb) { inttemp = input[a]; input[a] = input[b]; input[b] = temp; returninput; } publicint[] getArray() { returnarray; } publicvoid setArray(int[] array) { this.array = array; }

  15. Bubble.java(continued) /** * The main method, program start from here * @param args */ publicstaticvoid main(String[] args) { int[] x = { 2, 4, 1, 3 }; print(x); bubbleSort(x); print(x); }

  16. Selection Sort

  17. index 0 1 n-2 n-1 4 2 3 1 min 2 4 3 1 第0回合 min 第1回合 4 2 1 3 min 4 第n-2回合 3 1 2 3 4 1 2

  18. 每回合處理 a[i … n-1](i 從 0 到 n-2) select min,與 a[ i ] swap selectionSort ( int[n] a) { for( i = 0, i <= n-2, i++){ /*select min */ min = i ; for ( j = i, j <= n-1, j++) if a[ j + 1] < a[min] min=j+1; swap ( a[min], a[ i ]); } } // end of selectionSort()

  19. Selection.java /** * @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 */ publicint[] 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); } returnarray; }

  20. Selection.java(continued) /** * Print the array of this object */ publicvoid 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 */ publicint[] swap(int[] input, int a, int b) { int temp = input[a]; input[a] = input[b]; input[b] = temp; return input; } publicint[] getArray() { returnarray; } publicvoid setArray(int[] array) { this.array = array; } }

  21. 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 */ publicstaticvoid main(String[] args) { int[] x = { 3, 1, 4, 2 }; print(x); selectionSort(x); print(x); }

  22. Insertion Sort

  23. index 1 n-2 n-1 0 3 1 4 2 unsorted未排序 sorted 排序好 4 2 1 3 第1回合 2 4 3 第二回合 1 2 1 4 3 第 n-1回合 1 2 3 4

  24. 每回合處理未排序的a[i … n-1](i 從 1到 n-1) 把a[i] insert 到左邊排序好的適當位置 (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; } // end of insertionSort() swap(int a, int b) { int temp; temp=a;a=b;b=temp; }

  25. Insertion.java /** * @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 */ publicvoid insertionSort(int[] array) { for(inti = 1; i <= array.length-1; i++) { for(intj = i; j >= 1; j--) { if(array[j] < array[j-1]) array = swap(array, array[j],array[j-1]); } } returnarray; }

  26. Insertion.java(continued) /* Print the array of this object*/ publicvoid print() { for(inti = 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 */ publicint[] swap(int[] input, inta, intb) { inttemp = input[a]; input[a] = input[b]; input[b] = temp; returninput; } publicint[] getArray() { returnarray; } publicvoid setArray(int[] array) { this.array = array; }

  27. Insertion.java(continued) /** * The main method, program start from here * @param args */ publicstaticvoid main(String[] args) { int[] x = { 3, 1, 4, 2 }; print(x); insertionSort(x); print(x); }

More Related