MilaDimitrovaa

QuickSort - Method

Jan 8th, 2021
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. package QuickSort_183_02;
  2.  
  3.  public class QuickSort_Method {
  4.      public static void QuickSort(int[] arr, int low, int high) {
  5.  
  6.          if(low < high){
  7.  
  8.              int PivotIndex = Partition(arr,low,high);
  9.  
  10.              QuickSort(arr,low,PivotIndex - 1 ); //Recursively call for left part
  11.              QuickSort(arr,PivotIndex + 1, high);//Recursively call for left part
  12.  
  13.          }
  14.  
  15.      }
  16.  
  17.      private static int Partition(int[] arr, int low, int high) {
  18.  
  19.          int i = (low - 1 ); // Index of current element which we have to swap
  20.          int pivot = arr[high];
  21.  
  22.          for (int j = low; j < high  ; j++) {
  23.  
  24.              if(arr [j] < pivot){
  25.                  i++;
  26.                  int TEMP = 0;
  27.                  // Spaw if the current element is smaller  than pivot
  28.                  TEMP = arr[i];
  29.                  arr[i] = arr [j];
  30.                  arr[j] = TEMP;
  31.  
  32.  
  33.              }
  34.          }
  35.  
  36.          int TEMP;
  37.          TEMP = arr[i+1];
  38.          arr[i+1] = arr[high];
  39.          arr[high] = TEMP;
  40.  
  41.          return i + 1;
  42.  
  43.      }
  44.  
  45.  }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment