Advertisement
TsetsoP

quicksort_methods

Jan 7th, 2021
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1. package quicksort;
  2.  
  3. public class quicksort_methods {
  4.  
  5.     public static void QuickSort(int[] ARR, int low, int high) {
  6.         if(low < high) {
  7.            
  8.             int pi = Partition(ARR, low, high);
  9.            
  10.             QuickSort(ARR, high,pi-1);
  11.             QuickSort(ARR, pi+1, high);
  12.            
  13.         }
  14.  
  15.     }
  16.  
  17.     private static int Partition(int[] ARR, int low, int high) {
  18.        
  19.         int pivot = ARR[high];
  20.         int i = low-1; // Index for iteration if pivot is bigger
  21.        
  22.         for(int j = 0; j < high; j++) {
  23.            
  24.             if(ARR[j] < pivot) {
  25.                
  26.                 i++;   
  27.                 int TEMP = ARR[i];
  28.                 ARR[i] = ARR[j];
  29.                 ARR[j] = TEMP;
  30.                
  31.             }
  32.            
  33.         }
  34.        
  35.         int TEMP = ARR[high];
  36.         ARR[high] = ARR[i + 1];
  37.         ARR[i + 1] = TEMP;
  38.        
  39.         return i + 1;
  40.        
  41.     }
  42.  
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement