Java 如何对具有不同整数值的数组进行排序
我目前正在计时不同的排序方法,当它们运行20k随机整数时,但是如果我想测试1000、20k、100k和200k 我该怎么做呢 不,我不是来给我答案的,我只是需要一些指导Java 如何对具有不同整数值的数组进行排序,java,arrays,Java,Arrays,我目前正在计时不同的排序方法,当它们运行20k随机整数时,但是如果我想测试1000、20k、100k和200k 我该怎么做呢 不,我不是来给我答案的,我只是需要一些指导 public static void main(String[] args) { int n = 10; int count = 0; for (int i=0; i < n; i++) count++; System.out.println(count);
public static void main(String[] args) {
int n = 10;
int count = 0;
for (int i=0; i < n; i++)
count++;
System.out.println(count);
int size = 20000;
int[] array = new int[size];
populateArrat (array);
long start;
long elapsed;
start = System.currentTimeMillis();
SortingAlgorithms.BubbleSortCS(array.clone());
elapsed = System.currentTimeMillis() - start;
System.out.printf("BubbleSortCS: %,d\n", elapsed);
start = System.currentTimeMillis();
SortingAlgorithms.QuickSort(array.clone(), 0, array.length-1);
elapsed = System.currentTimeMillis() - start;
System.out.printf("Quick Sort: %,d\n", elapsed);
start = System.currentTimeMillis();
SortingAlgorithms.InsertionSort(array.clone());
elapsed = System.currentTimeMillis() - start;
System.out.printf("Insertion Sort: %,d\n", elapsed);
}
public static void populateArrat(int[] array)
{
Random rand = new Random();
for (int i = 0; i < array.length; i++)
array[i] = rand.nextInt(100);
}
}
publicstaticvoidmain(字符串[]args){
int n=10;
整数计数=0;
对于(int i=0;i
我的排序算法课是这样的
public class SortingAlgorithms {
public static void BubbleSortCS(int[] array)
{
for (int i=0; i <array.length - 1; i++)
{
boolean Swap = false;
for (int j=0; j < array.length - i - 1; j++)
if(array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
Swap = true;
}
if(Swap == false)
break;
}
}
public static void InsertionSort(int[] array)
{
int i, key, j;
for (i = 1; i < array.length; i++)
{
key = array[i];
j = i -1;
while (j >= 0 && array[j] > key)
{
array[j+1] = array[j];
j = j - 1;
}
array[j+1] = key;
}
}
public static int partition( int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1);
for (int j = low; j<high; j++)
{
if (arr[j] < pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
public static void QuickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
QuickSort(arr, low, pi-1);
QuickSort(arr, pi+1, high);
}
}
}
公共类排序算法{
公共静态void BubbleSortCS(int[]数组)
{
对于(int i=0;i数组[j+1])
{
int temp=数组[j];
数组[j]=数组[j+1];
阵列[j+1]=温度;
Swap=true;
}
if(Swap==false)
打破
}
}
公共静态void InsertionSort(int[]数组)
{
int i,key,j;
对于(i=1;i=0&&array[j]>键)
{
数组[j+1]=数组[j];
j=j-1;
}
数组[j+1]=键;
}
}
公共静态int分区(int-arr[],int-low,int-high)
{
int pivot=arr[高];
int i=(低-1);
对于(intj=low;jI)
- 为每个尺寸创建一个新的数据集
- 并对每种排序类型进行复制,以确保公平性
您可以根据需要迭代上述内容。例如,您可以创建一个包含所有要测试的大小的数组,然后在数组中循环,对于每个大小,创建一个具有该大小的数组并运行上面的所有计算。如果数组已经循环,如何使数组循环?在创建之前。在decl之前启动循环大小相同。有效地将所有应用程序代码包装在一个uber循环中
int ds = 100_000;
int [] array = createTestArray(ds);
// Now do your tests, using a copy of the same data
// set for each test.
int [] copy = Arrays.copyOf(array,array.length);
// test1
copy = Arrays.copyOf(array.array.length);
// test2
....
// and so forth.
public static int[] createTestArray(int size) {
Random rand = new Random();
int[] array = rand.ints(size).toArray();
return array;
}