title: Asm date: 2022.06.21 toc: true comments: true tags: categories: description: keywords: top_img: cover:
qsort 排序
功能: 使用快速排序例程进行排序
头文件:stdlib.h
用法: void qsort(数值地址,数组长度 ,每个元素的字节大小,自定义的函数地址);
参数: 1 void* base 待排序数组,排序之后的结果仍放在这个数组中
2 size_t num 数组中待排序元素数量
3 size_t width 各元素的占用空间大小(单位为字节)
4 int(__cdecl* compare)(const void*,const void *)); 指向函数的指针,用于确定排序的顺序(需要用户自定义一个比较函数)
qsort用的是快速排序算法
对于自定义的函数,用于返回排序的升序还是降序,返回一个int值
对int进行排序
int num[100];
int cmp_int(const void* _a , const void* _b) //参数格式固定
{
int* a = (int*)_a; //强制类型转换
int* b = (int*)_b;
return *a - *b;
//降序是*b-*a
}
qsort(num,100,sizeof(num[0]),cmp_int);
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//或者
int cmp(const void *a, const void *b)
{
return *(int*)a - *(int*)b; //由小到大排序
//return *(int *)b - *(int *)a; 由大到小排序
}
例子
#include<stdio.h>
#include<stdlib.h>
#define L 20
int inc(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int main ()
{
int a[L] = {0, 5, 2, 3, 4, 9, 8, 7, 6, 1,
11, 15, 14, 12, 13, 16, 17, 18, 19, 10};
qsort(a, L, sizeof(int), inc);
for (int i = 0; i < L; i++)
printf("%d ", a[i]);
}
对double排序
double in[100];
int cmp_double(const void* _a , const void* _b) //参数格式固定
{
double* a = (double*)_a; //强制类型转换
double* b = (double*)_b;
return *a > *b ? 1 : -1; //特别注意
}
qsort(in,100,sizeof(in[0]),cmp_double);
//xxxxxxxxxxxxxxxxxxxxxxxxxx
//或者
int inc (const void * a, const void * b)
{
return *(double *)a > *(double *)b ? 1 : -1;
}
使我们要用三目运算符??
思考一下2.7-2.6=0.1
返回值是多少?
没错,返回去0
那么是升序还是降序呢?是相等......
我还是不太了解,,,,,,,
例子
#include<stdio.h>
#include<stdlib.h>
#define L 20
int inc(const void *a, const void *b)
{
return *(double *)a > *(double *)b? 1 : -1;
}
int main ()
{
double a[L] = {0.1, 0.11, 1.1, 1.5, 1.8, 1.51, 2.5, 2.9, 1.3, 0.8,
15.5, 7.9, 8.5, 8.51, 8.6, 3, 1.41, 1.11, 1.51, 2};
qsort(a, L, sizeof(double), inc);
for (int i = 0; i < L; i++)
printf("%.2lf\n", a[i]);
}
对char进行排序
char word[100];
int cmp_char(const void* _a , const void* _b) //参数格式固定
{
char* a = (char*)_a; //强制类型转换
char* b = (char*)_b;
return *a - *b;
}
qsort(word,100,sizeof(word[0]),cmp_char);
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
int inc(const void *a,const void *b)
{
return *(char *)a - *(char *)b;
}
根据首字母排序
int inc(const void *a, const void *b)
{
return * (char *)a - *(char * )b;
}
例子
#include<stdio.h>
#include<stdlib.h>
#define L 10
#define K 10
int inc(const void *a, const void *b)
{
return *(char *)a - *(char *)b;
}
int main ()
{
char a[L][K] = {
"rbsc",
"jcse",
"efgd",
"arbs",
"bbs",
"cbfe",
"dgafg" ,
"ewqrta",
"ofgd",
"mbcv",
};
qsort(a, L, sizeof(char) * K, inc);
for (int i = 0; i < L; i++)
printf("%s\n", a[i]);
}
根据字符串长度排序
int inc(const void *a, const void *b)
{
return strlen((char * )a) > strlen((char * )b) ? 1 : -1;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define L 10
#define K 10
int inc(const void *a, const void *b)
{
return strlen((char *)a) > strlen((char *)b) ? 1 : -1;
}
int main ()
{
char a[L][K] = {
"rbsc",
"jcsse",
"efgdsd",
"arbs",
"bbs",
"cbfefaa",
"dgafg" ,
"ewqrta",
"ofgd",
"mbcv312",
};
qsort(a, L, sizeof(char) * K, inc);
for (int i = 0; i < L; i++)
printf("%s\n", a[i]);
}
按字典排序字符串。
int inc(const void *a, const void *b)
{
return (strcmp((char *)a, (char *)b));
}
对字符串
char word[100][10];
int cmp_string(const void* _a , const void* _b) //参数格式固定
{
char* a = (char*)_a; //强制类型转换
char* b = (char*)_b;
return strcmp(a,b);
}
qsort(word,100,sizeof(word[0]),cmp_string);
例子
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define L 10
#define K 10
int inc(const void *a, const void *b)
{
return strcmp((char * )a, (char *)b);
}
int main ()
{
char a[L][K] = {
"rbsc",
"jcsse",
"afgdsd",
"arbs",
"abs",
"cbfefaa",
"cgafg" ,
"ewqrta",
"ofgd",
"mbcv312",
};
qsort(a, L, sizeof(char) * K, inc);
for (int i = 0; i < L; i++)
{
printf("%s\n", a[i]);
}
}
结构体
struct node
{
double one;
int two;
} s[100];
根据double
int inc( const void *a ,const void *b)
{
return ( * (node * )a).one > ( * (node * )b).one ? 1 : -1;
}
先根据double型的one的大小排序,若两值相等,在根据int型two的大小排序。
int inc( const void *a , const void *b )
{
if((* (node * )a).one != ( * (node * )b).one)
return ( * (node * )a).one > ( * (node * )b).one ? 1 : -1;
else
return (* (node * )a).two -( * (node * )b).two;
}
例子
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define L 10
typedef struct node {
double first;
int numb;
}node;
int inc(const void *a, const void *b)
{
if((* (node *)a).first != ( * (node *)b).first)
return ( * (node * )a).first > ( * (node * )b).first ? 1 : -1;
else return (* (node * )a).numb -( * (node * )b).numb;
}
int main ()
{
node arr[L] = {
1.0, 1,
2.0, 2,
1.1, 3,
2.1, 4,
3.5, 5,
1.0, 6,
1.1, 7,
5.1, 8,
5.0, 9,
3.6, 10,
};
qsort(arr, L, sizeof(node), inc);
for (int i = 0; i < L; i++)
{
printf("%.2lf %d\n", arr[i].first, arr[i].numb);
}
}