首页 新闻 会员 周边

归并排序问题

0
[已关闭问题] 关闭于 2022-04-09 22:27

麻烦大佬看看哪有问题

import java.util.Arrays;

public class a {
public static void main(String[] args) {
int[] arr = {3, 6, 23, 7, 9, 4, 23, 67};
chai(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}

private static void chai(int[] arr, int startIndex, int endIndex) {
int centerIndex = (startIndex + endIndex) / 2;
if (startIndex<endIndex) {
chai(arr, startIndex, centerIndex);
chai(arr,centerIndex+1,endIndex);
guibin(arr,startIndex,centerIndex,endIndex);
}
}

private static void guibin(int[] arr, int startIdex, int centerIndex, int endIndex) {
    int[] temparr = new int[endIndex - startIdex + 1];
    int i = startIdex;
    int j = centerIndex + 1;
    int index = 0;
    while (i<=centerIndex&&j<=endIndex){
        if (arr[i]<=arr[j]){
            temparr[index]=arr[i];
            i++;
        }else{
            temparr[index]=arr[j];
            j++;
        }
        index++;
    }
    while (i<=centerIndex){
        temparr[index] = arr[i];
        i++;
        index++;
    }
    while (j<=centerIndex){
        temparr[index] = arr[j];
        j++;
        index++;
    }
    for (int k = 0; k < temparr.length; k++) {
        arr[k + startIdex] = temparr[k];
    }
}

}

芜炎的主页 芜炎 | 菜鸟二级 | 园豆:202
提问于:2022-04-08 23:36
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册