解题思路:使用快速排序完成数组的排序,虽然比较复杂一点,而且对于这个有顺序的序列显然效果是最不好的,只是练习一下。
注意事项:
参考代码:
#include<iostream>
using namespace std;
int Partition(int a[], int low,int high){
int pivot = a[low];
// int mid = (low+high)/2;
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 pivot = Partition(a,low,high);
QuickSort(a,low,pivot-1);
QuickSort(a,pivot+1,high);
}
}
int main()
{
int a[10];
for(int i=0; i<10; i++)
cin>>a[i];
QuickSort(a,0,9);
for(int i=0; i<10; i++)
cout<<a[i]<<" ";
return 0;
}
0.0分
0 人评分