【问题标题】:Java swap and comparisons in Bubble sort冒泡排序中的 Java 交换和比较
【发布时间】:2015-03-22 19:29:09
【问题描述】:
public class SortingArray
{
   public static void main(String[] args)
   {
      int[] array1 = { 1, 2, 3, 4, 5};
      bubbleSort(array1);
   }

   public static void bubbleSort(int[] array2)
   {      
      // Display the array's contents.
      System.out.println("Original order: ");
      for (int element : array2)
         System.out.print(element + " ");

      int lastPos;     // Position of last element to compare
      int index;       // Index of an element to compare
      int temp;        // Used to swap to elements
        int count = 0;
      int count2 = 1;

      // The outer loop positions lastPos at the last element
      // to compare during each pass through the array. Initially
      // lastPos is the index of the last element in the array.
      // During each iteration, it is decreased by one.
      for (lastPos = array2.length - 1; lastPos >= 0; lastPos--)
      {
         // The inner loop steps through the array, comparing
         // each element with its neighbor. All of the elements
         // from index 0 thrugh lastPos are involved in the
         // comparison. If two elements are out of order, they
         // are swapped.
         for (index = 0; index <= lastPos - 1; index++)
         {
            count2++;
            // Compare an element with its neighbor.
            if (array2[index] > array2[index + 1])
            {
                    count++;
               // Swap the two elements.
               temp = array2[index];
               array2[index] = array2[index + 1];
               array2[index + 1] = temp;
            }
         }
      }
      count2++;

      // Display the array's contents.
      System.out.println("\nSorted order: ");
      for (int element : array2)
         System.out.print(element + " ");

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


   }

}

我正在尝试在一个简单的冒泡排序程序中计算比较和交换,并且我知道我计算了正确数量的交换(在本例中为 0),但我不知道如何跟踪比较.我必须使用这个特定的代码,只需稍作修改。有人可以帮助我了解使用这个硬编码的 int 数组将进行多少次比较。现在,当我运行程序时,我得到 12,我认为这是正确的,但我并不是 100% 肯定。

【问题讨论】:

  • 为什么count2 在所有循环之后都在末尾递增?它只能在(或即将发生的)比较(值)之后(或之前)递增,其中只有一个这样的表达式。识别它,然后确保前面的规则成立。
  • 而count2为什么要以1开头?
  • 当我在外观中尝试它时,我得到了 16 次比较,这对于这个数组中的数字来说似乎太高了。
  • @fabian 这是我的一个错误,抱歉,我一直在疯狂地试图弄清楚这一点,并且几乎尝试了所有方法。
  • 现在我已经更改了 count2 = 0 并将 count2 放在外部 for 循环中,我得到了 15 个比较

标签: java


【解决方案1】:

编辑:原始代码确实正确实现了冒泡排序。

所以,请保留您的原始代码,只需使用下面的代码作为参考,以获取正确的比较计数。

无论如何,仅供参考,这里是另一个常见的实现,具有正确的交换和比较计数:

public class SortingArray
{
   public static void main(String[] args)
   {
      int[] array1 = { 5, 4, 3, 2, 1};
      bubbleSort(array1);
   }

   public static void bubbleSort(int[] array2)
   {      
      // Display the array's contents.
      System.out.println("Original order: ");
      for (int element : array2)
         System.out.print(element + " ");

      //int lastPos;     // Position of last element to compare
      int index;       // Index of an element to compare
      int temp;        // Used to swap to elements
      int count = 0;
      int count2 = 0;
      boolean swapped = true;


      while (swapped == true)
      {
         swapped = false;
         // The inner loop steps through the array, comparing
         // each element with its neighbor. All of the elements
         // from index 0 thrugh lastPos are involved in the
         // comparison. If two elements are out of order, they
         // are swapped.
         for (index = 0; index < array2.length - 1; index++)
         {
            count2++;
            // Compare an element with its neighbor.
            if (array2[index] > array2[index + 1])
            {
               count++;
               // Swap the two elements.
               temp = array2[index];
               array2[index] = array2[index + 1];
               array2[index + 1] = temp;
               swapped = true;
            }

         }
      }
      //count2++;

      // Display the array's contents.
      System.out.println("\nSorted order: ");
      for (int element : array2)
         System.out.print(element + " ");

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


   }

}

输出:

Original order: 
5 4 3 2 1 
Sorted order: 
1 2 3 4 5 
 Swaps:10
 Comparisons:20

对于您的排序示例:

int[] array1 = { 1, 2, 3, 4, 5};

输出:

Original order: 
1 2 3 4 5 
Sorted order: 
1 2 3 4 5 
 Swaps:0
 Comparisons:4

【讨论】:

  • 谢谢!我认为这是我可以做出的改变,即使它不是给我的特定代码,这更有意义。
  • @Justin:不过,您的冒泡排序在最坏的情况下使用较少的比较。我几乎可以肯定它是正确的。
  • Justin 的代码确实正确地实现了冒泡排序。它只考虑在n-th 迭代之后数组的最后一个n 元素包含n 最大值。您的算法的另一个区别是,如果没有切换任何元素,您就会停止,这很容易添加到 Justin 的代码中。
  • @fabian,你是对的。原始算法实际上是冒泡排序的正确实现。我刚刚检查了我的 CLRS 算法书。我会更新答案。
  • @Daniel Nugent 和 Fabian 感谢我刚开始学习 Java 排序算法的所有帮助,我有点难以理解所有不同的方法,我仍然必须向该程序添加选择、插入和快速排序。虽然我很确定一旦我把这件事做对了,我会过得更轻松。再次感谢您!
猜你喜欢
  • 2018-10-01
  • 2018-05-10
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
相关资源
最近更新 更多