Article Directory
C language
Primitive
void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
head File
#include<stdlib.h>
Code example
Description
Wherein const void * a
denotes a constant pointer declared a, (int*)a
refers to the constant force a pointer into a pointer integer a, then add it to one of the preceding *
says element position pointer points to take up.
Integer
#include<iostream>
#include<stdlib.h>
using namespace std;
int a[] = { 4, 3, 2, 1 };
int compare(const void* a, const void* b);
int main()
{
int i;
qsort(a, 4, sizeof(int), compare);
for ( i = 0; i < 4; i++)
{
cout << a[i] << endl;
}
return 0;
}
int compare(const void* a, const void* b)//升序
{
return *(int*)a - *(int*)b;
}
Output: 1 2 3 4
Character type
#include<iostream>
#include<stdlib.h>
using namespace std;
char a[][4] = { {"bca"}, {"abc"}, {"cab"} };
int compare(const void* a, const void* b);
int main()
{
int i;
qsort(a, 3, sizeof(a[0]), compare);
for ( i = 0; i < 3; i++)
{
cout << a[i] << endl;
}
return 0;
}
int compare(const void* a, const void* b)//升序
{
return strcmp((char*)a, (char*)b);
//return *(int*)a - *(int*)b;
}
Output: abc bca cab
C++
Generally, when sorting an array, this is simple, because the third parameter "compare" function can be defaulted, and the default is ascending order.
Primitive
There are three parameters:
1. The starting address of the array;
2. The ending address of the array;
3. How to sort (large to small or small to large).
head File
#include<algorithm>
Code example
Comparison function default
#include<iostream>
#include<algorithm>
using namespace std;
int value[] = { 5, 4, 3, 2, 1 };
bool compare(int a, int b);
int main()
{
int i;
sort(value, value + 5);
for ( i = 0; i < 5; i++)
{
cout << value[i] << endl;
}
return 0;
}
Output: 1 2 3 4 5
Partial array sorting
You can also play like this:
#include<iostream>
#include<algorithm>
using namespace std;
int value[] = { 5, 4, 3, 2, 1 };
bool compare(int a, int b);
int main()
{
int i;
sort(value + 1, value + 5);
for ( i = 0; i < 5; i++)
{
cout << value[i] << endl;
}
return 0;
}
Output: 5 1 2 3 4
Custom comparison function
#include<iostream>
#include<algorithm>
using namespace std;
int value[] = { 5, 4, 3, 2, 1 };
bool compare(int a, int b);
int main()
{
int i;
sort(value, value + 5, compare);
for ( i = 0; i < 5; i++)
{
cout << value[i] << endl;
}
return 0;
}
bool compare(int a, int b) //降序
{
return a > b;
}
Output: 5 4 3 2 1 in
descending order
Library function comparison function (namespace std)
less<数据类型>()
greater<数据类型>()
Character type:
#include<iostream>
#include<algorithm>
using namespace std;
char value[] = { 'a', 'b', 'c', 'd', 'e' };
int main()
{
int i;
sort(value, value + 5, greater<char>());
for ( i = 0; i < 5; i++)
{
cout << value[i] << endl;
}
return 0;
}
Output: edcba
Sort structure-custom comparison function
#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
string name;
int age;
}value[3];
bool compare(const student& a, const student& b);
int main()
{
int i;
value[0] = { "Kite", 10 };
value[1] = { "Tom", 15 };
value[2] = { "Mike", 20 };
sort(value, value + 3, compare);
for ( i = 0; i < 3; i++)
{
cout << value[i].name << " " << value[i].age << endl;
}
return 0;
}
bool compare(const student& a, const student& b)
{
if (a.name == b.name)
{
return a.age < b.age;
}
else
{
return a.name < b.name;
}
}
Export:
Kite 10
Mike 20
Tom 15
Sort structure-overload relational operators
#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
string name;
int age;
bool operator < (const student& b) const //符号重载
{
if (name == b.name)
{
return age < b.age;
}
else
{
return name < b.name;
}
}
//简单地说,和之前的 compare 函数相比,就是把当前“对象”看作 a 了
}value[3];
int main()
{
int i;
value[0] = { "Kite", 10 };
value[1] = { "Tom", 15 };
value[2] = { "Mike", 20 };
sort(value, value + 3);
for ( i = 0; i < 3; i++)
{
cout << value[i].name << " " << value[i].age << endl;
}
return 0;
}
Export:
Kite 10
Mike 20
Tom 15