#include<stdio.h>
typedef struct Array
{
int key;
}Array;
#define LENGTH 8
void QuickSort(Array A[], int low, int high);
int Partition(Array A[], int low, int high);
int main() {
int arr[LENGTH] = { 2,15,3,6,34,32,23,63 };
int index = 1;
Array A[LENGTH+1];
while (index <= LENGTH) {
A[index].key = arr[index-1];
printf("排序前:%d\n", A[index].key);
index++;
}
QuickSort(A,1,LENGTH);
index = 1;
while (index <= LENGTH) {
printf("排序后:%d\n", A[index].key);
index++;
}
}
int Partition(Array A[],int low,int high) {
int pivot = A[low].key;
while (low<high)
{
while (low < high&&A[high].key >= pivot) {
--high;
}
A[low] = A[high];
while (low < high&&A[low].key <=pivot){
++low;
}
A[high] = A[low];
}
A[low].key = pivot;
return low;
}
void QuickSort(Array A[],int low,int high) {
if (low < high) {
int pivotpos = Partition(A,low,high);
QuickSort(A, low, pivotpos - 1);
QuickSort(A, pivotpos + 1, high);
}
}
C:C的排序算法:快速排序(QuickSort)
最新推荐文章于 2022-06-16 14:08:34 发布