void qsort (基地址,元素个数,元素大小,比较函数);
其中比较函数是:int compare(当前元素1的地址,当前元素2地址);
举例:对int类型数组排序 如下(里面有一个const void*a的用法,这个东西是什么呢,就是定义了一个指针这个指针可以指向任意类型数据,而且指针指向的数据不能被修改,const则限制指向的数据是常数类型,不是,const作用是保证参数值不变)而且可以变动比较函数实现倒序排列
#include<iostream>
#include<stdlib.h>
using namespace std;
int cmp(const void* a, const void* b)
{
return (*((int*)a) - *((int*)b));
}
int main()
{
int arr[10] = { -1,3,45,2,-4,23,12,4,7,2 }; int n = 10;
qsort(arr, n, sizeof(arr[0]), cmp);
for (int i = 0; i < 10; i++)
{
cout << i << ":" << arr[i] << endl;
}
return 0;
}
在对double类型数组进行排序时比较函数就不能用两个数据相减来作为比较函数的返回值,因为小数经过int型就变成0了,应该比较大小来返回值
对于char类型数组排序的话:
#include<iostream>
#include<stdlib.h>
using namespace std;
int cmp(const void* a, const void* b)
{
return (*((char*)a) - *((char*)b));
}
int main()
{
//int arr[10] = { -1,3,45,2,-4,23,12,4,7,2 }; int n = 10;
char arr[] = "adfgyjylkui"; //int n = 10;
qsort(arr, 10, sizeof(arr[0]), cmp);
/*for (int i = 0; i < 10; i++)
{
cout << i << ":" << arr[i] << endl;
}*/
puts(arr);
return 0;
}
对二维char类型数组排序
#include<iostream>
#include<stdlib.h>
using namespace std;
int cmp(const void* a, const void* b)
{
char *l = *((char**)a);
char *r = *((char**)b);
return strcmp(l, r);
//return (*((char*)a) - *((char*)b));
}
int main()
{
//int arr[10] = { -1,3,45,2,-4,23,12,4,7,2 }; int n = 10;
//char arr[] = "adfgyjylkui"; //int n = 10;
const char *words[10] =
{
"welcome",
"to",
"the",
"new",
"world"
};
int n = 5;
qsort(words, n, sizeof(char*), cmp);
/*for (int i = 0; i < 10; i++)
{
cout << i << ":" << arr[i] << endl;
}*/
for(int i=0;i<5;i++)
puts(words[i]);
return 0;
}
对于结构体数组排序方法
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct
{
int a;
int b;
}structTest;
int cmp(const void* a, const void* b)
{
int l = ((structTest*)a)->a;
int r= ((structTest*)b)->a;
return l - r;
//char *l = *((char**)a);
//char *r = *((char**)b);
//return strcmp(l, r);
//return (*((char*)a) - *((char*)b));
}
int main()
{
//int arr[10] = { -1,3,45,2,-4,23,12,4,7,2 }; int n = 10;
//char arr[] = "adfgyjylkui"; //int n = 10;
/*const char* words[10] =
{
"welcome",
"to",
"the",
"new",
"world"
};*/
structTest structArray[5] =
{
{1,2},
{0,3},
{2,1},
{5,1},
{-1,0}
};
int n = 5;
qsort(structArray, n, sizeof(structArray[0]), cmp);
/*for (int i = 0; i < 10; i++)
{
cout << i << ":" << arr[i] << endl;
}*/
for (int i = 0; i < 5; i++)
printf("%d,%d\n", structArray[i].a, structArray[i].b);
//puts(words[i]);
return 0;
}
对结构体数组的二次排序,即第一个量比较完后相等则比较第二个
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct
{
int a;
int b;
}structTest;
int cmp(const void* a, const void* b)
{
int l = ((structTest*)a)->a;
int r= ((structTest*)b)->a;
if (l > r)return -1;
else if (l == r)
{
int m = ((structTest*)a)->b;
int n = ((structTest*)b)->b;
return m - n;
}
else return 1;
//return l - r;
//char *l = *((char**)a);
//char *r = *((char**)b);
//return strcmp(l, r);
//return (*((char*)a) - *((char*)b));
}
int main()
{
//int arr[10] = { -1,3,45,2,-4,23,12,4,7,2 }; int n = 10;
//char arr[] = "adfgyjylkui"; //int n = 10;
/*const char* words[10] =
{
"welcome",
"to",
"the",
"new",
"world"
};*/
structTest structArray[5] =
{
{1,2},
{0,3},
{2,1},
{5,1},
{-1,0}
};
int n = 5;
qsort(structArray, n, sizeof(structArray[0]), cmp);
/*for (int i = 0; i < 10; i++)
{
cout << i << ":" << arr[i] << endl;
}*/
for (int i = 0; i < 5; i++)
printf("%d,%d\n", structArray[i].a, structArray[i].b);
//puts(words[i]);
return 0;
}
2.上面是c语言版本的,下面是c++语言的函数,即void sort(基地址,基地址+n);
用法相对于c语言的函数简单,还有就是该函数可以扩展参数,及加入比较函数
#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;
typedef struct
{
int a;
int b;
}structTest;
int cmp(const structTest &st1, const structTest& st2)//这里为什么用引用符呢,本来这里表示的指针已经是不能改变参数值了,用引用符因为一个原因
{
return st1.a < st2.a;
/*int l = ((structTest*)a)->a;
int r= ((structTest*)b)->a;
if (l > r)return -1;
else if (l == r)
{
int m = ((structTest*)a)->b;
int n = ((structTest*)b)->b;
return m - n;
}
else return 1;*/
//return l - r;
//char *l = *((char**)a);
//char *r = *((char**)b);
//return strcmp(l, r);
//return (*((char*)a) - *((char*)b));
}
int main()
{
//int arr[10] = { -1,3,45,2,-4,23,12,4,7,2 }; int n = 10;
//char arr[] = "adfgyjylkui"; //int n = 10;
/*const char* words[10] =
{
"welcome",
"to",
"the",
"new",
"world"
};*/
structTest array[5] =
{
{1,2},
{0,3},
{2,1},
{5,1},
{-1,0}
};
sort(array, array + 5, cmp);
for (int i = 0; i < 5; i++)
cout << array[i].a << "," << array[i].b << endl;
/*int n = 5;
qsort(structArray, n, sizeof(structArray[0]), cmp);
for (int i = 0; i < 10; i++)
{
cout << i << ":" << arr[i] << endl;
}
for (int i = 0; i < 5; i++)
printf("%d,%d\n", structArray[i].a, structArray[i].b);
//puts(words[i]);*/
return 0;
}
关于这两个函数的应用呢,有一个很好的例子,具体视频在b站