uopspop

Untitled

May 22nd, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. package sorting;
  2.  
  3. public class QuickSortUoPeople {
  4.     private static boolean isDebugMode = false;
  5.     private static int exchangeCount = 0;
  6.     private static int compareCount = 0;
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         int ary[] = {12,9,4,99,120,1,3,10,23,45,75,69,31,88,101,14,29,91,2,0,77}; // course requirement
  11. //        int ary[] = {1,2,3,4,5,6,7,8,9,75,1,2,3,4,5,6,7,7,7,7}; // when pivot is the max one
  12. //        int ary[] = {100,102,103,104,105,106,75,102,103,102,101,100,104}; // when pivot is the min one
  13.  
  14.         System.out.println("size: " + ary.length);
  15.         System.out.println("Original: ");
  16.         printAry(ary, 0, ary.length-1);
  17.  
  18.         myQuickSort(ary, 0, ary.length-1);
  19.  
  20.         System.out.println("Sorted:");
  21.         printAry(ary, 0, ary.length-1);
  22.         System.out.println("compareCount: " + compareCount);
  23.         System.out.println("exchangeCount: " + exchangeCount);
  24.     }
  25.  
  26.     static void myQuickSort(int[] ary, final int startIndex, final int endIndex){
  27.         if (isDebugMode) System.out.println("STEP01 - check the current sub-array: ");
  28.         printAry(ary, startIndex, endIndex);
  29.  
  30.         // 1. find the pivot for the current sub-array
  31.         int pivotIndex = findPivotIndex(ary, startIndex, endIndex);
  32.         if (isDebugMode) System.out.printf("STEP02 - findPivotIndex ary[%d]:%d %n", pivotIndex, ary[pivotIndex]);
  33.  
  34.         // 2. move the pivot to the end of the current sub-array
  35.         if (isDebugMode) System.out.println("STEP03 - Move pivot to the last index:  - pivot: " + ary[pivotIndex]);
  36.         swapValInAray(ary, pivotIndex, endIndex);
  37.         pivotIndex = endIndex;
  38.         printAry(ary, startIndex, endIndex);
  39.  
  40.         // 3. puts things into left and right chunks
  41.         if (isDebugMode) System.out.println("STEP04 - Arrange into two sub-arrays: ");
  42.         int pivotFinalIndex = quickSortHelper(ary, startIndex, endIndex - 1, pivotIndex); // endIndex - 1 in order to remove pivot in the process
  43.  
  44.         // 4. in the end, return the final index which will be the index for the current pivot
  45.         if (isDebugMode) System.out.println("STEP05 - Swap the current pivot with the returned index - pivot: " + ary[pivotIndex]);
  46.         swapValInAray(ary, pivotIndex, pivotFinalIndex);
  47.         printAry(ary, startIndex, endIndex);
  48.         if (isDebugMode) System.out.println("END    =========================================================");
  49.  
  50.         if (pivotFinalIndex - 1 > startIndex) myQuickSort(ary, startIndex, pivotFinalIndex - 1);
  51.         if (pivotFinalIndex + 1 < endIndex) myQuickSort(ary, pivotFinalIndex + 1, endIndex);
  52.  
  53.  
  54.     }
  55.  
  56.     private static int quickSortHelper(int[] ary, int startIndex, int endIndex, int pivotIndex) {
  57.         int debugStartIndex = startIndex;
  58.         int debugEndIndex = endIndex;
  59.  
  60.         while(startIndex != endIndex){
  61.             // find one number from the left part which is > pivot
  62.             while(startIndex < endIndex && !(ary[startIndex] > ary[pivotIndex])) {
  63.                 startIndex++;
  64.                 compareCount++;
  65.             }
  66.             // find one number from the right part which is <= pivot
  67.             while(endIndex > startIndex && !(ary[endIndex] <= ary[pivotIndex])) {
  68.                 endIndex--;
  69.                 compareCount++;
  70.             }
  71.  
  72.             if (isDebugMode) System.out.printf("       ary[%d]=%d <-> ary[%d]=%d %n" , startIndex, ary[startIndex], endIndex, ary[endIndex]);
  73.  
  74.             swapValInAray(ary, startIndex, endIndex);
  75.             printAry(ary, debugStartIndex, debugEndIndex);
  76.  
  77.         } // end while
  78.  
  79.  
  80.         if (ary[startIndex] >= ary[pivotIndex]) return startIndex;
  81.         else {return startIndex + 1;}
  82. //
  83. //        return startIndex + 1;
  84.     }
  85.  
  86.     static int findPivotIndex(int[] ary, int startIndex, int endIndex) {
  87.         return (startIndex + endIndex)/2;
  88.     }
  89.  
  90.     private static void swapValInAray(int[] ary, int i, int j){
  91.         int tmp = ary[i];
  92.         ary[i] = ary[j];
  93.         ary[j] = tmp;
  94.  
  95.         exchangeCount++;
  96.  
  97. //        printAry(ary);
  98.     }
  99.  
  100.     private static void printAry(int[] ary, int start, int end){
  101.         System.out.print("       ");
  102. //        for (int i = start; i <= end; i++){
  103. //            System.out.print(ary[i] + " ");
  104. //        }
  105.         for (int i = 0; i < ary.length; i++){
  106.             if (start <= i && i <= end)
  107.                 System.out.printf("%3d " , ary[i]);
  108.             else
  109.                 System.out.printf("%3s " , " ");
  110.         }
  111.         System.out.println();
  112.     }
  113.  
  114.     private static void printAry(int[] ary){
  115.         for (int i = 0; i < ary.length; i++){
  116.             System.out.print(ary[i] + " ");
  117.         }
  118.         System.out.println();
  119.     }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment