c/c++中sort用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZLK961543260/article/details/78171102

      c++中sort函数:

1、包含头文件:
#include<iostream>
#include<algorithm>
using namespace std;


2、返回值类型:void,参数类型:sort(iterator iter1, iterator iter2)


3、数据结构:这个我查到的好像是快速排序的非递归实现方法。总之就是快排,这时就要注意了,如果排序的序列基本有序就不要用这个排序算法,原因:快排在元素基本有序的情况下就变成了冒泡排序了,效率慢。要是基本无序可以用这个,效果还是不错的。


4、时间复杂度:好的情况(序列基本无序的情况)o(n logn)、坏的情况(基本有序的情况,冒泡排序)o(n*n)


用法如下:

#include "stdafx.h"
#include <vector>
#include<iostream>
#include<algorithm>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int a[10] = {9,6,3,2,5,8,7,4,1,0};
    vector<int> nums(a,a+10);
	sort(nums.begin(),nums.end());
	for(int i = 0; i < nums.size(); i++)
		cout<<nums[i]<<" ";
	cout<<endl;
	vector<int>::iterator it = nums.begin();
	while(it != nums.end())
	{
		cout<<*it<<" ";
		it++;
	}
	system("pause");
    return 0;

}
代码测试在vs2012。效果图如下:



猜你喜欢

转载自blog.csdn.net/ZLK961543260/article/details/78171102
今日推荐