Java 调试:合并排序
尝试在Java中实现合并排序。我已经在脑子里反复思考了我的代码,我觉得它应该能工作,但显然我做错了什么。这是密码Java 调试:合并排序,java,algorithm,data-structures,mergesort,Java,Algorithm,Data Structures,Mergesort,尝试在Java中实现合并排序。我已经在脑子里反复思考了我的代码,我觉得它应该能工作,但显然我做错了什么。这是密码 public static void mergeSort(int[] input, int start, int end) { if (end - start < 2) return; int mid = (start + end) / 2; mergeSort(input, st
public static void mergeSort(int[] input, int start, int end) {
if (end - start < 2)
return;
int mid = (start + end) / 2;
mergeSort(input, start, mid);
mergeSort(input, mid + 1, end);
merge(input, start, mid, end);
}
public static void merge(int[] input, int start, int mid, int end) {
if (input[mid - 1] <= input[mid])
return;
int i = start;
int j = mid;
int tempIndex = 0;
int[] temp = new int[end - start]; //combined size of both arrays being merged
/*if i is >= mid, then that means the left portion of the array is done being sorted
and vice versa if j >= end. Once this happens, we should be able to
just copy the remaining elements into the temp array
*/
while (i < mid && j < end) {
temp[tempIndex++] = (input[i] <= input[j]) ? input[i++] : input[j++];
}
//Copying left over elements in left portion
while (i < mid)
temp[tempIndex++] = input[i++];
//Copying left over elements in right portion
while (j < end)
temp[tempIndex++] = input[j++];
//Copy the sorted temp array into the original array
//This is where I think I must be messing up
for (int k = 0; k < temp.length; k++) {
input[start + k] = temp[k];
}
}
公共静态无效合并排序(int[]输入,int开始,int结束){
如果(结束-开始<2)
返回;
int mid=(开始+结束)/2;
合并排序(输入、开始、中间);
合并排序(输入,中间+1,结束);
合并(输入、开始、中间、结束);
}
公共静态无效合并(int[]输入,int开始,int中间,int结束){
如果(input[mid-1]=mid,则表示数组的左侧部分已完成排序
反之亦然,如果j>=end。一旦发生这种情况,我们应该能够
只需将其余元素复制到临时数组中
*/
而(i temp[tempIndex++]=(输入[i]查看以下更改:
计算中间值
int mid=start+(end-start)/2
正确分配指针i,j
int i=开始;
int j=mid+1
临时数组的大小正确
int[]temp=新int[结束-开始+1]
已更正代码中的while循环条件
class Solution{
public static void mergeSort(int[] input, int start, int end)
{
if (end == start ) return;
int mid = start + (end - start) / 2;
mergeSort(input, start, mid);
mergeSort(input, mid+1, end);
merge(input, start, mid, end);
}
public static void merge(int[] input, int start, int mid, int end) {
// No Need of the under mentioned instruction
// if(input[mid-1] <= input[mid]) return;
int i = start;
int j = mid+1;
int tempIndex = 0;
int [] temp = new int[end-start+1]; //combined size of both arrays being merged
/*if i is >= mid, then that means the left portion of the array is done being sorted and vice versa if j >= end. Once this happens, we should be able to just copy the remaining elements into the temp array */
while(i <= mid && j <= end){
temp[tempIndex++] = (input[i] <= input[j]) ? input[i++] : input[j++];
}
//Copying left over elements in left portion
while(i <= mid)
temp[tempIndex++] = input[i++];
//Copying left over elements in right portion
while(j <= end)
temp[tempIndex++] = input[j++];
//Copy the sorted temp array into the original array
//This is where I think I must be messing up
for(int k = 0; k < temp.length; k++){
input[start+k] = temp[k];
}
}
public static void main(String[] args){
int[] input = {9,4,6,8,5,7,0,2};
mergeSort(input,0,7);
for(int i : input)
System.out.println(i);
}
}
类解决方案{
公共静态void合并排序(int[]输入,int开始,int结束)
{
如果(结束==开始)返回;
int mid=start+(end-start)/2;
合并排序(输入、开始、中间);
合并排序(输入,中间+1,结束);
合并(输入、开始、中间、结束);
}
公共静态无效合并(int[]输入,int开始,int中间,int结束){
//不需要下面提到的说明
//如果(input[mid-1]=mid,则表示数组的左部分已排序完毕,如果j>=end,则反之亦然。一旦发生这种情况,我们应该能够将其余元素复制到临时数组中*/
while(i)你为什么给j加1,给临时数组的大小加1?@cris,因为j的数组从mid+1开始。如果start=2,end=5,那么元素总数将是4(2,3,4,5)。这就是为什么我给start-end加1。你调试的结果是什么?