publicclassMergesort2{publicstaticvoidmain(String[]args){intx[]={2,1,4,67,34,9,78,99};sort(x,0,x.length-1);for(intnum:x)System.out.printf(num+"");}publicstaticvoidsort(in...
public class Mergesort2 {
public static void main(String[] args) {
int x[]={2,1,4,67,34,9,78,99};
sort(x,0,x.length-1);
for(int num:x)
System.out.printf(num+" ");
}
public static void sort(int a[],int left,int right){
if(left
int i=(left+right)/2;
sort(a,0,i);
sort(a,i+1,right);
int[] b=new int[a.length];
Merge(a,b,left,i,right);
copy(a,b,left,right);
}
}
public static void Merge(int c[],int d[],int left,int m,int right){
int i=0,j=m+1,k=0;
while((i<=m)&&(j<=right))
if(c[i]<=c[j])
d[k++]=c[i++];
else
d[k++]=c[j++];
if(i>m)
for(int q=j;q<=right;q++)
d[k++]=c[q];
else
for(int q=i;q<=m;q++)
d[k++]=c[q];
}
public static void copy(int a[],int b[],int left,int right){
for(int t=left;t<=right;t++)
a[t]=b[t];
}
}
输出结果为:1 2 4 9 34 67 78 99
但是当我在主程序中数组定义为int int x[]={2,1,44,67,34,9,78,99};
就只是将4变成了44,怎么输出结果变成了:1 2 44 44 44 67 78 99
请问程序中出了什么问题?需改进哪些地方?
展开