如何使用sort快速排序

试题 算法提高 快速排序

			提交此题   评测记录   
		
		资源限制时间限制:1.0s   内存限制:256.0MB问题描述

用递归来实现快速排序(quick sort)算法。快速排序算法的基本思路是:假设要对一个数组a进行排序,且a[0] = x。首先对数组中的元素进行调整,使x放在正确的位置上。同时,所有比x小的数都位于它的左边,所有比x大的数都位于它的右边。然后对于左、右两段区域,递归地调用快速排序算法来进行排序。

输入格式:输入只有一行,包括若干个整数(不超过10个),以0结尾。

输出格式:输出只有一行,即排序以后的结果(不包括末尾的0)。

输入输出样例样例输入
5 2 6 1 7 3 4 0
样例输出
1 2 3 4 5 6 7

#include<iostream>
//#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
    
    
   int str[100];
   int i=-1;
   while(cin>>str[++i]&&str[i]!=0);
   int cns=i;
   sort(str,str+cns,less<int>());
   for(int j=0;j<cns;j++)
   {
    
    
   cout<<str[j]<<' ';
   }
   return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_46232829/article/details/107668180