我试图为一个类创建一个合并排序,并偶然发现了这一点。出于某种原因,当我将原始数组拆分为较小的数组时,它会将内容从最小到最大排序。所以,我所要做的就是将它们重新组合在一起,最终得到我想要的结果。我看到了我能想到的任何地方,无法找到行为的任何解释。没有任何谷歌搜索给了我任何答案。
原始数组的内容是使用Math.random()生成的双精度数。
以下是相关代码:
import java.util.Random;
import java.lang.*;
public class Sorting
{
private static int n = 10;
private static double[] arrOne = null;
private static double[] arrTwo = null;
private static boolean timedOut = false;
private static boolean bubbleTimeOut = false;
private static boolean mergeTimeOut = false;
private static long startTime = 0;
private static long endTime = 0;
private static long bubbleSortTime = 0;
private static long mergeSortTime = 0;
public static void main(String[] args)
{
try
{
while(!(timedOut))
{
arrOne = fillArray();
arrTwo = arrOne;
if(bubbleTimeOut==false)
{
startTime = System.currentTimeMillis();
bubbleSort();
endTime = System.currentTimeMillis();
bubbleSortTime = endTime-startTime;
System.out.println("Bubble sort with "+n+" items takes "+bubbleSortTime+"ms.");
}
if(mergeTimeOut==false)
{
startTime = System.currentTimeMillis();
arrTwo = mergeSort(arrTwo);
endTime = System.currentTimeMillis();
mergeSortTime = endTime-startTime;
System.out.println("Merge sort with "+n+" items takes "+mergeSortTime+"ms.");
}
if(bubbleTimeOut && mergeTimeOut)
{
timedOut = true;
}
n = n*10;
}
}
catch(Exception e)
{
System.out.println("Java ran out of memory.");
}
}
public static double[] mergeSort(double[] arr)
{
if(arr.length<=1)
{
return arr;
}
int middle = arr.length/2;
int left = middle;
int right = arr.length - left;
double[] tempOne = new double[left];
double[] tempTwo = new double[right];
//put first half into an array
for(int i=0; i<left; i++)
{
tempOne[i] = arr[i];
endTime = System.currentTimeMillis();
if(endTime-startTime>20000)
{
mergeTimeOut = true;
return arr;
}
}
//put second half into an array
int temp = 0;
for(int i=left;i<arr.length;i++)
{
tempTwo[temp] = arr[i];
temp++;
endTime = System.currentTimeMillis();
if(endTime-startTime>20000)
{
mergeTimeOut = true;
return arr;
}
}
int ph = tempOne.length + tempTwo.length;
double[] tempThree = new double[ph];
for(int i=0;i<tempOne.length;i++)
{
tempThree[i] = tempOne[i];
endTime = System.currentTimeMillis();
if(endTime-startTime>20000)
{
mergeTimeOut = true;
return arr;
}
}
int k = tempOne.length;
for(int i=0; i<tempTwo.length;i++)
{
tempThree[k] = tempTwo[i];
k++;
endTime = System.currentTimeMillis();
if(endTime-startTime>20000)
{
mergeTimeOut = true;
return arr;
}
}
return arr;
}
public static void bubbleSort()
{
double ph = 0;
boolean swapped = true;
while(swapped)
{
swapped = false;
for(int i=n-1; i>0; i--)
{
if(arrOne[i] < arrOne[i-1])
{
ph = arrOne[i];
arrOne[i] = arrOne[i-1];
arrOne[i-1] = ph;
swapped = true;
}
endTime = System.currentTimeMillis();
if(endTime-startTime>20000)
{
swapped = false;
bubbleTimeOut = true;
}
}
}
}
//this method is just to help me test my code.
public static void printArray(double[] arr)
{
for(int i=0; i<arr.length; i++)
{
System.out.println(arr[i]);
}
}
public static double[] fillArray()
{
double[] arr = new double[n];
double rand = 0.0;
for(int i=0;i<n;i++)
{
rand = Math.random();
arr[i] = rand;
}
return arr;
}
}
所以,如果原作的内容是{8,5,4,7,3,1,2,6},那么在我把它分成两半并把它放回去之后我会得到{1,2, 3,4,5,6,7,8}我不需要实际排序任何东西。
答案 0 :(得分:2)
问题从这里开始:
arrOne = fillArray();
arrTwo = arrOne;
这使得arrTwo只是一个&#34;别名&#34; of arrOne,即引用相同数据的另一个名称。为避免这种情况,如果您确实希望它们是具有相同内容的2个单独数组,则必须复制数组,例如使用
arrOne = fillArray();
arrTwo = Arrays.copyOf(arrOne,arrOne.length);
,当然,如果你不能使用数组,可以使用for
循环手动完成。