1、sort()函数的基本语法和使用
sort(首指针,尾指针,排序方法)
普通用法可以没有排序方法,默认情况下是升序。- 使用sort函数时,必须引用
#include<iostream>, #include<algorithm>
两个头文件,和命名空间namespace std 或者std::sort();
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[10];
for (int i = 0; i < 10; ++i){
cin >> a[i];
}
sort(a, a + 10); //范围,尾地址,最后一个元素的下一个位置。因此为a+20
for (int i = 0; i < 10; i++) {
cout << a[i] <<" ";
}
cout << endl;
return 0;
}
- 如果实现从大到小,利用sort函数,该如何实现呢?这时候就是sort()函数的第三个参数实现排序方法,即可!代码直接走起。。。。
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b){
return a > b;
}
int main(){
int a[10];
for (int i = 0; i < 10; ++i){
cin >> a[i];
}
sort(a, a + 10, cmp); //范围,尾地址,最后一个元素的下一个位置。因此为a+20.排序方法就不需要传参数了
for (int i = 0; i < 10; i++) {
cout << a[i] <<" ";
}
cout << endl;
return 0;
}
- Sort函数的第三个参数可以用这样的语句告诉程序你所采用的排序方法
sort(首地址,尾地址,less<数据类型>());//从小到大排序
sort(首地址,尾地址,greater<数据类型>());//从大到小排序
代码有:
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[10];
for (int i = 0; i < 10; ++i){
cin >> a[i];
}
sort(a, a + 10, less<int>());
for (int i = 0; i < 10; i++) {
cout << a[i] <<" ";
}
cout << endl;
return 0;
}
、
2、qsort()函数的基本语法和使用
qsort()
函数其声明在stdlib.h
文件中,是根据二分法写的,其时间复杂度为n*log(n)。- 函数原型:
void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *));
各参数的含义:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针 qsort()
要求提供的函数是需要自己定义的一个比较函数,比较函数使得qsort()
通用性更好。有了比较函数qsort()
可以实现对数组、字符串、结构体等结构进行升序或降序排序。- qsort()中几种常见的cmp()函数:
(1)、对int型数组进行排序
#include<stdio.h>
#include<stdlib.h>
//1、对int类型数组排序
int int_cmp(const void * p1, const void * p2){
return (*(int *)p1 - *(int *)p2);
}
int main(){
int arr[] = { 1, 8, 9, 2, 3, 4, 5, 6, 7, 10 };
int i = 0;
qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(int), int_cmp);
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i){
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
(2)、对char型数组进行排序
#include<stdio.h>
#include<stdlib.h>
//2、对char类型数组排序
int cmp(const void *p1, const void * p2){
return (*(char*)p1 - *(char*)p2);
}
int main(){
char ch[] = { 'w', 'm', 'g', 't', 'b', 'i' ,'\0'};
int i = 0;
qsort(ch, sizeof(ch) / sizeof(ch[0]), sizeof(char), cmp);
for (i = 0; i < sizeof(ch) / sizeof(ch[0]); ++i){
printf("%c ", ch[i]);
}
printf("\n");
return 0;
}
(3)、对double类型数组排序
#include<stdio.h>
#include<stdlib.h>
int double_cmp(const void * p1, const void * p2){
return *(double *)p1 > *(double *)p2 ? 1 : -1;
//返回值的问题,显然double_cmp返回的是一个整型,所以避免double返回小数而被丢失。
}
int main(){
double arr[] = { 1.1, 8.6, 9.6, 2, 3.7, 4.77, 5, 6.66, 7.90, 1.0 };
int i = 0;
qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(double), double_cmp);
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i){
printf("%0.2f ", arr[i]);
}
printf("\n");
return 0;
}
(4)对结构体一级排序
#include<stdio.h>
#include<stdlib.h>
typedef struct S{
double data;
int other;
}S;
int S_cmp(const void * p1, const void * p2){
return (*(S*)p1).data >(*(S*)p2).data ? 1 : -1;
}
int main(){
S s[] = { 2.3, 5, 6, 7, 8, 9, 9,100 };
int i = 0;
qsort(s, sizeof(s) / sizeof(s[0]), sizeof(S), S_cmp);
for (i = 0; i < sizeof(s) / sizeof(s[0]); ++i){
printf("%0.2f ", s[i]);
}
printf("\n");
return 0;
}
(5)、对字符串进行排序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
int s_cmp(const void * p1, const void * p2){
return strcmp( *(string*)p1, *(string*)p2);
}
int main(){
string s[] = { "good", "well", "noce"};
int i = 0;
qsort(s, sizeof(s) / sizeof(s[0]), sizeof(string), s_cmp);
for (i = 0; i < sizeof(s) / sizeof(s[0]); ++i){
printf("%s ", s[i]);
}
printf("\n");
return 0;
}
3、qsort与sort的区别:
- sort是一个改进版的qsort,sort函数优于qsort的一些特点:对大数组采取9项取样,更完全的三路划分算法,更细致的对不同数组大小采用不同方法排序。
- cmp函数和qsort中cmp函数的不同Sort中的cmp函数参数可以直接是参与比较的引用类型,sort可以采用标准库自带的比较函数,而qsort没有。
int cmp(const int &a,const int &b){
return a>b;
}
- cmp函数比较时qsort用“-”,而sort用”>”.这也是一个重要的区别。
- sort函数是c++中标准模板库的的函数,在qsort()上已经进行了优化,根据情况的不同可以采用不同的算法,所以较快。在同样的元素较多和同样的比较条件下,sort()的执行速度都比qsort()要快。另外,sort()是类属函数,可以用于比较任何容器,任何元素,任何条件。使用时需调用
sort(begin(),end(),cmp)。
参考链接:http://www.cnblogs.com/BeyondAnyTime/archive/2012/05/22/2513776.html