排序分类

内排:
插入
直接插入

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

折半插入

public void binarySort(int[] list){
        for(int i=1;i<list.length;i++){
            int temp=list[i];
            int low=0,high=i-1;
            while(low<=high){
                int mid=(low+high)/2;
                if(list[mid]<temp){
                    low=mid+1;
                }else{
                    high=mid-1;
                }
            }
            for(int j=i;j>low;j--){
                list[j]=list[j-1];
            }
            list[low]=temp;
        }
    }

交换
冒泡排序

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

快速排序

//partition使用两种方法实现
public int partition(int[] list,int low,int high){
        int temp=list[low];
        while(low<high){
            while(low<high && list[high]>=temp)
                high--;
            list[low]=list[high];
            while(low<high && list[low]<=temp)
                low++;
            list[high]=list[low];
        }
        list[low]=temp;
        return low;
    }
    public int partition1(int[] list,int low,int high){
        int rear=list[high];
        int p=low-1;
        for(int i=low;i<=high-1;i++){
            if(list[i]<=rear){
                p++;
                int temp=list[i];
                list[i]=list[p];
                list[p]=temp;
            }
        }
        p++;
        int temp=list[p];
        list[p]=rear;
        list[high]=temp;
        return p;
    }
    public void qucikSort(int[] list,int low ,int high){
            if(low<high){
                int res=partition(list,low,high);
                //System.out.println(res);
                qucikSort(list,low ,res-1);
                qucikSort(list,res+1 ,high);
            }
    }

选择
简单选择

void SimpleSort(int list[]) {
       for(int i=0;i<list.length-1;i++){
           int k=i;
           for(int j=i+1;j<=list.length-1;j++){
               if(list[j]<list[k])
                   k=j;
           }
           int temp=list[i];
           list[i]=list[k];
           list[k]=temp;
       }
    }

堆排序

//res为最终排序结果
    public ArrayList heapSort(int[] list, int length){
        ArrayList res=new ArrayList();
        buildMaxHeap(list,length);
        for(int i=0;i<=length-1;i++){
            res.add(list[0]);
            list[0]=list[length-i-1];
            adjustDown(list,0,length-i-1);
       }
        return res;
    }
    public void buildMaxHeap(int[] list,int length){
        for(int i=(length-1)/2;i>=0;i--){
            adjustDown(list,i,length);
        }

    }
    public void adjustDown(int[] list,int k,int length){
        int temp=list[k];
        for(int i=2*k+1;i<=length-1;i=2*i+1){
            if(i<length-1 && list[i+1]>list[i]){
                i++;
            }
            if(temp>=list[i]){
                break;
            }else{
                list[k]=list[i];
                k=i;
            }
        }
        list[k]=temp;
    }

归并
二路归并

public  void merge(int[] list,int low,int mid,int high){
        int[] temp=new int[high+1];
        for(int i=low;i<=high;i++)
            temp[i]=list[i];
        int i=low,j=mid+1,k=low;
        for(;i<=mid&& j<=high;){
            if(temp[i]<temp[j]){
                list[k++]=temp[i++];
            }else{
                list[k++]=temp[j++];
            }
        }
        while(i<=mid){
            list[k++]=temp[i++];
        }
        while(j<=high){
            list[k++]=temp[j++];
        }
    }

    public void mergeSort(int[] list,int low,int high){
        if(low<high){
            int mid=(low+high)/2;
            mergeSort(list,low,mid);
            mergeSort(list,mid+1,high);
            merge(list,low,mid,high);
        }
    }

计数排序

public int[] countSort(int[] list,int k){
        int[] res=new int[list.length];
        int[] temp=new int[k+1];
        for(int i=0;i<list.length;i++){
            temp[list[i]]++;
        }
        for(int i=1;i<=temp.length-1;i++){
            temp[i]=temp[i]+temp[i-1];
        }
        for(int i=list.length-1;i>=0;i--){
            res[temp[list[i]]-1]=list[i];
            temp[list[i]]--;
        }
        return res;
    }

基数排序

桶排序

外排:

多路归并

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值