最少交换次数

描述

给出一个无序数列,每次只能交换相邻两个元素,求将原数列变成递增数列的最少交换次数。 如:数列:2,3,1,交换3和1后变成:2,1,3;交换1和2之后变成:1,2,3。总共交换2次。

输入样例

2,3,1

输出样例

2

我的代码

private static String solution(String line) {
    // 在此处理单行数据
    String[] array = line.split(",");
    int[] array1 = new int[array.length];
    for (int i = 0; i < array1.length; i++) {
        array1[i] = Integer.parseInt(array[i]);
    }
    int result = sort(array1);
    // 返回处理后的结果
    return result + "";
}

private static int sort(int []arr){
    int []temp = new int[arr.length];//在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间
    return sort(arr,0,arr.length-1,temp);
}
private static int sort(int[] arr,int left,int right,int []temp){
    if(left<right){
        int mid = (left+right)/2;
        int count = 0;
        count += sort(arr,left,mid,temp);//左边归并排序,使得左子序列有序
        count += sort(arr,mid+1,right,temp);//右边归并排序,使得右子序列有序
        count += merge(arr,left,mid,right,temp);//将两个有序子数组合并操作
        return count;
    }
    return 0;
}
private static int merge(int[] arr,int left,int mid,int right,int[] temp){
    int i = left; // 左序列指针
    int j = mid+1; // 右序列指针
    int t = left; // 临时数组指针
    int count = 0;
    while (i<=mid && j<=right){
        if(arr[i]<=arr[j]){ // 此处为稳定排序的关键,不能用小于
            temp[t++] = arr[i++];
        }else {
            temp[t++] = arr[j++];
            count+=j-t; //每当后段的数组元素提前时,记录提前的距离(有几个数大于当前处于j位置的数)
        }
    }
    while(i<=mid){ // 将左边剩余元素填充进temp中
        temp[t++] = arr[i++];
    }
    while(j<=right){ // 将右序列剩余元素填充进temp中
        temp[t++] = arr[j++];
    }
    t = left;
    // 将temp中的元素全部拷贝到原数组中
    while(left <= right){
        arr[left++] = temp[t++];
    }
    return count;
}

说明

这题其实就是求一串数字的逆序数,本解决方法采用的是归并排序算法求逆序数,归并排序算法参考博客:博客园 dreamcatcher-cx 的 图解排序算法(四)之归并排序
(咳咳,我完全就是拿他写的归并排序算法,然后改了一点东西)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值