快速排序算法

void swap(void *v[], int a, int b)

{

    void* tmp;

    tmp = v[a];

    v[a]=v[b];

    v[b]=tmp;

}



/* qsort: sort v[left]...v[right] into increasing order */

void qsort(void *v[], int left, int right, int (*comp)(void *, void *))

{

  int i, last;

  if (left >= right) /* do nothing if array contains */

    return;

  /* fewer than two elements */

  swap(v, left, (left + right)/2);

  last = left;

  for (i = left+1; i <= right; ++i )
  {
    if ((*comp)(v[i], v[left]) < 0)
    {
      if( i == last ) ++last;
else swap(v,
++last, i);
    }
  }
  swap(v, left, last);   qsort(v, left, last
-1, comp);   qsort(v, last+1, right, comp); }

 

你可能感兴趣的:(快速排序)