3

首先,我看到了一个与 C++ 相关的类似问题,但我不太明白 - 加上我的问题是关于 Java 的。

基本上,我编写了两种可以在解析的数组上使用 SelectionSort 和 BubbleSort 的方法。虽然我相信这些方法可以正常工作(我已经运行测试并且它们都按升序对数字进行了排序),但我不确定我是否我正在正确计算比较次数和交换次数。如果有人能够在下面测试我的代码并提供一些反馈,我将非常感激。

注意:如果需要,我可以压缩我的 Java 项目文件并将它们发送给任何人。

冒泡排序方法:

public String bubbleSort(int[] numbers)
    {
        System.out.println("******|Bubble Sort|******");
        StringBuilder originalArray = new StringBuilder();

        for(int i = 0; i <= numbers.length - 1; i++)
        {
            originalArray.append(numbers[i] + " ");
        }
        System.out.println("Original array: " + originalArray);
        int temp; // temporary variable

        //Set boolean variable to true, 
        //to allow the first pass.
        boolean pass = true;

        int comparisons = 0;
        int swaps = 0;

        //While a pass can be made, 
        while(pass)
        {
            //Set the boolean value to false, 
            //indicating a number swap could 
            //be made.
            pass = false;

            for(int i = 0; i < numbers.length - 1; i++)
            {
                //increment the number of comparisons by 1.
                comparisons++;
                if(numbers[i] > numbers[i+1])
                {
                    temp = numbers[i];
                    numbers[i] = numbers[i + 1];
                    numbers[i+1] = temp;

                    //increment the amount of swaps made by 1, 
                    //to put numbers in correct order.
                    swaps++;
                    pass = true;
                }
            }
        }

        //Create a StringBuilder object - to hold 
        //the output of sorted numbers.
        StringBuilder sb = new StringBuilder();

        //Loop through the now sorted array - appending 
        //each subsequent number in the array to the 
        //StringBuilder object.
        for(int i = 0; i < numbers.length; i++)
        {
            sb.append(numbers[i] + " ");
        }

        //Return the final results of the sorted array.
        return "Sorted Array (asc): " + sb.toString() + "\nComparisons made: " + comparisons 
                + "\nSwaps made: " + swaps;
    }

选择排序方法

public String selectionSort(int[] numbers)
    {
        System.out.println("******|Selection Sort|******");
        StringBuilder originalArray = new StringBuilder();

        int comparisons = 0;
        int swaps = 0;

        for(int i = 0; i <= numbers.length - 1; i++)
        {
            originalArray.append(numbers[i] + " ");
        }
        System.out.println("Original array: " + originalArray);

        //Declare variable to hold first element
        int first;

        //declare temporary variable, to be used in 
        //swapping integers.
        int temp;

        for(int x = numbers.length - 1; x > 0; x--)
        {
            first = 0;
            comparisons++;
            for(int y = 1; y <= x; y++)
            {
                //comparisons++;
                if(numbers[y] > numbers[first])
                {
                    first = y;
                    //comparisons++;
                    swaps++;
                }
                temp = numbers[first];
                numbers[first] = numbers[x];
                numbers[x] = temp;
                //swaps++;
            }
        }

        //Create a StringBuilder object - to hold 
        //the output of sorted numbers.
        StringBuilder sb = new StringBuilder();

        //Loop through the now sorted array - appending 
        //each subsequent number in the array to the 
        //StringBuilder object.
        for(int i = 0; i < numbers.length; i++)
        {
            sb.append(numbers[i] + " ");
        }

        //Return the final results of the sorted array.
        return "Sorted Array (asc): " + sb.toString() + "\nComparisons made: " + comparisons 
                + "\nSwaps made: " + swaps;
    }
4

1 回答 1

5

对于冒泡排序:

主要比较 -> (n*(n-1))/2

项目分配(交换)-> 3*(n-1)

对于选择排序:

主要比较 -> (n*(n-1))/2(与气泡相同)

项目分配(交换)-> (n*(n-1))/4

(请注意,n是您的数组大小的数量)

于 2014-12-05T17:37:56.970 回答