C 如何修改此快速排序算法的枢轴选择?

C 如何修改此快速排序算法的枢轴选择?,c,algorithm,sorting,random,quicksort,C,Algorithm,Sorting,Random,Quicksort,我想修改列出的快速排序算法,将枢轴选项选择为1)数组中的随机元素。2) 数组中第一个中间元素和最后一个元素的中间值。 ==更新==== 另外,如何实现计时机制来为每个算法的运行时间计时 void QuickSort1(long *L, int first, int last) { int SplitPoint; /* Value to Split list around */ if( first < last ) { /* Use first elemen

我想修改列出的快速排序算法,将枢轴选项选择为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 */
}
void QuickSort1(长*L,int first,int last)
{
int SplitPoint;/*要拆分列表的值*/
如果(第一次<最后一次)
{
/*使用第一个元素作为轴(用于拆分)*/
SplitPoint=first;/*将SplitPoint分配给第一个索引*/
拆分(L、第一个、最后一个和拆分点);/*围绕拆分点拆分列表*/
快速排序1(L,第一,拆分点-1);/*对列表的第一部分进行排序*/
快速排序1(L,拆分点+1,最后一个);/*对列表的第二部分进行排序*/
}
}
无效拆分(长*L、整数优先、整数最后、整数*拆分点)
/*围绕SplitPoint拆分列表,使所有值都位于
SplitPoint小于SplitPoint,且所有值位于
拆分点>=拆分点*/
{
int x;/*键*/
int未知;/*未知值的索引*/
x=L[*拆分点];/*将x指定给拆分点处的值*/
互换(&L[*拆分点],&L[第一位]);
*拆分点=第一个;
/*循环遍历列表的未知部分*/

对于(unknown=first+1;unknown来说,秘密在于您总是选择数组中的第一个元素作为轴心点。如果您希望使用另一个元素,这是一个非常好的主意,如果输入已经排序,或者(常见情况)大部分排序后添加了一些新元素,那么请将其与第一个元素交换。现在轴心是第一个元素


有了这样的洞察力,任务应该变得非常简单。要选择一个随机轴,请生成一个随机数)到N-1,并与第一个元素交换。要取中间值3,请编写一组非常混乱但直接的if…else语句。

您应该为编程语言添加标记。代码有一条注释,说明它在何处选择枢轴元素。请让它选择另一个。