代码
#include<iostream>
/*一趟快速排序*/
#define N 8
#define ElemType int
int Partition(ElemType A[], int low, int high) {
int pivot = A[low];
while (low<high)
{
while (low < high&&A[high] >= pivot) --high;
A[low] = A[high];
while (low < high&&A[low] <= pivot) ++low;
A[high] = A[low];
}
A[low] = pivot;
return low;
}
/*快速排序*/
void QuickSort(ElemType A[],int low,int high) {
if (low < high) {
int pivotops = Partition(A, low, high);
QuickSort(A, low, pivotops - 1);
QuickSort(A, pivotops + 1, high);
}
}
/*输出数组*/
void OutPrint(ElemType A[]) {
int i;
for (i = 0; i < N; i++)
{
printf("%d ", A[i]);
}
}
int main() {
ElemType i,low=0,high=N-1;
int A[N] = { 48, 62, 35, 77, 55,14,35,98 };
printf("排序前数组
");
OutPrint(A);
QuickSort(A, low, high);
printf("
排序后数组
");
OutPrint(A);
system("pause");
return 0;
}
结果如图: