首页 > 代码库 > sort与qsort的区别与联系

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 }

 

sort与qsort的区别与联系