YORDAN2347

QuickSortMethods

Jan 15th, 2021 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. //https://pastebin.com/2mfpRaFT  ----> QuickSortMain
  2.  
  3. public class QuickSortMethods
  4. {
  5.     public void quickSort(int[] arr, int low, int high)
  6.     {
  7.         if (low < high)
  8.         {
  9.             int pivot = partition(arr, low, high);
  10.  
  11.             quickSort(arr, low, pivot-1);
  12.             quickSort(arr, pivot+1, high);
  13.         }
  14.  
  15.     }
  16.  
  17.     private int partition(int[] arr, int low, int high)
  18.     {
  19.         int pivot = arr[high];
  20.         int i = low -1;
  21.         for (int j = low; j < high; j++)
  22.         {
  23.             if (arr[j] < pivot)
  24.             {
  25.                 i++;
  26.                 //swap
  27.                 int temp = arr[i];
  28.                 arr[i] = arr[j];
  29.                 arr[j] = temp;
  30.             }
  31.         }
  32.         int temp =arr[high];
  33.         arr[high] = arr[i+1];
  34.         arr[i+1] = temp;
  35.         return i+1;
  36.     }
  37. }
  38.  
Add Comment
Please, Sign In to add comment