#include<stdio.h>
#define MaxSize 5
int Swap(int *i, int *j){
int tmp;
tmp=*j;
*j=*i;
*i=tmp;
}
void Qsort(int A[], int L,int R) { //a数组保存数据,L和R是边界
if (L >= R)
return; //当前区间元素个数<=1则退出
int key, i = L, j = R; //i和j是左右两个数组下标移动
//把a数组中随机一个元素和A[L]交换 //快排优化,使得基准值的选取随机
key = A[L]; //key作为枢值参与比较
while (i < j) {
while (i<j && A[j]>key)
j--;
while (i < j && A[i] <= key)
i++;
if (i < j)
Swap(&A[i], &A[j]); //交换A[i]和A[j]
}
Swap(&A[L], &A[i]);
Qsort(A, L, i-1); //递归处理左区间
Qsort(A, i+1, R); //递归处理右区间
}
void output(int A[],int maxsize){
int j=maxsize;
for(int i=0;i<j;i++){
printf("%d ", A[i]);
}
}
int main() {
int a[MaxSize] = { 7, 2, 3, 4, -1 };
printf("原始序列为:");
output(a,MaxSize);
printf("\n快速排序后:");
Qsort(a,0,MaxSize-1);
output(a,MaxSize);
}
标签:tmp,int,快排,MaxSize,Swap,key
From: https://www.cnblogs.com/Devoir/p/16600643.html