Guest User

Untitled

a guest
Apr 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3.  
  4. public class Quicksort {
  5.  
  6.     /**
  7.      * @param args
  8.      */
  9.     // use int or inline
  10.     static void swap(int[] arr, int i, int j){
  11.         int temp=arr[i];
  12.         arr[i] = arr[j];
  13.         arr[j] = temp;
  14.     }
  15.     static void quicksort(int[] arr, int left, int right){
  16.         if(right-left > 0){
  17.             int p = partition(arr, left, right);
  18.             quicksort(arr,left,p-1);
  19.             quicksort(arr, p+1, right);
  20.         }
  21.     }
  22.     static int partition(int[] arr, int left, int right){
  23.         int pivot = right;
  24.         int higherthanpivot=left;
  25.         for(int i=left; i<right;i++){
  26.             if(arr[i] < arr[pivot]){
  27.                 swap(arr, i, higherthanpivot);
  28.                 higherthanpivot++;
  29.             }
  30.         }
  31.         swap(arr,pivot,higherthanpivot);
  32.         return higherthanpivot;
  33.     }
  34.     public static void main(String[] args) {
  35.         int[] a= new int[]{2,2,2,3,54,1,12,3453,453,1,1,2,2,3,8,02,1,23};
  36.         quicksort(a,0,a.length-1);
  37.         System.out.println(Arrays.toString(a));
  38.        
  39.  
  40.     }
  41.  
  42. }
Add Comment
Please, Sign In to add comment