java - 如何计算快速排序中的比较和交换?

标签 java quicksort

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 编写这个快速排序程序来计算比较次数和交换次数?在我现在有交换代码的地方,即使使用已排序的数字数组,它也会对交换进行计数,但它不应该这样做。比较代码是否位于正确的位置?感谢您的帮助。

最佳答案

Where I have the swap code now, it counts swaps even with a sorted array of numbers and it shouldn't.

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

quicksort-on-sorted

此外,删除此比较:

if (start < end)
      {
         numOfComps++;

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

关于java - 如何计算快速排序中的比较和交换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29124070/

相关文章:

java - 保留默认值并避免异常是否合法?

java - 生产服务器上的 Tomcat、PermGen 和重新部署

algorithm - 在 Quicksort 中,为什么分区步骤从低 - 1 和高 + 1 开始?

c - 对文件进行快速排序

Java在设置组件大小时重用Dimension对象

java - 如何在 Mac 上使用 SceneBuilder 和 IntelliJ

java - 无法将相对路径 ~/.gradle/daemon/6.4.1 转换为绝对文件

java - 为什么使用排序(O(n log n) 复杂度)比使用 HashMap(O(n) 复杂度)更快地找到多数元素?

algorithm - Jon Bentley 漂亮的快速排序——它是如何工作的?

algorithm - QuickSort分区的循环不变量