归并排序


import java.util.Arrays;


public class MergeSort {
    public static void main(String[] args) {
        int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr) {
        int[] temp = new int[arr.length];// 在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间
        sort(arr, 0, arr.length - 1, temp);
    }

    private static void sort(int[] arr, int left, int right, int[] temp) {
        if (left < right) {
            int mid = (left + right) / 2;
            sort(arr, left, mid, temp);// 左边归并排序,使得左子序列有序
            sort(arr, mid + 1, right, temp);// 右边归并排序,使得右子序列有序
            merge(arr, left, mid, right, temp);// 将两个有序子数组合并操作
        }
    }

    private static void merge(int[] arr, int left, int mid, int right,
            int[] temp) {
        int i = left;// 左序列指针
        int j = mid + 1;// 右序列指针
        int t = 0;// 临时数组指针
        while (i <= mid && j <= right) {
            if (arr[i] <= arr[j]) {
                temp[t++] = arr[i++];
            } else {
                temp[t++] = arr[j++];
            }
        }
        while (i <= mid) {// 将左边剩余元素填充进temp中
            temp[t++] = arr[i++];
        }
        while (j <= right) {// 将右序列剩余元素填充进temp中
            temp[t++] = arr[j++];
        }
        t = 0;
        // 将temp中的元素全部拷贝到原数组中
        while (left <= right) {
            arr[left++] = temp[t++];
        }
    }
}

 

//剑指offer中的方法

package sort;

import java.util.Arrays;

public class Test51 {
    static int count = 0;

    public static void main(String[] args) {
        int[] s = new int[] { 1, 2, 3, 9, 8, 2, 0 };
        countrepair(s);
        System.out.println(Arrays.toString(s));
        System.out.println(count);

    }

    public static void countrepair(int[] a) {
        int[] temp = new int[a.length];

        repair(a, 0, a.length - 1, temp);
    }

    private static void repair(int[] a, int begin, int to, int[] temp) {
        // TODO Auto-generated method stub
        if (begin < to) {
            int mid = (begin + to) >>> 1;
            repair(a, begin, mid, temp);
            repair(a, mid + 1, to, temp);
            magerArray(a, begin, mid, to, temp);

        }

    }

    private static void magerArray(int[] a, int begin, int mid, int to,
            int[] temp) {
        int i = begin;
        int j = mid + 1;
        int t = begin;
        while (i <= mid && j <= to) {
            if (a[i] > a[j]) {
                temp[t++] = a[j++];
                count += mid - i + 1;

            } else {
                temp[t++] = a[i++];
            }

        }
        while (i <= mid) {
            temp[t++] = a[i++];
        }
        while (j <= to) {

            temp[t++] = a[j++];
        }
        int x = begin;
        while (x <= to) {
            a[x] = temp[x++];
        }
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值