Advertisement
Ivan18113

Алгоритми - Тема 14 (QuickSortМethods)

Jan 15th, 2021
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. package Quick_Sort_181;
  2.  
  3. public class QuickSortMethods {
  4.  
  5.     public static void quickSort(int[] arr, int low, int high){
  6.  
  7.         if(low < high) {
  8.  
  9.             int pi = partition(arr, low, high);
  10.  
  11.             quickSort(arr, low, pi - 1 );
  12.             quickSort(arr, pi + 1, high);
  13.  
  14.            }
  15.         }
  16.  
  17.         public static int partition(int[] arr, int low, int high){
  18.         int pivot = arr[high];
  19.         int i = low - 1;
  20.  
  21.             for (int j = low; j < high; j++) {
  22.  
  23.                 if(arr[j] < pivot){
  24.  
  25.                     i++;
  26.                     //swap
  27.                     int temp = arr[j];
  28.                     arr[j] = arr[i];
  29.                     arr[i] = temp;
  30.                 }
  31.             }
  32.  
  33.             //put the pivot element at right position
  34.             int temp = arr[high];
  35.             arr[high] = arr[i+1];
  36.             arr[i+1] = temp;
  37.  
  38.         return i + 1;
  39.         }
  40.  
  41.     public static void main(String[] args) {
  42.  
  43.  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement