sort与qsort的区别与联系

sort属于C++范畴,在algorithm头文件中,下面直奔主题,给大家一个清晰明了的认识.qsort有C,和C++两个版本.

qsort的compare函数原型 //comp ,也就说,如果the first < the second 返回-1;如果the first > the second 返回1;如果the first == the second 返回0.,排序结果就表现为升序

comp - comparison function which returns ?a negative integer value if the first argument is less than the second,

a positive integer value if the first argument is greater than the second and zero if the arguments are equal.
The signature of the comparison function should be equivalent to the following:

int cmp(const void *a, const void *b);

The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.

 1 #include <algorithm>
 2 #include <functional>
 3 #include <array>
 4 #include <iostream>
 5 using namespace std;
 6 int cmpself(const void *a, const void *b){
 7     int arg1 = *static_cast<const int*>(a);
 8     int arg2 = *static_cast<const int*>(b);
 9
10     if(arg1 < arg2) return -1;
11     if(arg1 > arg2) return 1;
12     return 0;
13     //return arg1 < arg2;//如果将上面的改为这个呢?就意味着前面元素大于后面元素,也就是降序排列了
14 }
15 int main()
16 {
17     //C++ sort 排序规则:前面元素大于后面元素,就返回降序结果;否则,返回升序结果.
18     std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
19
20     // sort using the default operator< sort默认升序排列
21     std::sort(s.begin(), s.end());
22     for (auto a : s) {
23         std::cout << a << " ";
24     }
25     std::cout << ‘\n‘;
26
27     // sort using a standard library compare function object
28     //the former is greater than the later,降序排列
29     std::sort(s.begin(), s.end(), std::greater<int>());
30     for (auto a : s) {
31         std::cout << a << " ";
32     }
33     std::cout << ‘\n‘;
34
35     // sort using a custom function object
36     struct {
37         bool operator()(int a, int b)
38         {
39             return a < b;//升序排列
40         }
41     } customLess;
42     std::sort(s.begin(), s.end(), customLess);
43     for (auto a : s) {
44         std::cout << a << " ";
45     }
46     std::cout << ‘\n‘;
47
48     // sort using a lambda expression
49     std::sort(s.begin(), s.end(), [](int a, int b) {
50         return b < a;   //降序排列
51     });
52     for (auto a : s) {
53         std::cout << a << " ";
54     }
55     std::cout << ‘\n‘;
56
57     //c++ qsort
58     int arr[] = {-2, 99, 0, -743, 2, INT_MIN+1, 4};
59     constexpr std::size_t elesize = sizeof(arr[0]);
60     constexpr std::size_t size = sizeof(arr) / sizeof(arr[0]);
61
62     std::qsort(arr, size, sizeof(arr[0]), [](const void* a, const void* b)
63     {
64         int arg1 = *static_cast<const int*>(a);
65         int arg2 = *static_cast<const int*>(b);
66         //return arg1 < arg2; //error
67         if(arg1 < arg2) return -1;
68         if(arg1 > arg2) return 1;//需要注意的是,无论是C还是C++中的qsort,
69         return 0;
70
71         //  return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
72         //  return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
73     });
74
75     for(int ai : arr)
76         std::cout << ai << ‘ ‘;
77     std::cout << ‘\n‘;
78     std::qsort(arr, size, sizeof(arr[0]), cmpself);
79     for(int ai : arr)
80         std::cout << ai << ‘ ‘;
81     std::cout << ‘\n‘;
82 }
时间: 2024-08-27 06:02:34

sort与qsort的区别与联系的相关文章

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

在平时刷算法题和oj的时候,排序算法是最经常用到的算法之一:且在各类算法书的目录中 也通常是将各种排序算法放在最前面来讲,可见排序算法的重要性.可能许多人都在算法书中有学过冒泡.快速排序的方法,也都大致了解其原理:实际应用时,冒泡排序是最为简单的,当然复杂度也是最高的.....(就如高德纳所说:"冒泡排序除了它迷人的名字和导致了某些有趣的理论问题这一事实之外,似乎没有什么值得推荐的"):快排在理想情况下可以说是最高效的了(nlogn).但快速排序并不像冒泡排序那样好理解和记忆,每次都需

sort与sorted的区别

描述 我们需要对List进行排序,Python提供了两个方法对给定的List L进行排序 :         方法1.用对List的成员函数sort进行排序        方法2.用内置函数sorted进行排序(从2.4开始) -------------------------------------------------sorted-------------------------------------------------------- sorted() 函数对所有可迭代的对象进行排序

sort与sorted的区别及实例

描述 我们需要对List进行排序,Python提供了两个方法对给定的List L进行排序 : 方法1.用对List的成员函数sort进行排序方法2.用内置函数sorted进行排序(从2.4开始) sorted sorted() 函数对所有可迭代的对象进行排序操作 >>> help(sorted) Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=Fal

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

主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 功 能: 使用快速排序例程进行排序 参 数: 1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针,用于确定排序的顺序 说 明:qsort函数是ANSI C标准中提供的,其

sort与qsort的异同

主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 功 能: 使用快速排序例程进行排序 参 数: 1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针,用于确定排序的顺序 说 明:qsort函数是ANSI C标准中提供的,其

sort与qsort

1.qsort函数: 原 型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 功 能: 使用快速排序例程进行排序 参 数: 1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针,用于确定排序的顺序 说 明:qsort函数是ANSI C标准中提供的,其声明在stdlib.h文件中,是根据二分法写的,其时间复杂度为n*log(n). q

pair/sort/find/qsort

1. pair template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair() : first(T1()), second(T2()) {} pair(const T1 & a, const T2 & b) : first(a), second(b) {} template <class U1, cl

c++中sort()及qsort()的用法总结

函数名 功能描述  sort 对给定区间所有元素进行排序  stable_sort 对给定区间所有元素进行稳定排序  partial_sort 对给定区间所有元素部分排序  partial_sort_copy 对给定区间复制并排序  nth_element 找出给定区间的某个位置对应的元素  is_sorted 判断一个区间是否已经排好序  partition 使得符合某个条件的元素放在前面  stable_partition 相对稳定的使得符合某个条件的元素放在前面 要使用此函数只需用#inc

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

做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错.STL里面有个sort函数,可以直接对数组排序,复杂度为n*log2(n). 使用这个函数,需要包含头文件 #include <algorithm>. 这个函数可以传两个参数或三个参数.第一个参数是要排序的区间首地址,第二个参数是区间尾地址的下一地址.也就是说,排序的区间是[a,b).简单来说,有一个数组int a[100],要对从a[0]到a[