Header file: <stdlib.h>
function name: qsort
Functional style:
qsort (array to be sorted, the length of the array is sorted, the length type, sort);
Parameters explanation:
- Be sorted array: the array are arranged, such as an array of integersa[100]。
- Sort the array length: For example, there20One is sort numbers.
- Type Length: can be usedsizeof(a[0]) Representation.
- Sort by: There areAscendingSorting andDescendingSort two kinds, tofunctionTransfer mode.
Sort Code:
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;//这是从小到大排序,若是从大到小改成: return *(int *)b-*(int *)a;
}
Code Example:
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;//这是从小到大排序,若是从大到小改成: return *(int *)b-*(int *)a;
}
int main()
{
int a[100];
int n;
scanf("%d",&n);//n代表数组中有几个数字
int i;
for(i=1;i<=n;i++)
scanf("%d",&a[i-1]);
qsort(a,n,sizeof(a[0]),cmp);//(数组,需要排序的数字个数,单个数字所占内存大小,比较函数)
for(i=1;i<=n;i++)
printf("%d ",a[i-1]);
return 0;
}
Source Code: https://www.cnblogs.com/rjgcs/p/5645791.html