【问题标题】:Trouble with creating a Selection Sorter counting swaps and comparisons创建选择排序器计数交换和比较的问题
【发布时间】:2016-11-27 20:59:40
【问题描述】:

我在尝试使用 Java 中的选择排序来计算一个 int 数组的交换和比较次数时遇到了麻烦。我对交换和比较在循环中的位置感到困惑。任何指导将不胜感激。

    public class IntSelectionSorter {

public static int count = 0;
public static int count2 = 0;

public static void selectionSort(int[] array)
{
    int startScan;
    int index;
    int minIndex;
    int minValue;

    for (startScan = 0; startScan < (array.length - 1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];

        for (index = startScan + 1; index < array.length; index++)
        {
            count2++;
            if (array[index] < minValue)
            {
                minValue = array[index];
                count++;
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        count++;
        array[startScan] = minValue;
        count++;
    }
}

}

【问题讨论】:

  • 具体是什么问题?您希望它做什么,它会做什么?
  • 我正在为一个 int 数组创建一个选择排序器,该数组应该计算交换和比较的次数。我已经创建了一个 Bubble Sorter 来使用相同的 int 数组。我不确定我是否在此选择排序器的适当位置使用了交换 (count) 和比较 (count2)。我得到了 48 次交换和 300 次比较的答案。如果我改变放置“count”和“count2”的位置,我会得到不同的答案。我也可以发布其他文件。

标签: java selection-sort


【解决方案1】:

count2 似乎是对的。计数应更改如下:

for (startScan = 0; startScan < (array.length - 1); startScan++)
{
    minIndex = startScan;
    minValue = array[startScan];

    for (index = startScan + 1; index < array.length; index++)
    {
        count2++;
        if (array[index] < minValue)
        {
            minValue = array[index];
            // count++; delete this
            minIndex = index;
        }
    }
    if (minIndex != startScan) {
        array[minIndex] = array[startScan];
        count++;
        array[startScan] = minValue;
    }
}

基本上,仅在需要交换时才增加计数,即当 startScan 之后数组中有另一个数字小于 startScan 时的值时。

【讨论】:

  • 非常感谢您的帮助!这更有意义。但是,我得到了 0 次交换和 300 次比较。这似乎不正确。
  • 我对此进行了测试,它给出了正确的答案。检查输入数组 [2,1,4,3],它确实提供了 2 次交换(第一次用于索引 0,第二次用于索引 2)。您尝试的输入是什么?
【解决方案2】:

公共类 SortingTest {

public static void main(String[] args) 
{
    int[] values = { 1,53,86,21,49,32,90,65,33,11,34,68,54,32,78,80,35,22,96,59,265,44324,123,3123,25435};

    System.out.println("Original Order: ");
    for (int element : values) 
        System.out.print(element + " ");

    IntBubbleSorter.bubbleSort(values);

    System.out.println("\nSorted order: ");
    for (int element : values) 
        System.out.print(element + " ");

    System.out.println();

    System.out.print("\n Bubble Sort Swaps:" + IntBubbleSorter.count);
    System.out.print("\n Bubble Sort Comparisons:" + IntBubbleSorter.count2);

    System.out.println();

    System.out.println("\nOriginal Order: ");
    for (int element : values) 
        System.out.print(element + " ");

    IntSelectionSorter.selectionSort(values);

    System.out.println("\nSorted order: ");
    for (int element : values) 
        System.out.print(element + " ");

    System.out.println();

    System.out.print("\n Selection Sort Swaps:" + IntSelectionSorter.count);
    System.out.print("\n Selection Sort Comparisons:" + IntSelectionSorter.count2);

    System.out.println();

    System.out.println("\nOriginal Order: ");
    for (int element : values) 
        System.out.print(element + " ");

    IntInsertionSorter.insertionSort(values);

    System.out.println("\nSorted order: ");
    for (int element : values) 
        System.out.print(element + " ");

    System.out.println();

    System.out.print("\n Insertion Sort Swaps:" + IntInsertionSorter.count);
    System.out.print("\n Insertion Sort Comparisons:" + IntInsertionSorter.count2);
}

}

【讨论】:

    猜你喜欢
    • 2015-07-08
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多