Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main8 {
- public static void QuickSort(int[] arr, int low, int high) {
- if(arr == null || arr.length == 0)
- return;
- if(low >= high)
- return;
- int middle = low + (high - low) / 2;
- int pivot = arr[middle];
- int i = low, j = high;
- while (i <= j){
- while (arr[i] < pivot){
- i++;
- }
- while (arr[j] > pivot){
- j--;
- }
- if(i <= j){
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- i++;
- j--;
- }
- }
- if(low < j)
- QuickSort(arr, low, j);
- if(high > i)
- QuickSort(arr, i, high);
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int N = scan.nextInt();
- int arr[] = new int[N];
- for (int a = 0; a < N; a++) {
- arr[a] = scan.nextInt();
- }
- int low = 0;
- int high = arr.length - 1;
- QuickSort(arr, low, high);
- System.out.println(Arrays.toString(arr));
- }
- }
Add Comment
Please, Sign In to add comment