使用此代码,我应该使用一些大小为2048,4096,8192和16384的数组。使用1-1000中的随机整数填充它们,使用快速排序对它们进行排序,然后显示最小值,最大值和根据您想要重复循环的次数进行平均比较。
问题是,当我选择仅重复此过程一次时,min max和avg都是相同的数字。这是控制台:
WELCOME TO QuickSortTest!
Please enter how many times you would like to repeat the test:
1
*************** CURRENT REPEAT: 1 ***************
Test on array with size 2048
Before sort:
4392, 24, 3658, 4353, 3450, 2737, 2317, 1223, 1534, 3363, 551, 3369, 4597, 4712, 681.....
After sort:
2, 7, 8, 11, 13, 14, 16, 16, 23, 24, 32, 41.......
Sorted?: true
Test on array with size 4096
Before sort:
4182, 3847, 4388, 3628, 4309, 2987, 2454, 4938, 4187, 223.....
After sort:
2, 3, 4, 4, 5, 6, 7, 7, 10, 11, 11, 12, 12, 13, 13.....
Sorted?: true
Test on array with size 8192
Before sort:
2795, 1531, 4347, 2122, 2015, 3900, 3198, 240, 3821...
After sort:
1, 1, 1, 1, 1, 2, 3, 3, 3, 4, 4, 6, 6, 7, 7, 8, 8, 10...
Sorted?: True
Test on array with size 16384
Before sort:
3577, 3411, 1576, 3966, 3270, 2922, 1478, 617, 3466, 3612....
Test on array with size 16384
Before sort:
3577, 3411, 1576, 3966, 3270, 2922, 1478, 617, 3466, 361.....
Sorted?: true
Data results for array size 2048:
Min: 25348
Max: 25348
AVG: 25348.0
Data results for array size 4096:
Min: 60721
Max: 60721
AVG: 60721.0
Data results for array size 8192:
Min: 121244
Max: 121244
AVG: 121244.0
Data results for array size 16384:
Min: 284787
Max: 284787
AVG: 284787.0
现在,当我输入10次重复时,结果是不同的:
Data results for array size 2048:
Min: 22856
Max: 26858
AVG: 24962.5
Data results for array size 4096:
Min: 52289
Max: 58102
AVG: 55973.7
Data results for array size 8192:
Min: 116986
Max: 131413
AVG: 124013.6
Data results for array size 16384:
Min: 272162
Max: 322172
AVG: 287152.9
这是我的代码:
import java.util.*;
public class QuickSortTest {
static int qcount = 0,k = 0;
static int[] size;
static int[][] stats;
public static void main(String[] args) {
System.out.println("WELCOME TO QuickSortTest!\n");
Scanner console = new Scanner(System.in);
System.out.println("Please enter how many times you would like to repeat the test: ");
k = console.nextInt();
int[] size = {2048, 4096, 8192, 16384};
stats = new int[4][k]; //[sizeArray][totalRepeats]
for (int j = 1; j <= k; j++) {
System.out.println("*************** CURRENT REPEAT: " + j + " ***************");
for (int s = 0; s < size.length; s++) {//size array
qcount = 0;
int[] a = new int[size[s]];
populateArray(a);
System.out.println("\nTest on array with size " + (size[s]) + "\nBefore sort: ");
print(a);
quicksort(a, 0, a.length - 1);
System.out.println("After sort: ");
print(a);
System.out.println("Sorted?: " + verifySort(a));
stats[s][j - 1] = qcount;
qcount=0;
}
}
getData(size);
}
/*
Retrives data based on camparisons. Calculates min, max, and average comparison
*/
private static void getData(int[] a){
for(int q = 0; q < a.length; q++){
System.out.println("\nData results for array size " + a[q] +": ");
int sum = 0; double average = 0.0;
for(int x = 0; x < k; x++){
quicksort(stats[q], 0, stats[q].length-1);
sum += stats[q][x];
average = (double)sum/k;
}
System.out.println("\n\tMin: " + stats[q][0] +
"\n\tMax: " + stats[q][stats[q].length-1] +
"\n\tAVG: " + average);
}
}
/*
CHECKS IF ARRAY IS SORTED
*/
public static boolean verifySort(int[] a){
boolean sorted = true;
for (int i = 1; i < a.length; i++) {
if (a[i] < a[i-1]){
sorted = false;
}
}
return sorted;
}
/*
* QUICK SORT
*/
private static void quicksort(int[] a, int bottom, int top) {
if (bottom < top) {
int p = partition(a, bottom, top);
quicksort(a, bottom, p - 1);
quicksort(a, p + 1, top);
}
}
private static int partition(int[] a, int bottom, int top) {
//Keeps track of what is being switched
int save = a[bottom];
while (bottom != top) {
while (bottom < top && save <= a[top]) {
top--;
qcount++;
}
if (bottom != top) {
//swap
a[bottom] = a[top];
}
//Moves bottom until finding value > save/top
while ((bottom < top) && (save >= a[bottom])) {
qcount++;
bottom++;
}
if (bottom != top) {
//swap
a[top] = a[bottom];
}
}
a[bottom] = save;
return bottom;
}
/*
Prints the array being passed through
*/
private static void print(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
if (i < a.length - 1) {
System.out.print(", ");
}
}
System.out.println("\n");
}
/*
* Create an array of type int and populate with random
* numbers
*/
private static void populateArray(int[] a) {
Random rand = new Random();
for (int i = 0; i < a.length; i++) {
a[i] = rand.nextInt(5000) + 1;
}
}
}
这些结果是否正常?我只是试图找到一个运行的地方,它使用最小数量的比较,基于重复的所有运行的最大值和平均值。
我放置qcount的地方错了吗? Qcount是比较量的计数器。或者我在3d数组中存储数据的方式是错误的?我访问它们的方式? getData()方法错了吗?
任何帮助将不胜感激!谢谢!