我想組數字,一直stucking這裏持續3天沒有找到我的代碼錯誤...真的絕望了排序(由命令行參數)的輸入...使用歸併處理用戶輸入
威爾任何人都請給我一個提示?
import java.util.Arrays;
public class MergeSorter
{
public MergeSorter(int[] anArray)
{
a = anArray;
}
public void sort()
{
if (a.length <= 1) return;
int[] first = new int[a.length/2];
int[] second = new int[a.length - first.length];
System.arraycopy(a, 0, first, 0, first.length);
System.arraycopy(a, first.length, second, 0, second.length);
MergeSorter firstSorter = new MergeSorter(first);
MergeSorter secondSorter = new MergeSorter(second);
firstSorter.sort();
secondSorter.sort();
merge(first, second);
}
private void merge(int[] first, int[] second)
{
int iFirst = 0;
int iSecond = 0;
int j = 0;
while (iFirst < first.length && iSecond < second.length)
{
if (first[iFirst] < second[iSecond])
{
a[j] = first[iFirst];
iFirst++;
}
else
{
a[j] = second[iSecond];
iSecond++;
}
j++;
}
System.arraycopy(first, iFirst, a, j, first.length - iFirst);
System.arraycopy(second, iSecond, a, j, second.length - iSecond);
}
private int[] a;
public static void main(String[] args)
{
int[] a = new int[args.length];
for (int i = 0; i < args.length; i++)
{
a[i] = Integer.parseInt(args[i]);
}
MergeSorter sorter = new MergeSorter(a);
sorter.sort();
System.out.println(Arrays.toString(a));
}
}
如果這不是Mer的練習gesort,你可以簡單地使用'Arrays.sort(array);',這是一個mergesort。 – 2012-04-15 09:45:28
男人!你真的把我拉出苦難......上帝保佑你 – Harvey 2012-04-15 10:04:15
嗯..我只是跑你的代碼,它就像它應該?? – Anthales 2012-04-15 10:05:21