Advertisement
Guest User

Quick Sort 17226

a guest
Nov 19th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. class QuickSort {
  5.     /* This function takes last element as pivot,
  6.        places the pivot element at its correct
  7.        position in sorted array, and places all
  8.        smaller (smaller than pivot) to left of
  9.        pivot and all greater elements to right
  10.        of pivot */
  11.     int partition(int arr[], int low, int high) {
  12.         int pivot = arr[high];
  13.         int i = (low - 1); // index of smaller element
  14.         for (int j = low; j < high; j++) {
  15.             // If current element is smaller than the pivot
  16.             if (arr[j] < pivot) {
  17.                 i++;
  18.  
  19.                 // swap arr[i] and arr[j]
  20.                 int temp = arr[i];
  21.                 arr[i] = arr[j];
  22.                 arr[j] = temp;
  23.             }
  24.         }
  25.  
  26.         // swap arr[i+1] and arr[high] (or pivot)
  27.         int temp = arr[i + 1];
  28.         arr[i + 1] = arr[high];
  29.         arr[high] = temp;
  30.  
  31.         return i + 1;
  32.     }
  33.  
  34.  
  35.     /* The main function that implements QuickSort()
  36.       arr[] --> Array to be sorted,
  37.       low  --> Starting index,
  38.       high  --> Ending index */
  39.     void sort(int arr[], int low, int high) {
  40.         if (low < high) {
  41.             /* pi is partitioning index, arr[pi] is
  42.               now at right place */
  43.             int pi = partition(arr, low, high);
  44.  
  45.             // Recursively sort elements before
  46.             // partition and after partition
  47.             sort(arr, low, pi - 1);
  48.             sort(arr, pi + 1, high);
  49.         }
  50.     }
  51.  
  52.     // Driver program
  53.     public static void main(String args[]) {
  54.         Scanner scan = new Scanner(System.in);
  55.         int N = 0;
  56.  
  57.         System.out.printf("Input the number of elements N : "); // Input the number of elements in the array
  58.         N = scan.nextInt();
  59.  
  60.         int arr[] = new int[N];
  61.  
  62.         for (int i = 0; i < N; i++) {
  63.             System.out.printf("Please input the [%d] element : ", i); // Input the elements of the array
  64.             arr[i] = scan.nextInt();
  65.         }
  66.  
  67.         System.out.println();
  68.         System.out.println("Array Before Quick Sort : ");
  69.  
  70.         System.out.println("Array: " + Arrays.toString(arr)); // Print the array before Quick Sort
  71.         System.out.println();
  72.  
  73.             QuickSort ob = new QuickSort();
  74.             ob.sort(arr, 0, N - 1);
  75.  
  76.         System.out.println();
  77.  
  78.         System.out.println("Array After Quick Sort : ");
  79.         System.out.println("Array: " + Arrays.toString(arr)); // Print the array after Quick Sort
  80.         System.out.println();
  81.         }
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement