快速排序算法

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); }

 

posted @ 2014-03-25 18:57  samu  阅读(243)  评论(0编辑  收藏  举报