Quicksort + java implementation

import java.util.Arrays;

public  class QuickSort {
     // Three numbers are Chinese. Take out the big and small position 
    public  static  int getPivotPos( int [] a, int low, int high) {
         int mid=(low+high)/2 ;
         int pos= low;
         int minpos= low;
         int maxpos= low ;
         if (a[mid]<a[minpos]) minpos= mid;
         if (a[high]<a[minpos]) minpos= high;
         if (a[mid]>a[maxpos]) maxpos= mid;
         if (a[high]>a[maxpos]) maxpos=high;
        if(low!=minpos&&low!=maxpos) pos=low;
        if(mid!=minpos&&mid!=maxpos) pos=mid;
        if(high!=minpos&&high!=maxpos) pos=high;
        return pos;
    }
    // divide, take out the pivot position 
    public  static  int partition( int [] a, int low, int high) {
         int pivotpos= getPivotPos(a,low,high);
         int pivot= a[pivotpos];
         int temp= pivot;
        a[pivotpos]=a[low];
        a[low]=temp;
        while(low<high) {
            while(low<high&&a[high]>=pivot) high--;
                a[low]=a[high];
            while(low<high&&a[low]<=pivot)  low++;
                a[high]=a[low];
        }
        a[low]=pivot;
        return low;
    }
    //快排
    public static void quickSort(int[] a,int low,int high) { 
        if(low<high) {
            int pivotpos=partition(a,low,high);
            quickSort(a,low,pivotpos-1);
            quickSort(a,pivotpos+1,high);
        }
    }
    //重载快排
    public static void quickSort(int[] a) {
        if(a.length==0)
            return;
        int low=0;
        int high=a.length-1;
        quickSort(a,low,high);
    }
    
    public static void main(String[] args) {
        int[] a= {12,32,24,99,54,76,48};
        quickSort(a);
        System.out.println(Arrays.toString(a));
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325264020&siteId=291194637