Java 创建随机数组以实现时间排序算法
我在一篇文章中看到,他们对经典排序算法进行计时,以确定哪种算法最快,速度有多快。我想复制这个过程。我认为我所有的排序算法都是正确的,但我在创建随机数组时遇到了麻烦 这是我的主要方法,它调用另一个类并向它们发送随机数组Java 创建随机数组以实现时间排序算法,java,arrays,sorting,Java,Arrays,Sorting,我在一篇文章中看到,他们对经典排序算法进行计时,以确定哪种算法最快,速度有多快。我想复制这个过程。我认为我所有的排序算法都是正确的,但我在创建随机数组时遇到了麻烦 这是我的主要方法,它调用另一个类并向它们发送随机数组 public class Timer { public static void main(String[] args) { int[] numArray; InsertionSort insert = new InsertionSort(); MergeS
public class Timer {
public static void main(String[] args) {
int[] numArray;
InsertionSort insert = new InsertionSort();
MergeSort merge = new MergeSort();
QuickSort quick = new QuickSort();
SelectionSort select = new SelectionSort();
BubbleSort bubble = new BubbleSort();
int sizeNumber = 0;
Scanner scanner;
scanner = new Scanner(System.in);
System.out.println("Enter size of arrayList to be sorted: ");
sizeNumber = scanner.nextInt();
scanner.close();
numArray = new int[sizeNumber];
arraySize(sizeNumber, numArray);
insert.sort(numArray);
merge.sort(numArray);
quick.sort(numArray);
select.sort(numArray);
bubble.sort(numArray);
}
public static int[] arraySize(int number, int[] array) {
Random rng = new Random();
array = new int[number];
for (int i = 0; i < array.length; i++) {
int random = rng.nextInt();
array[i] = random;
}
return array;
}
}
公共类计时器{
公共静态void main(字符串[]args){
国际[]努马拉;
InsertionSort insert=新建InsertionSort();
MergeSort merge=新的MergeSort();
快速排序快速=新建快速排序();
SelectionSort select=新建SelectionSort();
BubbleSort bubble=新的BubbleSort();
int-sizeNumber=0;
扫描仪;
扫描仪=新扫描仪(System.in);
System.out.println(“输入要排序的arrayList的大小:”);
sizeNumber=scanner.nextInt();
scanner.close();
Numaray=新整数[sizeNumber];
阵列化(Sizember,Numaray);
insert.sort(numArray);
merge.sort(numaray);
快速排序(numArray);
选择。排序(numArray);
气泡排序(numArray);
}
公共静态int[]数组化(int编号,int[]数组){
随机rng=新随机();
数组=新整数[数字];
for(int i=0;i
为了清楚起见,我想知道如何创建int[]数组,并根据用户的选择长度用随机数填充它。目前,我使用的方法返回的值类似于[I@9931f5
而不是任何数字。int[]随机数组(int-size,int-lower,int-higher)
int[] randomArray(int size, int lower, int higher)
{
int[] arr = new int[size];
Random r = new Random();
for(int i = 0 ; i < size ; i++)
{
arr[i] = r.nextInt(higher-lower) + lower;
}
return arr;
}
{
int[]arr=新的int[size];
随机r=新随机();
对于(int i=0;i
这将生成一个大小为size
的数组,其值介于lower
和higher
之间
array[i] = array[random];
这不是你想要的。random
可以是任何合法的int
值,因此你可能会越界。我相信你会想要的
array[i] = random;
相反
您还在其中分配了一个不必要的数组(
newint[…]
,然后在arraySize
中覆盖它),并且实际上不需要从arraySize
返回值(尽管使用返回值而不是输出参数会更干净).如果您限制从Random.nextInt生成的数字会更好,这样数字就不会太大,也不会太多样化
因此,通过一些小的修复,arraySize函数可以像这样正常工作:
public static void arraySize(int[] array) {
Random rng = new Random();
for(int i=0;i<array.length;i++){
array[i]=rng.nextInt(100);
/*here I fix the size to 0 - 100, you can create a way
to adjust this with respect to the array size as well,
so that the numbers could distribute more evenly */
}
}
numArray将按第一个insert.sort进行排序,因此第二个和下面的排序只会得到一个已排序的numArray,它不会成功测试排序的复杂性。您必须手动获取numArray的多个副本,作为每个排序的参数,一个简单的函数可以做到这一点:
public static void copyArray(int[] copy,int[] array){
for(int i=0;i<array.length;i++){
copy[i]=array[i];
}
}
publicstaticvoidcopyArray(int[]copy,int[]array){
对于(int i=0;i您遇到了什么样的问题?微型基准测试Java算法是需要解决的最复杂的任务之一。不要这样做。如果这样做,请使用一种工具,试图避免所有错误基准测试的陷阱,如卡尺。此外,您应该选择:名称不正确的arraySize()方法修改其参数,或返回一个(新)数组。将两者都设置为数组是非常令人困惑的。Java的做法是让它创建并返回一个数组。我已经想到了你的第二点。我在每个类中都这样设置了几行,试图保持原始数组未排序。public void sort(int[]数组){int[]array2=new int[array.length];array2=Arrays.copyOf(array,array.length);
我尝试使用您为随机数数组提供的代码,每次运行时都会得到[I@df8f5e
当我对阵列进行syso时。
public static void copyArray(int[] copy,int[] array){
for(int i=0;i<array.length;i++){
copy[i]=array[i];
}
}