排序算法总结

排序算法

总纲

在这里插入图片描述

归并排序

public MagazineNode sort(MagazineNode list) {
    if (list == null || list.next == null) return list;
    MagazineNode fast = list.next;
    MagazineNode slow = list;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    MagazineNode tmp = slow.next;
    slow.next = null;
    MagazineNode left = sort(list);
    MagazineNode right = sort(tmp);
    MagazineNode h = new MagazineNode();
    MagazineNode res = h;
    while (left != null && right != null) {
        if (left.magazine.toString().compareTo(right.magazine.toString()) < 0){
            h.next = left;
            left = left.next;
        }else{
            h.next = right;
            right = right.next;
        }
        h = h.next;
    }
    h.next = left != null ? left : right;
    return res.next;
}

在这里插入图片描述

快速排序

在这里插入图片描述


package DuiXiang;
public class Demo7 {
    public static void main(String[] args) {
        int arr[] = {123,124,43,645,746,823,537,135};
        Quick(arr,0, arr.length - 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] + " ");
        }
    }
    public static void Quick(int arr[],int x,int y){
        int start = x;
        int end = y;
        if(start > end){
            return;
        }
        int baseNumber = arr[x];
        while(start != end){
            while(true){
                if(end <= start || arr[end]< baseNumber){
                    break;
                }
                end--;
            }
            while(true){
                if(end <= start || arr[start] > baseNumber){
                    break;
                }
                start++;
            }
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
        }
        int temp2 = arr[end];
        arr[end] = arr[x];
        arr[x] = temp2;
        Quick(arr,x,start - 1);
        Quick(arr,start + 1,y);
    }
}

插入排序

在这里插入图片描述

package DuiXiang;
public class Demo7 {
    public static void main(String[] args) {
        int arr[] = {123,5,124,426,14,126,125,14,124135};
        int startIndex  = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            if(arr[i] > arr[i+1]){
                startIndex = i + 1;//N+1
                break;
            }
        }
        for (int i = startIndex; i < arr.length; i++) {
            int j = i;
            while(j > 0){
                if(arr[j] < arr[j - 1]){
                    int temp = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = temp;
                    j--;
                }else{
                    break;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值