排序

冒泡排序:

public class Main{
    public static void main(String[] args){
        int[] a = {3,5,2,9,1,6,8,7,4,0};
        for(int i=0;i<a.length;i++){
         for(int j=0;j<a.length-i-1;j++){
          if(a[j]>a[j+1]){
           int temp = a[j];
           a[j] = a[j+1];
           a[j+1] = temp;
          }
         }
        }
        System.out.println(Arrays.toString(a));
    }
}

选择排序:

public class Main{
    public static void main(String[] args){
        int[] a = {3,5,2,9,1,6,8,7,4,0};
        for(int i=0;i<a.length;i++){
         for(int j=i+1;j<a.length;j++){
          if(a[j]<a[i]){
           int temp = a[i];
           a[i] = a[j];
           a[j] = temp;
          }
         }
        }
        System.out.println(Arrays.toString(a));
    }
}

快速排序:

public class ckuaiSu {
    private int[] arr;
    public ckuaiSu(int[] arr) {
        this.arr = arr;
    }
    public static void main(String[] args) {
        int[] a = {5,8,7,1,9,3,6,8,0,2,4,9};
        ckuaiSu ks = new ckuaiSu(a);
        ks.leaveGrp(0, a.length-1);
        System.out.println(Arrays.toString(a));
    }
    public void leaveGrp(int low,int high){
        if(low>=high){
            return;
        }else{
            int pivot = arr[low];
            int standard = find(low,high,pivot);
            leaveGrp(low, standard-1);
            leaveGrp(standard+1, high);
        }
    }
    private int find(int low, int high, int pivot) {
        while(low<high){
            while(low<high&&arr[high]>=pivot){
                high--;
            }
            swap(low,high);
            while(low<high&&arr[low]<=pivot){
                low++;
            }
            swap(low, high);
        }
        return low;
    }
    private void swap(int low, int high) {
        int temp = arr[low];
        arr[low] = arr[high];
        arr[high]= temp;
    }
}

插入排序:

public class dchaRu {
    public static void main(String[] args) {
        int[] a = {5,9,7,6,3,4,8,1,0,2,9};
        test(a);
        System.out.println(Arrays.toString(a));
    }
    public static void test(int[] a){
        for(int i=1;i<a.length;i++){
            int current = a[i];
            int preIndex = i-1;
            while(preIndex>=0 && current<a[preIndex]){
                a[preIndex+1] = a[preIndex];
                preIndex--;
            }
            a[preIndex+1] = current;
        }
    }
}

归并排序:

public class fguiBing {
    int[] arr;
    public fguiBing(int[] arr) {
        this.arr = arr;
    }
    public static void main(String[] args) {
        int[] a = {5,8,7,1,9,3,6,8,0,2,4,9};
        fguiBing gb = new fguiBing(a);
        int[] workspace = new int[a.length];
        gb.leave(0, a.length-1, workspace);
        System.out.println(Arrays.toString(a));
    }
    public void leave(int firIndex,int lastIndex,int[] workspace){
        if(firIndex >= lastIndex){
            return;
        }else{
            int mid = (firIndex+lastIndex)/2;
            leave(firIndex, mid, workspace);
            leave(mid+1, lastIndex, workspace);
            merge(firIndex,lastIndex,mid,workspace);
        }
    }
    public void merge(int firIndex, int lastIndex, int mid, int[] workspace) {
        int lowBegin = firIndex;
        int lowEnd = mid;
        int highBegin = mid+1;
        int highEnd = lastIndex;
        int j = 0;
        int k = lastIndex-firIndex+1;
        while(lowBegin<=lowEnd && highBegin<=highEnd){
            if(arr[lowBegin]<arr[highBegin]){
                workspace[j++] = arr[lowBegin++];
            }else{
                workspace[j++] = arr[highBegin++];
            }
        }
        while(lowBegin<=lowEnd){
            workspace[j++] = arr[lowBegin++];
        }
        while(highBegin<=highEnd){
            workspace[j++] = arr[highBegin++];
        }
        for(j=0;j<k;j++){
            arr[firIndex++] = workspace[j];
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值