【问题标题】:How to count comparisons and swaps in quicksort?如何计算快速排序中的比较和交换?
【发布时间】:2015-05-21 08:32:45
【问题描述】:
public class IntQuickSorter
{
   public static int numOfComps = 0,
                     numOfSwaps = 0;

    public static void main(String[] args)
   {
      // Create an int array with test values.
      int[] values = { 1, 2, 3, 4, 5,  6 };
      //int[] values = { 5, 1, 3, 6, 4, 2 };
      //int[] values = { 5, 7, 2, 8, 9, 1 };

        System.out.println("\n\nQuick Sort:");

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

      // Sort the array.
      quickSort(values);

      //System.out.println("\n\nNumber of comps = " + numOfComps);
      //System.out.println("Number of swaps = " + numOfSwaps);

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

      System.out.println();
   }

   public static void quickSort(int array[])
   {
      doQuickSort(array, 0, array.length - 1);
      System.out.println("\n\nNumber of comps = " + numOfComps);
       System.out.println("Number of swaps = " + numOfSwaps);
   }

   private static void doQuickSort(int array[], int start, int end)
   {
      int pivotPoint;

      if (start < end)
      {
         numOfComps++;

         // Get the pivot point.
         pivotPoint = partition(array, start, end);

         // Sort the first sub list.
         doQuickSort(array, start, pivotPoint - 1);

         // Sort the second sub list.
         doQuickSort(array, pivotPoint + 1, end);
      }
   }

   private static int partition(int array[], int start, int end)
   {
      int pivotValue;    // To hold the pivot value
      int endOfLeftList; // Last element in the left sub list.
      int mid;           // To hold the mid-point subscript

      // Find the subscript of the middle element.
      // This will be our pivot value.
      mid = (start + end) / 2;

      // Swap the middle element with the first element.
      // This moves the pivot value to the start of 
      // the list.
      swap(array, start, mid);

      // Save the pivot value for comparisons.
      pivotValue = array[start];

      // For now, the end of the left sub list is
      // the first element.
      endOfLeftList = start;

      // Scan the entire list and move any values that
      // are less than the pivot value to the left
      // sub list.
      for (int scan = start + 1; scan <= end; scan++)
      {

         if (array[scan] < pivotValue)
         {
            endOfLeftList++;
            swap(array, endOfLeftList, scan);

                numOfSwaps ++;
         }
         numOfComps++;
      }

      // Move the pivot value to end of the
      // left sub list.
      swap(array, start, endOfLeftList);

      // Return the subscript of the pivot value.
      return endOfLeftList;
   }

   private static void swap(int[] array, int a, int b)
   {
      int temp;

         temp = array[a];
         array[a] = array[b];
         array[b] = temp;
    }
}

如何用 Java 编写这个快速排序程序来计算比较次数和交换次数?我现在有交换代码的地方,即使有一个排序的数字数组,它也会计算交换,它不应该。比较代码是否在正确的位置?感谢您的帮助。

【问题讨论】:

  • 您是否尝试将计数器编号放入swap(),并每次都增加它的值,它被调用了?同样,您应该创建一个compare() 函数,在其中比较两个整数,并在每次调用时递增一个全局变量。
  • @Nagy 嗨,Nagy,我是计算机编程的新手;我有不到一年的时间。是的,我放置了交换计数器“numOfSwaps ++;”进入swap(),它用6个数字的排序数组计算了3次交换,它不应该计算排序数组中的交换。我如何编码来修复它?如何编写 compare() 方法?我已包含我的代码?

标签: java quicksort


【解决方案1】:

您可以使用以下方法http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-
据我记得它使用快速排序算法
当您实现自己的比较器时,您可以在调用比较器时增加计数器(比较次数),并在一个值大于另一个值时增加其他计数器(然后发生交换)。

【讨论】:

  • 没有。 Collections.sort 不是快速排序。它叫做TimSort。来自Java 7 docsImplementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered.
  • 但是,如果你想计算内置排序的比较并将其与 QuickSort 之类的东西进行比较,你可以调用 Collections.sort(list, myComparator) 并传入你自己的 Comparator,它有一个简单的 int,每次递增称呼。我不确定你如何以类似的方式计算掉期......
  • 如果需要,在比较后进行交换操作 - 取决于比较结果
【解决方案2】:

我现在有交换代码的地方,即使有一个排序的数字数组,它也会计算交换,它不应该。

这不太准确。排序后的数组与快速排序进行一些交换,这就是旋转和分区的工作方式。这是使用排序列表的典型快速排序的动画。 Animationgist

另外,删除这个比较:

if (start < end)
      {
         numOfComps++;

因为这不是数组中两个事物的“关键比较”,只是您的索引。
除此之外,您的比较和交换看起来是在正确的地方。

【讨论】:

  • 这是一个非常酷的动画 gist 程序。你自己创造的吗?非常感谢您的帮助。
  • 我没有创建原始程序(尽管我将它分叉并使用 mergesort 和其他一些程序进行设置)。当我cited above 一个名为@mbostock 的用户创建它时。他的所有要点(只是托管在 GitHub 上的一小段代码)都位于 gist.github.com/mbostock
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
相关资源
最近更新 更多