Valeri173

Сортиране

May 17th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.*;
  2. public class Main8 {
  3.  
  4.     public static void QuickSort(int[] arr, int low, int high) {
  5.         if(arr == null || arr.length == 0)
  6.             return;
  7.         if(low >= high)
  8.             return;
  9.  
  10.         int middle = low + (high - low) / 2;
  11.         int pivot = arr[middle];
  12.  
  13.         int i = low, j = high;
  14.         while (i <= j){
  15.             while (arr[i] < pivot){
  16.                 i++;
  17.             }
  18.             while (arr[j] > pivot){
  19.                 j--;
  20.             }
  21.             if(i <= j){
  22.                 int temp = arr[i];
  23.                 arr[i] = arr[j];
  24.                 arr[j] = temp;
  25.                 i++;
  26.                 j--;
  27.             }
  28.         }
  29.         if(low < j)
  30.             QuickSort(arr, low, j);
  31.         if(high > i)
  32.             QuickSort(arr, i, high);
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.  
  37.  
  38.         Scanner scan = new Scanner(System.in);
  39.         int N = scan.nextInt();
  40.  
  41.         int arr[] = new int[N];
  42.         for (int a = 0; a < N; a++) {
  43.             arr[a] = scan.nextInt();
  44.         }
  45.  
  46.         int low = 0;
  47.         int high = arr.length - 1;
  48.  
  49.         QuickSort(arr, low, high);
  50.  
  51.         System.out.println(Arrays.toString(arr));
  52.     }
  53. }
Add Comment
Please, Sign In to add comment