【问题标题】:Java bubble sorter needs to count swaps and comparisonsJava 冒泡排序器需要计算交换和比较
【发布时间】:2016-11-25 22:38:39
【问题描述】:

我对 Java 还很陌生。我正在一个 int 数组上创建排序算法,这样每个方法都会计算它们进行的交换和比较的数量。我在显示原始订单、排序订单、掉期和比较时遇到问题。不知道我必须做些什么来修复它。任何帮助将不胜感激。

public class IntBubbleSorter {

    public static void bubbleSort(int[] array)
    {
        int lastPos;
        int index;
        int temp;
        int count = 0;
        int count2 = 0;

        for (lastPos = array.length - 1; lastPos >= 0; lastPos--)
          {
            for (index = 0; index <= lastPos - 1; index++)
            {
                count2++;

                if (array[index] > array[index + 1])
                {
                    count++;
                    temp = array[index];
                    array[index] = array[index + 1];
                    array[index + 1] = temp;
                }
            }
          }
        System.out.print("\n Swaps:" + count);

        System.out.print("\n Comparisons:" + count2);
    }
}


public class 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();
    }
}

//这是输出的内容

Original Order: 
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 
 Swaps:80
 Comparisons:300
Sorted order: 
1 11 21 22 32 32 33 34 35 49 53 54 59 65 68 78 80 86 90 96 123 265 3123 25435 44324 

【问题讨论】:

  • 你为什么认为这不正确?
  • 主要方法是获取原始订单,然后转到冒泡排序器并获取交换和比较,然后返回主要方法进行排序,这不是我想要的顺序,而是任何东西我试试不行。我将创建更多使用相同主要方法的排序方法,我认为它会变得非常混乱和混乱。我应该有某种“获取”方法来获取交换和比较吗?
  • 这似乎很合理。您可能会考虑创建某种抽象基类来实现比较和交换功能,然后您的各种排序实现可以继承通用功能。我相信Sedgewick 采用了这种方法。

标签: java sorting bubble-sort


【解决方案1】:

根据您的评论,您可以将计数变量设为静态并将输出放在 main 方法中,如下所示:

public class IntBubbleSorter {

    public static int count = 0;

    public static int count2 = 0;

    public static void bubbleSort(int[] array)
    {
        int lastPos;
        int index;
        int temp;
        count = 0;
        count2 = 0;

        for (lastPos = array.length - 1; lastPos >= 0; lastPos--)
          {
            for (index = 0; index <= lastPos - 1; index++)
            {
                count2++;

                if (array[index] > array[index + 1])
                {
                    count++;
                    temp = array[index];
                    array[index] = array[index + 1];
                    array[index + 1] = temp;
                }
            }
          }
    }
}


public class 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 Swaps:" + IntBubbleSorter.count);

        System.out.print("\n Comparisons:" + IntBubbleSorter.count2);
    }
}

但我要提一下,static 应该谨慎使用。

【讨论】:

  • 我已经尝试过了,但是你有 System.out.print("\n Swaps:" + count);和 System.out.print("\n 比较:" + count2);我收到“count”和“count2”的错误。它要我为他们创建一个局部变量或字段。
  • @ducatigirl74 糟糕,我太急于回答,以至于我错过了您的代码示例包含两个不同的类。现在应该可以工作了。
  • 看起来它会起作用,但我仍然遇到错误。 "更改计数(或计数 2)的可见性以为它们打包或创建 getter 和 setter。
  • @ducatigirl74 奇怪,你用的是什么IDE?不过把public static int改成static int就可以解决问题了。
  • 我在 Mac 上使用 Eclipse Neon。我已将 JRE 从 8 更改为 7,因为它们有时都会给出不同的错误。我到底在哪里取出“公共静态整数”?我将再次显示代码。
猜你喜欢
  • 1970-01-01
  • 2018-10-01
  • 2018-05-10
  • 2013-03-10
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
相关资源
最近更新 更多