我正在编写一个程序,它通过创建一个随机的整数数组并用五种不同的算法对它进行排序来计算不同排序算法的运行时间。我需要对原始数组进行深度复制,这样当我运行下一个排序算法时,它不会对已排序的数组进行排序(从而影响我的测试结果)。
这是我的代码:
import java.io.*;
import java.util.*;
class SortingTest
{
static int runtime=0;
static int start=0;
static int stop=0;
/**@param start the time when the algorithm started sorting
@param stop the time when the algorithm finished sorting
@return runtime the total time it took for the sorting algorithm to sort
a function that calculates the runtime of the sorting algorithm*/
public static int findruntime(int start, int stop)
{
return runtime=stop-start;
}
/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs bubble sort and prints the run time*/
public static void runbubble(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.bubble(A);
stop=(int)System.currentTimeMillis();
if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}
findruntime(start,stop);
System.out.println("The running time for Bubble Sort is "+ runtime+ " milliseconds.");
}
/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs selection sort and prints the run time*/
public static void runselection(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.select(A);
stop=(int)System.currentTimeMillis();
if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}
findruntime(start,stop);
System.out.println("The running time for Selection Sort is "+ runtime+" milliseconds.");
}
/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs insertion sort and prints the run time*/
public static void runinsertion(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.insertion(A);
stop=(int)System.currentTimeMillis();
if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}
findruntime(start,stop);
System.out.println("The running time for Insertion Sort is "+ runtime+" milliseconds.");
}
/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs quick sort and prints the run time*/
public static void runquick(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.quick(A);
stop=(int)System.currentTimeMillis();
if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}
findruntime(start,stop);
System.out.println("The running time for Quick Sort is "+ runtime+" milliseconds.");
}
/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs shell sort and prints the run time*/
public static void runshell(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.shell(A);
stop=(int)System.currentTimeMillis();
if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}
findruntime(start,stop);
System.out.println("The running time for Shell Sort is "+ runtime+" milliseconds.");
}
public static void main (String [] args)
throws IOException
{
System.out.println("Welcome to the Sorting Algorithms Speed Test!");
System.out.print("Please enter a value for n:");
Scanner cin = new Scanner(System.in);
int n= cin.nextInt();
//create array of size n
Integer[] list1= new Integer[n];
//generate and fill in the array
for(int i=0; i<list1.length; i++)
{
int number= (int)(1+n*Math.random());
list1[i]=number;
}
//print if n<=100
if (n<=100)
{
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();
}
// MUST FIGURE OUT A WAY SO THAT THE ORIGINAL ARRAY LIST DOES NOT GET SORTED AS WELL
//using bubble sort
/*runbubble(list1,n);
//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();
//using selection sort
runselection(list1,n);
//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();
//using insertion sort
runinsertion(list1,n);
//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();
//using quick sort
runquick(list1,n);
//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();*/
//using shell sort
runshell(list1,n);
//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();
}
}
答案 0 :(得分:2)
您应该使用System.arrayCopy()
所以当你声明你的整数数组
时//create array of size n
Integer[] list1 = new Integer[n];
Integer[] copyList1 = new Integer[n];
所以,当您使用可以调用的值填充list1后,您就可以创建2个数组
System.arraycopy(list1 ,0 ,copyList1 ,0 ,list1.length);
将list1中的所有值复制到copyList1中,显然你可以给出更好的名字。