快速排序

# include <stdio.h>
# include <stdlib.h>

void quickSort(int * , int, int);
int findPos(int * , int, int);

int main(void)
{
 int a[6] = {8, 7 , 16, 3, 4, 12};
 int i;

 quickSort(a, 0, 5);

 for (i=0; i<6; i++)
  printf("%d ", a[i]);
 printf("/n");

 return 0;
}

void quickSort(int * a, int low, int high)
{
  int position;

  if (low < high)
  {
   position = findPos(a, low, high);

   quickSort(a, low, position-1);
   quickSort(a, position+1, high);
  }
}

int findPos(int * a, int low, int high)
{
 int val = a[low];

 while (low < high)
 {
  while (low < high && a[high] >= val)
   high--;
  a[low] = a[high];

  while(low < high && a[low] <= val)
   low++;
  a[high] = a[low];
 }
 a[low] = val;
 return low;
}

你可能感兴趣的:(include)