Advertisement
Kriss_7777

Quick_Sort_01

Dec 5th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. // QUICK SORT
  5.  
  6. class QuickSort01
  7. {
  8.     /* This function takes last element as pivot,
  9.        places the pivot element at its correct
  10.        position in sorted array, and places all
  11.        smaller (smaller than pivot) to left of
  12.        pivot and all greater elements to right
  13.        of pivot */
  14.    
  15.    
  16.     // PARTITIONI -------------------------------------------------------------/////
  17.     int partition(int arr[], int low, int high)
  18.     {
  19.         int pivot = arr[high];  
  20.         int i = (low-1); // index of smaller element
  21.         for (int j=low; j<high; j++)
  22.         {
  23.            
  24.             // If current element is smaller than the pivot
  25.             //..............................................
  26.             if (arr[j] < pivot)
  27.             {
  28.                 i++;
  29.  
  30.                 // swap arr[i] and arr[j]
  31.                 //.......................
  32.                 int temp = arr[i];
  33.                 arr[i] = arr[j];
  34.                 arr[j] = temp;
  35.             }
  36.         }
  37.  
  38.  
  39.        
  40.         // swap arr[i+1] and arr[high] (or pivot)
  41.         //........................................
  42.         int temp = arr[i+1];
  43.         arr[i+1] = arr[high];
  44.         arr[high] = temp;
  45.  
  46.         return i+1;
  47.     }
  48.  
  49.  
  50.    
  51.     /* The main function that implements QuickSort()
  52.       arr[] --> Array to be sorted,
  53.       low  --> Starting index,
  54.       high  --> Ending index */
  55.    
  56.    
  57.    
  58.    
  59.     // QUICK SORT -------------------------------------------------------------/////
  60.     void sort(int arr[], int low, int high)
  61.     {
  62.         if (low < high)
  63.         {
  64.            
  65.             /* pi is partitioning index, arr[pi] is  
  66.               now at right place */
  67.             int pi = partition(arr, low, high);
  68.  
  69.            
  70.             // Recursively sort elements before
  71.             // partition and after partition
  72.             sort(arr, low, pi-1);
  73.             sort(arr, pi+1, high);
  74.         }
  75.     }
  76.    
  77.    
  78.    
  79.  
  80.     // UTILITIES --------------------------------------------------------------/////
  81.     /* A utility function to print array of size n */
  82.     static void printArray(int arr[])
  83.     {
  84.        // int n = arr.length;
  85.         // for (int i=0; i<n; ++i)
  86.         //    System.out.print(arr[i]+" ");
  87.        
  88.         System.out.println(Arrays.toString(arr));
  89.     }
  90.  
  91.    
  92.    
  93.    
  94.     // MAIN program ===========================================================
  95.     public static void main(String args[])
  96.     {
  97.         Scanner input = new Scanner(System.in);
  98.         System.out.print("Input the number of elements : ");
  99.         int N = input.nextInt();
  100.        
  101.        
  102.         int arr[] = new int[N];
  103.        
  104.        
  105.         for(int a = 0; a < N; a++)
  106.         {
  107.             System.out.printf("Input the [%d] element : ", a);
  108.             arr[a] = input.nextInt();
  109.         }
  110.        
  111.        
  112.        
  113.         int n = arr.length;
  114.  
  115.         System.out.println();
  116.         System.out.println("Array BEFORE Quick Sort : ");
  117.         printArray(arr);
  118.         System.out.println();
  119.        
  120.        
  121.         QuickSort01 sorter = new QuickSort01();
  122.        
  123.         sorter.sort(arr, 0, n-1);
  124.        
  125.         // sort(arr, 0, n-1); // If you want to use sort "directly" have to change
  126.         // SORT-method and PARTITION-method to STATIC
  127.        
  128.        
  129.  
  130.         System.out.println("Array AFTER Quick Sort : ");
  131.         printArray(arr);
  132.        
  133.        
  134.         input.close();
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement