快速排序
Posted brainstorm-yc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速排序相关的知识,希望对你有一定的参考价值。
代码
#include<iostream>
/*一趟快速排序*/
#define N 8
int Partition(int 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(int 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(int A[]) {
int i;
for (i = 0; i < N; i++)
{
printf("%d ", A[i]);
}
}
int main() {
int 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;
}
以上是关于快速排序的主要内容,如果未能解决你的问题,请参考以下文章