c语言快速排序算法代码

#include <stdio.h>
void print(int arr[], int length)
{
for(int i = 0; i < length; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}

void Qsort(int arr[], int low, int high)
{
if(low>=high)
return ;
int key = arr[low];
int i=low;
int j=high;
while(i<j)
{
// 从右边往左边找第一个小于key的
while(i<j)
{
if(arr[j] < key)
{
arr[i] = arr[j];
break;
}
j--;
}
//print(arr,high-low+1);
//从左边往右边找第一个大于key的
while(i<j) {
if (arr[i] > key) {
arr[j] = arr[i];
break;
}
i++;
}
//print(arr,high-low+1);
}
arr[i] = key;
//print(arr,high-low+1);
Qsort(arr, low, i-1);
Qsort(arr,i+1, high);

}



int main()
{
int a[10] = {72,6,57,88,60,42,83,73,48,85};
print(a,10);
Qsort(a, 0, 9);
print(a,10);
return 0;
}
posted @ 2020-09-22 19:42  牛牛码代码  阅读(1895)  评论(0编辑  收藏  举报