java归并排序

思路:
1.首先将要排序的数据进行分割,分组,直到每个组中只有两个数或者一个数
2.将当前组中的数据进行排序,然后再排序临近的两个数组,直到只有一个分组
例如 首先对角标为0,1的分组进行排序,然后排序角标为2,3的分组,然后对角标0到3的分组进行排序
代码:

package com.wangyq.datastructrue.sort;

import java.util.Arrays;

public class MeagerSort {
    public static void main(String[] args) {
        //待排序数组
        int[] ints = {65, 64, 561, 213, 5479, 87465, 13, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6};
        System.out.println("排序前:" + Arrays.toString(ints));
        //归并排序
        meagerSort(0, ints.length - 1, ints);
        System.out.println("排序后:" + Arrays.toString(ints));

    }

    private static void meagerSort(int l, int r, int[] ints) {
        //获取中中间值
        int mid = (l + r) / 2;
        //判断有没有到最末级,分到最小组
        if (mid > l) {
            meagerSort(l, mid, ints);
        }
        //mid != l防止死循环
        if (mid < r && mid != l) {
            meagerSort(mid + 1, r, ints);
        }
        System.out.println("对角标" + l + "到" + r + "进行排序");

        //确定好分组后任意一种排序方式进行排序,这里选择插入排序
        //存储当前需要插入的值
        int temp;
        int corner = 1;//记录当前最小值的角标

        for (int i = l; i <= r; i++) {
            temp = ints[i];
            for (corner = i; corner > 0; corner--) {
                //如果大于插入值,后移
                if (ints[corner - 1] > temp) {
                    ints[corner] = ints[corner - 1];
                } else {
                    break;
                }
            }
            //获取插入角标,将值插入
            ints[corner] = temp;
        }
        System.out.println("当前排序结果:" + Arrays.toString(ints));
    }
}

执行结果:

排序前:[65, 64, 561, 213, 5479, 87465, 13, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标0到1进行排序
当前排序结果:[64, 65, 561, 213, 5479, 87465, 13, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标2到3进行排序
当前排序结果:[64, 65, 213, 561, 5479, 87465, 13, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标0到3进行排序
当前排序结果:[64, 65, 213, 561, 5479, 87465, 13, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标4到5进行排序
当前排序结果:[64, 65, 213, 561, 5479, 87465, 13, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标6到6进行排序
当前排序结果:[13, 64, 65, 213, 561, 5479, 87465, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标4到6进行排序
当前排序结果:[13, 64, 65, 213, 561, 5479, 87465, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标0到6进行排序
当前排序结果:[13, 64, 65, 213, 561, 5479, 87465, 9, 7984, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标7到8进行排序
当前排序结果:[9, 13, 64, 65, 213, 561, 5479, 7984, 87465, 31, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标9到9进行排序
当前排序结果:[9, 13, 31, 64, 65, 213, 561, 5479, 7984, 87465, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标7到9进行排序
当前排序结果:[9, 13, 31, 64, 65, 213, 561, 5479, 7984, 87465, 6874, 51, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标10到11进行排序
当前排序结果:[9, 13, 31, 51, 64, 65, 213, 561, 5479, 6874, 7984, 87465, 321, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标12到12进行排序
当前排序结果:[9, 13, 31, 51, 64, 65, 213, 321, 561, 5479, 6874, 7984, 87465, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标10到12进行排序
当前排序结果:[9, 13, 31, 51, 64, 65, 213, 321, 561, 5479, 6874, 7984, 87465, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标7到12进行排序
当前排序结果:[9, 13, 31, 51, 64, 65, 213, 321, 561, 5479, 6874, 7984, 87465, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标0到12进行排序
当前排序结果:[9, 13, 31, 51, 64, 65, 213, 321, 561, 5479, 6874, 7984, 87465, 84, 651, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标13到14进行排序
当前排序结果:[9, 13, 31, 51, 64, 65, 84, 213, 321, 561, 651, 5479, 6874, 7984, 87465, 23, 68, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标15到16进行排序
当前排序结果:[9, 13, 23, 31, 51, 64, 65, 68, 84, 213, 321, 561, 651, 5479, 6874, 7984, 87465, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标13到16进行排序
当前排序结果:[9, 13, 23, 31, 51, 64, 65, 68, 84, 213, 321, 561, 651, 5479, 6874, 7984, 87465, 56, 52, 416, 1, 3, 464, 68, 4, 6]
对角标17到18进行排序
当前排序结果:[9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 84, 213, 321, 561, 651, 5479, 6874, 7984, 87465, 416, 1, 3, 464, 68, 4, 6]
对角标19到19进行排序
当前排序结果:[9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 84, 213, 321, 416, 561, 651, 5479, 6874, 7984, 87465, 1, 3, 464, 68, 4, 6]
对角标17到19进行排序
当前排序结果:[9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 84, 213, 321, 416, 561, 651, 5479, 6874, 7984, 87465, 1, 3, 464, 68, 4, 6]
对角标13到19进行排序
当前排序结果:[9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 84, 213, 321, 416, 561, 651, 5479, 6874, 7984, 87465, 1, 3, 464, 68, 4, 6]
对角标20到21进行排序
当前排序结果:[1, 3, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 84, 213, 321, 416, 561, 651, 5479, 6874, 7984, 87465, 464, 68, 4, 6]
对角标22到22进行排序
当前排序结果:[1, 3, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465, 68, 4, 6]
对角标20到22进行排序
当前排序结果:[1, 3, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465, 68, 4, 6]
对角标23到24进行排序
当前排序结果:[1, 3, 4, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465, 6]
对角标25到25进行排序
当前排序结果:[1, 3, 4, 6, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465]
对角标23到25进行排序
当前排序结果:[1, 3, 4, 6, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465]
对角标20到25进行排序
当前排序结果:[1, 3, 4, 6, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465]
对角标13到25进行排序
当前排序结果:[1, 3, 4, 6, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465]
对角标0到25进行排序
当前排序结果:[1, 3, 4, 6, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465]
排序后:[1, 3, 4, 6, 9, 13, 23, 31, 51, 52, 56, 64, 65, 68, 68, 84, 213, 321, 416, 464, 561, 651, 5479, 6874, 7984, 87465]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值