Java常用排序算法

插入排序

public void InsertSort(int[] a){
    int temp;
    for(int i=1;i<a.length;i++){
            temp=a[i];
            int j=i-1;
            while(j>=0&&temp<a[j]){
                a[j+1]=a[j];
                 --j;    
             }
            a[j+1]=temp;
        }    
}

交换类排序

冒泡排序

    public void sort(int a[]){
    int temp;
    for(int i=0;i<a.length-1;i++){
        for(int j=0;j<(a.length-1-i);j++){
        if(a[j]>a[j+1]){
            temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;
        }
        }
    }
}

快速排序

public void QuickSort(int[] a,int r,int l){
   int i=r,j=r;
   int temp;
   if(l<r){
    temp=a[l];
    while(i!=j){
        while(i<j&&temp>a[j]) j--;
        if(i<j){
            a[i]=a[j];
            i++;
            }
        while(i<j&&temp<a[i]) ++i;
        if(i<j){
            a[j]=a[i];
            --j;
            }
        }
       a[i]=temp;
        QuickSort(a,l,i-1);
        QuickSort(a,i+1,r);
    }
}
Austin wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!