很长一段时间搞不明白 sort 和 qsort 的区别,平时在写程序时习惯了使用 sort ,因为它用起来比 qsort 要简单的多 , 这里详细介绍一下 sort 与 qsort :

  给出一个数组 a[10] = {3 , 2 , 4 , 6 , 7 , 5} ;

  若要实现从小到大输出;

  sort 实现如下:

#include<iostream>
#include<algorithm>
using namespace std ;
int a[10] = {3 , 2 , 4 , 6 , 7 , 5} ;
int main() {
sort(a,a+6) ;
for(int i = 0 ; i < 6 ; i++)
cout << a[i] << " " ;
cout << endl ;
return 0 ;
}

从大到小代码如下:

#include<iostream>
#include<algorithm>
using namespace std ;
int a[10] = {3 , 2 , 4 , 6 , 7 , 5} ; int cmp(int a , int b ) {
return a > b ;
} int main() {
sort(a,a+6,cmp) ;
for(int i = 0 ; i < 6 ; i++)
cout << a[i] << " " ;
cout << endl ;
return 0 ;
}

qsort 实现如下:

  从大到小 :

#include<iostream>
#include<algorithm>
using namespace std ;
int a[10] = {3 , 2 , 4 , 6 , 7 , 5} ; int cmp(const void *a , const void *b ) {
int *_a = (int *)a ; // a 强制转化为指针
int *_b = (int *)b ; // b 强制转化为指针
return *_a > *_b ; // 和 sort完全相反
} int main() {
qsort(a,6,sizeof(int),cmp) ;
for(int i = 0 ; i < 6 ; i++)
cout << a[i] << " " ;
cout << endl ;
return 0 ;
}

sort 与 qsort的更多相关文章

  1. stl sort和qsort的使用

    好不容易使用了下stl的qsort函数,顺便和sort函数一起总结下: 很多时候我们都需要用到排序. 例如: 1 #include <iostream> #include <algo ...

  2. C++中sort()及qsort() (不完整介绍)

    在平时刷算法题和oj的时候,排序算法是最经常用到的算法之一:且在各类算法书的目录中 也通常是将各种排序算法放在最前面来讲,可见排序算法的重要性.可能许多人都在算法书中有学过冒泡.快速排序的方法,也都大 ...

  3. 算法学习笔记——sort 和 qsort 提供的快速排序

    这里存放的是笔者在学习算法和数据结构时相关的学习笔记,记录了笔者通过网络和书籍资料中学习到的知识点和技巧,在供自己学习和反思的同时为有需要的人提供一定的思路和帮助. 从排序开始 基本的排序算法包括冒泡 ...

  4. (C++)STL排序函数sort和qsort的用法与区别

    主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int widt ...

  5. CF:322D - Ciel and Duel 贪心 或者 DP 我用的贪心 。。难道sort跟qsort是不一样的么?

    D. Ciel and Duel time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. pair/sort/find/qsort

    1. pair template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_t ...

  7. c++中sort()及qsort()的使用方法总结

    当并算法具体解释请见点我 想起来自己天天排序排序,冒泡啊,二分查找啊,结果在STL中就自带了排序函数sort,qsort,总算把自己解脱了~ 所以自己总结了一下,首先看sort函数见下表:   函数名 ...

  8. sort和qsort排序

    qsort(数组名,数组长度,数组中每个元素大小,compare); compare函数的写法决定了排序是升序还是降序.需要#include<stdlib.h> 例如: int compa ...

  9. sort与qsort的用法,建议使用sort

    做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错.STL里面有个sort函数,可以直接对数组排序,复 ...

随机推荐

  1. hdu 1210_(逻辑训练)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1210 #include<stdio.h> int main() { int n,t,sum ...

  2. Data visualization 课程 笔记3

    Learn how humans work to create a more effective computer interface 三种reasoning的方式  Deductive Reason ...

  3. golang之interface(接口)与 reflect 机制

    一.概述 什么是interface,简单的说,interface是一组method的组合,通过interface来定义对象的一组行为: interface类型定义了一组方法,如果某个对象实现了某个接口 ...

  4. MailBee的简单使用

    保存为Eml文件方法:MailMessage.SaveMessage() 读取文件方法(不知道是不是我用的问题,没找到直接读取Eml文件的方法): MsgConvert conv = new MsgC ...

  5. C#文件压缩

    /// <summary> /// 文件压缩 /// </summary> /// <param name="filesUrl">多个文件路径& ...

  6. 【转】引入android项目在eclipse ADT中显示中文乱码问题

    (1)修改工作空间的编码方式:Window->Preferences->General->Workspace->Text file Encoding在Others里选择需要的编 ...

  7. jquery新增,删除 ,修改,清空select中的option

    jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...

  8. 善于 调用Windows API

    前一段时间看见别人做的一个自动填写信息并且点击登录的程序,觉得很有意思. 其实就是在程序中调用Windows的API,那么如何调用,下面就做个简单的介绍. 写的简单粗暴, 不喜轻喷. 0.首先引入名称 ...

  9. events模块

    /** * Created by Administrator on 2016/8/3. */ var http = require("http"); //Node 导入文件系统模块 ...

  10. 代码风格——Cocos2d-x学习历程(四)

    1.Cocos2d-x拥有一个包含其他所有头文件的文件"cocos2d.h".通常,我们只需要在使用时包含这个头文件,就可以使用引擎的全部功能了. 2.Cocos2d-x的类都放置 ...