排序

来源:互联网 发布:java直播系统 编辑:程序博客网 时间:2024/06/05 06:58
public class QuickSort {    public int[] sort(int[] A, int n){        if (n == 0 || A == null) {            return null;        }        quickSort(A, 0, n - 1);        return A;    }    public void quickSort(int[] a,int left,int right){        int i,j,t,temp;        if (left > right){            return;        }        temp = a[left];        i = left;        j = right;        while(i != j){            while (a[j]>=temp && i<j){                j--;            }            while (a[i]<=temp && i<j){                i++;            }            if (i<j){                t = a[i];                a[i]=a[j];                a[j]=t;            }        }        a[left] = a[i];        a[i] = temp;        quickSort(a,left,i-1);        quickSort(a,i+1,right);    }    public static void bubbleSort(int[] a){        for (int i=0;i<a.length-1;i++){            for (int j=0;j<a.length-1;j++){                if(a[j]<a[j+1]){                    int temp = a[j];                    a[j]=a[j+1];                    a[j+1] = temp;                }            }        }    }    public static void main(String[] args) {        int[] a = {54, 35, 48, 36, 27, 12, 44, 44, 8, 14, 26, 17, 28};        //int[] b = new QuickSort().sort(a,13);        bubbleSort(a);        for (int i=0;i<a.length-1;i++){            System.out.print(a[i]+ " ");        }    }}
0 0