JAVA快速排序

来源:互联网 发布:阿里云备案要多长时间 编辑:程序博客网 时间:2024/06/04 18:18

public class kS {

public static void disp(int a[],int n){    for(int i=0;i<n;i++){        System.out.printf("%2d",a[i]);    }    System.out.println();}private static int Partition(int[] a, int s, int t) {    int i = s,j =t;    int tmp = a[s];  //用第一个记录作为基准    while(i!=j){        //从序列两端交替向中间扫描 ,直到 i = j为止        while(j>i && a[j]>=tmp){              j--;        }        a[i] = a[j];        while(i<j && a[i]<=tmp){            i++;        }        a[j] = a[i];    }    a[i] = tmp;    return i;}public static void QuickSort(int a[],int s,int t){    int i;    if(s<t){        i = Partition(a,s,t);        QuickSort(a,s,i-1);        QuickSort(a,i+1,t);    }}public static void main(String[] args) {    int n = 10;    int []a = {2,4,6,3,5,9,1,7,8,0};    System.out.println("排序前:");    disp(a,n);    QuickSort(a,0,n-1);    System.out.println("排序后:");    disp(a,n);}

}

0 0
原创粉丝点击