为什么我的排序算法会返回相同数量的比较?

时间:2018-04-28 22:44:16

标签: java sorting bubble-sort insertion-sort selection-sort

我的程序应该运行一个用随机值填充数组的循环,然后使用选择排序,增强的冒泡排序和插入排序对数组进行排序。问题是它每次都返回相同的比较计数。

为什么我的代码会继续返回相同的比较计数?

import java.util.Random;

public class AssignmentIV {

public static void main(String[] args) {
    int[] testRuns = new int[4];        //stores the values for the generation of random arrays
    testRuns[0] = 10;
    testRuns[1] = 100;
    testRuns[2] = 1000;
    testRuns[3] = 10000;

    String[] algorithms = new String[3];        //stores the names of the algorithms 
    algorithms[0] = "Selection Sort";
    algorithms[1] = "Enhanced Bubble Sort";
    algorithms[2] = "Insertion Sort";

    int countComparisons;


    for(int i=0; i<3; i++)      //loops through the different algorithms
    {
        System.out.println("SORTING ALGORITHM: " + algorithms[i] + "\n");

        for(int j=0; j<4; j++)      //loops through different amounts (10, 100, 1000, 10000) of values for each algorithm
        {
            countComparisons = 0;

            int[] test = Generate(testRuns[j]);     //creates an array filled with random values

            if(algorithms[i] == "Selection Sort")
                countComparisons = selectionSort(test);             //calls the selection sort method
            else if(algorithms[i] == "Enhanced Bubble Sort")
                countComparisons = enhancedBubbleSort(test);        //calls the enhanced bubble sort method
            else if(algorithms[i] == "Insertion Sort")
                countComparisons = insertionSort(test);             //calls the insertion sort method

            System.out.println("Number of values in array: " + testRuns[j]);            //prints the number of values in the array
            System.out.println("Number of comparisons required: " + countComparisons + "\n");           //prints the number of comparisons required to sort the array
        }
    }
}


//method to populate an array with randomized integers
public static int[] Generate(int size)
{
    int[] valueArray = new int[size]; 


    //number generator is created
    Random gen = new Random();

    //each position in an array is filled with a random
    //integer up to maximum Integer.MAX_VALUE
    for(int i=0; i<size; i++)
    {
        valueArray[i] = gen.nextInt(Integer.MAX_VALUE);
    }
    return valueArray;
}

//sorts an array using selection sort and returns the number of comparisons made
public static int selectionSort(int[] arr)
{
    int count = 0;
    int min, min_location;

    for(int i=0; i < arr.length-1; i++)
    {
        min = arr[i];
        min_location = i;
        for(int j=i+1; j < arr.length; j++)
        {
            if(arr[j] < min)
            {
                min = j;
                min_location = j;
            }
            count++;
        }

        arr[min_location] = arr[i];
        arr[i] = min;
    }
    return count;
}

//sorts an array using enhanced bubble sort and returns the number of comparisons made
public static int enhancedBubbleSort(int[] arr)
{
    int count = 0;

    for(int i=0; i < arr.length-1; i++)
    {
        for(int j=0; j < arr.length-i-1; j++)
        {
            if(arr[j] > arr[j+1])
            {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
            count++;
        }
    }

    return count;
}

//sorts an array using insertion sort and returns the number of comparisons made
public static int insertionSort(int[] arr)
{
    int count = 0;

    for(int i=1; i < arr.length; i++)
    {
        for(int j=i; j>0; j--)
        {
            if(arr[j] < arr[j-1])
            {
                int temp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = temp;
            }
            count++;
        }
    }

    return count;
}
}

这是输出:

SORTING ALGORITHM: Selection Sort

Number of values in array: 10
Number of comparisons required: 45

Number of values in array: 100
Number of comparisons required: 4950

Number of values in array: 1000
Number of comparisons required: 499500

Number of values in array: 10000
Number of comparisons required: 49995000

SORTING ALGORITHM: Enhanced Bubble Sort

Number of values in array: 10
Number of comparisons required: 45

Number of values in array: 100
Number of comparisons required: 4950

Number of values in array: 1000
Number of comparisons required: 499500

Number of values in array: 10000
Number of comparisons required: 49995000

SORTING ALGORITHM: Insertion Sort

Number of values in array: 10
Number of comparisons required: 45

Number of values in array: 100
Number of comparisons required: 4950

Number of values in array: 1000
Number of comparisons required: 499500

Number of values in array: 10000
Number of comparisons required: 49995000

1 个答案:

答案 0 :(得分:0)

这些算法中的比较次数不依赖于数据的值。你应该期待不断的计数。

要看到这一点,请注意,增加计数的位置不在'if'语句中。它只计算通过驱动排序操作的循环。