冒泡排序中的Java交换和比较

时间:2015-03-22 19:29:09

标签: java

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%正面。

1 个答案:

答案 0 :(得分:0)

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

因此,请保留原始代码,只需使用以下代码作为参考,即可获得正确的比较计数。

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

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