Java-使用快速排序查找最小、最大和平均比较数

Java-使用快速排序查找最小、最大和平均比较数,java,sorting,comparison,Java,Sorting,Comparison,有了这段代码,我应该得到一些大小分别为2048、4096、8192和16384的数组。使用1-1000之间的随机整数填充它们,使用快速排序对它们进行排序,然后根据要重复循环的次数显示比较的最小值、最大值和平均值 问题是,当我选择只重复这个过程一次时,最小最大值和平均值都是相同的数字。这是控制台: WELCOME TO QuickSortTest! Please enter how many times you would like to repeat the test: 1 *****

有了这段代码,我应该得到一些大小分别为2048、4096、8192和16384的数组。使用1-1000之间的随机整数填充它们,使用快速排序对它们进行排序,然后根据要重复循环的次数显示比较的最小值、最大值和平均值

问题是,当我选择只重复这个过程一次时,最小最大值和平均值都是相同的数字。这是控制台:

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;
    }
  }
}
import java.util.*;
公共类QuickSortTest{
静态int qcount=0,k=0;
静态int[]大小;
静态int[][]统计信息;
公共静态void main(字符串[]args){
System.out.println(“欢迎使用QuickSortTest!\n”);
扫描仪控制台=新扫描仪(System.in);
System.out.println(“请输入您希望重复测试的次数:”);
k=console.nextInt();
int[]size={20484096819216384};
stats=newint[4][k];//[sizeArray][totalRepeats]
对于(int j=1;j=a[底部]){
qcount++;
底层++;
}
如果(底部!=顶部){
//交换
a[顶部]=a[底部];
}
}
a[底部]=保存;
返回底部;
}
/*
打印正在通过的数组
*/
专用静态无效打印(int[]a){
for(int i=0;i
这些结果正常吗?我只是想找到一个运行通过,它使用了最小数量的比较,最大值,以及基于重复的所有运行通过的平均值

我放qcount的地方错了吗?Qcount是比较量的计数器。或者我在3d阵列中存储数据的方式是错误的?我怎么接近他们?getData()方法错误吗


任何帮助都将不胜感激!谢谢

我猜你的问题与静态int[][]统计有关;您应该尽量避免为这些目的使用静态变量。尽量使用局部变量。@JFMeier这也是我猜的。。。我只是想弄清楚如何修复它将静态变量移到程序顶部,并使用局部变量。如果您需要不同方法中的变量,请将它们作为参数传递。@JFMeier在我将k设置为1(用于重复)时仍在做同样的事情。对不起,我不明白您所说的。你研究过静态变量吗?我想你的问题与静态int[][]统计有关;您应该尽量避免为这些目的使用静态变量。尽量使用局部变量。@JFMeier这也是我猜的。。。我只是想弄清楚如何修复它将静态变量移到程序顶部,并使用局部变量。如果您需要不同方法中的变量,请将它们作为参数传递。@JFMeier在我将k设置为1(用于重复)时仍在做同样的事情。对不起,我不明白您所说的。你有没有研究过静态变量?