用快速排序写出程序对下列序列进行排序,49,38,65,97,13,27,49
1个回答
展开全部
提供C语言的快速排序代码供参考
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void quicksort(int a[], int low, int high)
{
if (low >= high) return;
int first = low;
int last = high;
int key = a[first];
while (first<last)
{
while (first<last&&a[last]>=key) --last;
a[first] = a[last];
while (first<last&&a[first] <= key) ++first;
a[last] = a[first];
}
a[first] = key;
quicksort(a, low, first - 1);
quicksort(a, first + 1, high);
}
void print(int a[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
int main()
{
int a[] = {49,38,65,97,13,27,49};
quicksort(a,0,6);
printf("排序结果:\n");
print(a,7);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询