【问题标题】:How can I modify the pivot choice for this quick sort algorithm?如何修改此快速排序算法的枢轴选择?
【发布时间】:2017-02-25 17:41:10
【问题描述】:

我想修改列出的快速排序算法以选择枢轴选项为 1) 数组中的随机元素。 2) 数组中第一个中间元素和最后一个元素的中位数。 ====更新==== 另外,我如何实现一种计时机制来为每个算法的运行时间计时?

void QuickSort1(long *L, int first, int last)
{
   int SplitPoint;  /* Value to Split list around */

   if( first < last )
   {
      /* Use first element as pivot (for splitting) */
      SplitPoint = first;                 /* assign SplitPoint to 1st index */
      Split(L, first, last, &SplitPoint); /* Split List around SplitPoint */
      QuickSort1(L, first, SplitPoint-1);  /* Sort 1st section of list */
      QuickSort1(L, SplitPoint+1, last);   /* Sort 2nd section of list */
   }
}

void Split(long *L, int first, int last, int *SplitPoint)
/* Splits a list around SplitPoint such that all values to the left of
   SplitPoint are < than SplitPoint and all values to the right of
   SplitPoint are >= SplitPoint */
{
   int x;            /* Key */
   int unknown;      /* index of unknown value */

   x = L[*SplitPoint];   /* assign x to value at SplitPoint */
   Swap( &L[*SplitPoint], &L[first] ); 
   *SplitPoint = first; 

   /* Loop walks through unknown portion of list */
   for ( unknown = first+1; unknown <= last; ++unknown)
   {
      /* If unknown value is < SplitPoint Value, then: */
#ifdef TAKE_COUNT
         comparison_count++;
#endif
      if( L[unknown] < x ) {
         ++ (*SplitPoint);                     /* SplitPoint is incremented */
         Swap( &L[*SplitPoint], &L[unknown] ); /* values are swapped*/
      }
   }
   /* Original value which was being split upon is swapped with the current
      SplitPoint to put it in correct position */
   Swap( &L[first], &L[*SplitPoint] );
}

int FindMedian(long *L, int A, int B, int C)
/* Receives array and three respective indices in the array. */
/* Returns the index of the median. */
{
   if (L[A] < L[B])
     if (L[B] < L[C])       /*  A < B < C  */
       return (B);
     else
       if (L[A] < L[C])         /*  A < C < B  */
     return (C);
       else
     return (A);            /*  C < A < B  */
   else
     if (L[A] < L[C])           /*  B < A < C  */
       return (A);
     else
       if (L[B] < L[C])     /*  B < C < A  */
     return (C);
       else
     return (B);            /*  C < B < A  */
}

void Swap(long *a, long *b)
/* This function uses call by reference to switch two elements.*/
{
   long temp;    /* temporary variable used to switch. */

   temp = *a;   /* temp = 1st # */
   *a = *b;     /* 1st # = 2nd # */
   *b = temp;   /* 2nd # = temp */
}

我如何能够针对列出的枢轴选项进行修改。它们还必须作为程序的附加菜单选项添加。

【问题讨论】:

  • 你应该为编程语言添加标签
  • 代码有一个注释,说明它在哪里选择了枢轴元素。让它选择一个不同的。

标签: c algorithm sorting random quicksort


【解决方案1】:

秘诀在于您始终选择数组中的第一个元素作为枢轴点。如果您希望使用另一个元素,如果输入已经排序或(通常情况下)主要排序并附加了一些新元素,这是一个非常好的主意,请将其与第一个元素交换。现在枢轴是第一个元素。

有了这种洞察力,任务应该变得非常简单。要选择随机枢轴,请生成 ) 到 N-1 的随机数,并与第一个元素交换。要取三的中位数,请编写一组看似凌乱但直截了当的 if ... else 语句。

【讨论】:

    猜你喜欢
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多