Advertisement
uopspop

Untitled

May 22nd, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. package sorting;
  2.  
  3. public class QuickSort_V3 {
  4. public static void main(String[] args)
  5. {
  6. // int[] ary = new int[]{40,3,59, -1, -999,4,6,99,888,7};
  7. int ary[] = {12,30,4,99,98,1,3,10,23,45,75,69,31,88,97,14,29,91,2,0,77};
  8.  
  9. printAry(ary, 0, ary.length-1);
  10. System.out.println(" - Original");
  11. myQuickSort(ary, 0, ary.length-1);
  12.  
  13. printAry(ary, 0, ary.length-1);
  14. System.out.println(" - Sorted");
  15. }
  16.  
  17. static void myQuickSort(int[] ary, int startIndex, int endIndex){
  18. // if the range of the sub-ary is empty or only has one element, there is no need to sort at all
  19. if (startIndex >= endIndex) {
  20. return;
  21. }
  22.  
  23. // do the sorting
  24. Integer pivotIndex = myQuickSortHelper(ary, startIndex, endIndex);
  25.  
  26. // divide the arrays into two sub-arrays and use the same sorting algorithm again
  27. myQuickSort(ary, pivotIndex+1, endIndex); // right side
  28. myQuickSort(ary, startIndex, pivotIndex-1); // left side
  29. }
  30.  
  31. static Integer myQuickSortHelper(int[] ary, int startIndex, int endIndex){
  32.  
  33. // the initial last smaller value index for this round
  34. int lastSmallerValueIndex = startIndex - 1;
  35.  
  36. // execute the logic of quick sort
  37. int pivotVal = ary[endIndex]; // we pick the last element of the current range of sub-array as our pivot value
  38. System.out.printf("========= pivot - index: %d val: %d %n", endIndex, pivotVal);
  39. // System.out.printf("leftIndex: %d rightIndex: %d %n", startIndex, endIndex);
  40. for (int i = startIndex; i < endIndex; i++){
  41. int nowVal = ary[i];
  42. // System.out.printf("nowVal: %d - pivotVal: %d --- nowVal <= pivotVal: %b %n", nowVal, pivotVal, nowVal <= pivotVal);
  43. if (nowVal <= pivotVal) { // we found there is one number that is lower than the pivotValue, so we simply put it the the left side of the array
  44. lastSmallerValueIndex++;
  45. swapValInAray(ary, lastSmallerValueIndex, i);
  46. printAry(ary, startIndex, endIndex);
  47. System.out.println();
  48. }
  49. }
  50.  
  51. // put pivotValue to the pivotIndex
  52. int pivotIndex = lastSmallerValueIndex + 1;
  53. swapValInAray(ary, pivotIndex, endIndex);
  54. printAry(ary, startIndex, endIndex);
  55. System.out.println(" - put pivotValue to the pivotIndex ");
  56. return pivotIndex; // this is the index that is set to have the current pivotVal forever
  57. }
  58.  
  59. private static void swapValInAray(int[] ary, int i, int j){
  60. int tmp = ary[i];
  61. ary[i] = ary[j];
  62. ary[j] = tmp;
  63.  
  64. // printAry(ary);
  65. }
  66.  
  67. private static void printAry(int[] ary){
  68. for (int i = 0; i < ary.length; i++){
  69. System.out.print(ary[i] + " ");
  70. }
  71. System.out.println();
  72. }
  73.  
  74. private static void printAryWithNewLine(int[] ary, int start, int end){
  75. printAry(ary, start, end);
  76. System.out.println();
  77. }
  78. private static void printAry(int[] ary, int start, int end){
  79. for (int i = 0; i < ary.length; i++){
  80. if (start <= i && i <= end)
  81. System.out.printf("%2d " , ary[i]);
  82. else
  83. System.out.printf("%2s " , " ");
  84. }
  85. // System.out.println();
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement