【发布时间】:2018-03-30 11:37:57
【问题描述】:
我正在尝试这样做,所以我生成了一个随机数组,但只实现了一个具有不同排序的相同数组(以随机顺序)。我有:
public static void main(String[] args)
{
int[] array = new int[10];
for(int i = 0; i < array.length; i++) {
array[i] = (int)(Math.random()*100);}
System.out.println("\nBefore Bubble Sort: ");
for (int element : array)
System.out.print(element + " ");
bubbleSort(array);
System.out.println("After Bubble Sort: ");
for (int element : array)
System.out.print(element + " ");
System.out.println("\n");
System.out.println("\nBefore Insertion Sort: ");
for (int element : array)
System.out.print(element + " ");
insertionSort(array);
System.out.println("After Insertion Sort: ");
for (int element : array)
System.out.print(element + " ");
System.out.println("\n");
}
带有相应的分类代码(如有必要,我会发布它们)。它的输出是:
Array Before Bubble Sort:
2 64 27 1 81 60 72 6 9 82
Array After Bubble Sort:
1 2 6 9 27 60 64 72 81 82
Array Before Insertion Sort:
1 2 6 9 27 60 64 72 81 82
Array After Insertion Sort:
1 2 6 9 27 60 64 72 81 82
我希望这个数组2 64 27 1 81 60 72 6 9 82 也位于插入前行。冒泡排序中的排序数组只是被放入插入排序中,所以它没有做任何事情。我想我需要为随机数组创建一个方法并在每次排序时调用它?我该怎么做?或任何其他我会欣赏的解决方案。如果需要,我将编辑更多信息。
【问题讨论】:
-
使用
System.arraycopy将您的数组复制到“夹具”中,并在每次排序执行之前将该夹具复制回来。