Advertisement
Kriss_7777

Quick_Sort_02

Nov 15th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class QuickSort02
  4. {
  5.      
  6.     private int array[];
  7.     private int length;
  8.  
  9.    
  10.    
  11.    
  12.     // sort ...................................................................
  13.     public void sort(int[] inputArr)
  14.     {
  15.          
  16.         if (inputArr == null || inputArr.length == 0)
  17.         {
  18.             return;
  19.         }
  20.        
  21.         this.array = inputArr;
  22.         this.length = inputArr.length;
  23.        
  24.         quickSort(0, length - 1);
  25.     }
  26.    
  27.    
  28.    
  29.    
  30.     // QuickSort...............................................................
  31.     private void quickSort(int lowerIndex, int higherIndex)
  32.     {
  33.          
  34.         int i = lowerIndex;
  35.         int j = higherIndex;
  36.        
  37.        
  38.         // calculate pivot number, I am taking pivot as middle index number
  39.         int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
  40.        
  41.        
  42.         // Divide into two arrays
  43.         while (i <= j) {
  44.            
  45.             /*
  46.              * In each iteration, we will identify a number from left side which
  47.              * is greater then the pivot value, and also we will identify a number
  48.              * from right side which is less then the pivot value. Once the search
  49.              * is done, then we exchange both numbers.
  50.  
  51.              * При всяка итерация намираме числото от ДЯСНО, което е по-голямо
  52.              * от главния (pivot) елемента и също намираме числото от ЛЯВО,
  53.              * което е по-малко от главния елемнт.
  54.              * След като приключим с търсенето разменяме местата на двата елемента
  55.              *
  56.              */
  57.            
  58.             while (array[i] < pivot)
  59.             {
  60.                 i++;
  61.             }
  62.            
  63.             while (array[j] > pivot)
  64.             {
  65.                 j--;
  66.             }
  67.            
  68.            
  69.             if (i <= j)
  70.             {
  71.                 exchangeNumbers(i, j);
  72.                 //move index to next position on both sides
  73.                 i++;
  74.                 j--;
  75.             }
  76.         }
  77.        
  78.        
  79.        
  80.         // call quickSort() method RECURSIVELY
  81.         if (lowerIndex < j)
  82.             quickSort(lowerIndex, j);
  83.        
  84.         if (i < higherIndex)
  85.             quickSort(i, higherIndex);
  86.     }
  87.  
  88.  
  89.    
  90.     // Exchange numbers .......................................................
  91.     private void exchangeNumbers(int i, int j)
  92.     {
  93.         int temp = array[i];
  94.         array[i] = array[j];
  95.         array[j] = temp;
  96.     }
  97.      
  98.    
  99.    
  100.     // MAIN ////////////////////////////////////////////////////////////////////
  101.     public static void main(String args[])
  102.     {
  103.          
  104.         QuickSort02 sorter = new QuickSort02();
  105.        
  106.        
  107.         Scanner sc = new Scanner(System.in);
  108.         System.out.print("Input the number of elements : ");
  109.         int N = sc.nextInt();
  110.        
  111.        
  112.         int arr[] = new int[N];
  113.        
  114.         for(int a = 0; a < N; a++)
  115.         {
  116.             System.out.printf("Input the [%d] element : ", a);
  117.             arr[a] = sc.nextInt();
  118.         }
  119.        
  120.    
  121.         System.out.println();
  122.         // Print before sorting................................................
  123.         System.out.println("Before Sorting : ");
  124.         for(int i:arr)
  125.         {
  126.             System.out.print(i);
  127.             System.out.print(" ");
  128.         }
  129.         System.out.println();
  130.        
  131.        
  132.         sorter.sort(arr);
  133.        
  134.        
  135.         System.out.println();
  136.         // Print after sorting.................................................
  137.         System.out.println("After Sorting : ");
  138.         for(int i:arr)
  139.         {
  140.             System.out.print(i);
  141.             System.out.print(" ");
  142.         }
  143.        
  144.         sc.close();
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement