前言:看懂思路后,亲自实现一遍,体会更深
回顾: 冒泡排序的运用
话不多说直接看代码
引入qsort函数排序:
语法:1.头文件 #include<stdlib.h>
2. qsort(数组名,数组元素个数,每个元素所占字节,函数)
eg: qsort(arrl , sz , sizeof (arrl [0] ),sort_int);
运用:
整型
#include<stdio.h>
#include<stdlib.h>
void print_arrl(int arrl[], int sz) {
for (int i = 0; i < sz; i++) {
printf("%d ", arrl[i]);
}
}
int sort_int(const void* e1, const void* e2) {
return *(int*)e1 - *(int*)e2;
}
int main() {
int arrl[] = { 3,1,2,4,6,5,8,7,9,0 };
int sz = sizeof(arrl) / sizeof(arrl[0]);
qsort(arrl, sz, sizeof(arrl[0]), sort_int);
print_arrl(arrl, sz);
return 0;
}
结构体
#include<stdio.h>
#include<stdlib.h>
void print_arrl(int arrl[], int sz) {
for (int i = 0; i < sz; i++) {
printf("%d ", arrl[i]);
}
}
//test_int
//int sort_int(const void* e1, const void* e2) {
// return *(int*)e1 - *(int*)e2;
//}
int sort_ch_age(const void* e1, const void* e2) {
/* e1 为void* 无符号类型 需要先强转为 自定义类型的指针后再解引用*/
return ((struct stu*)e1)->age - ((struct stu*)e2)->age;
}
int sort_ch_name(const void* e1, const void* e2) {
return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
}
struct stu {
char name[10];
int age;
};
int main() {
/*int arrl[] = { 3,1,2,4,6,5,8,7,9,0 };*/
struct stu ch[3] = { {"lisi",30},{"zhangsan",27},{"wangwu",15}};
int sz = sizeof(ch) / sizeof(ch[0]);
/*qsort(arrl, sz, sizeof(arrl[0]), sort_int);*/
print_arrl(ch, sz,sizeof(ch[0]),sort_ch_age);
print_arrl(ch, sz, sizeof(ch[0]), sort_ch_name);
return 0;
}
模拟qsort函数实现 bubbl_sort自定义函数:
闲话少谈,先上代码:
//交换
void Swap(char* staging1, char* staging2, size_t bubbl_width) {
for (int i = 0; i < bubbl_width; i++) {
char tmp = *staging1;
*staging1 = *staging2;
*staging2 = tmp;
staging1++;
staging2++;
}
}
void bubbl_sort(void* base, size_t bubbl_sz, size_t bubbl_width, int (*mapl)(const void* e1, const void* e2)) {
//趟数
for (int i = 0; i < bubbl_sz - 1; i++) {
//每一趟的两两比较的下标
for (int j = 0; j < bubbl_sz - 1 - i; j++) {
//假设待排序的元素是升序
if (mapl((char*)base + j * bubbl_width , (char*)base + (j + 1) * bubbl_width)>0) {
//交换
/*Swap((*(char*)base + j * bubbl_width), (*(char*)base + (j + 1) * bubbl_width),bubbl_width);*/
Swap((char*)base + j * bubbl_width, (char*)base + (j + 1) * bubbl_width, bubbl_width);
}
}
}
}
void print_bubbl_arrl(int arrl[], int sz) {
for (int i = 0; i < sz; i++) {
printf("%d ", arrl[i]);
}
}
//结构体test
struct stu {
char name[20];
int age;
};
//传过来要实现的参数
//test.int_arrl
int map_sort(const void* e1, const void* e2) {
return *(int*)e1 - *(int*)e2;
}
//test.age
int map_sort_ch_age(const void* e1, const void* e2) {
return ((struct stu*)e1)->age - ((struct stu*)e2)->age;
}
//test.name
int map_sort_ch_name(const void* e1, const void* e2) {
return strcmp((*(struct stu*)e1).name, (*(struct stu*)e2).name);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
int arrl[] = {3,1,2,4,6,5,8,7,9,0};
int sz = sizeof(arrl) / sizeof(arrl[0]);
bubbl_sort(arrl, sz, sizeof(arrl[0]), map_sort);
struct stu ch[3] = { {"lisi",20},{"zhangsan",18},{"wangwu",35}};
int sz = sizeof(ch) / sizeof(ch[0]);
bubbl_sort(ch, sz, sizeof(ch[0]), map_sort_ch_age);
bubbl_sort(ch, sz, sizeof(ch[0]), map_sort_ch_name);
print_bubbl_arrl(ch, sz);
return 0;
}
代码拆解:代码可分为三个角度
1.自定义bubble_sort函数使用者角度:
代码如下:
这部分代码如果看懂了qsort函数的运用部分的话,直接把bubbl_sort函数看成是qsort函数进行了。
2.实现bubble_sort函数功能者角度:
代码如下:
下面对黄色代码的解析:
3.全局角度:
在test的时候,不注意发生的一些问题(共勉):
1.出现了访问权限冲突 ,原因:在写Swap函数时,不留意把base解引用了,导致在Swap函数内hcar*staging接收了一个常量,再对staging解引用的时候,造成了指针指向其他的内存区域存在越界访问,所以照成了访问权限冲突.(如果是其他原因,请指出,非常感谢!!!)
2.另外还有一个不小心犯的点,把定义的结构体放在了对结构体进行操作部分代码的后面,导致程序运行不了.