void qsort (void* base,// base 指向了要排序数组的第一个元素
size_t num, //base指向的数组中的元素个数(待排序的数组元素的个数)
size_t size,//base指向的数组中的元素的大小(单位字节)
int (*compar)(const void*,const void*)); //函数指针 - 指针指向的函数的函数
//是用来比较数组中的两个元素的
#include
#include
void print_arr(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
//cmp_int 这个函数是用来比较p1和p2指向元素的大小
int cmp_int(const void* p1,const void* p2)
{
return *(int*)p1 - *(int*)p2;
}
void test1()
{
//想排序一组整形数据
int arr[] = { 3,1,5,7,9,2,4,6,8,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
print_arr(arr, sz);
qsort(arr, sz,sizeof(arr[0]),cmp_int);
print_arr(arr, sz);
}
int main()
{
test1();
return 0;
}
#include
#include
//测试qsort排序结构体数据
struct Stu
{
char name[20];
int age;
};
//怎么比较2个结构体数据?-不能直接使用> < == 比较
//1. 按照名字比较
//2. 可以按照年龄比较
//按照年龄比较
int cmp_stu_by_age(const void* p1, const void* p2)
{
return ((struct Stu*)p1)->age - ((struct Stu*)p2)->age;
}
void test2()
{
struct Stu arr[] = { {"zhangsan",20},{"lisi",38},{"wangwu",18} };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), cmp_stu_by_age);
}
int main()
{
test2();
return 0;
}
#include
#include
#include
//按照名字比较
//两个字符串不能使用> == <
//而是使用库函数strcmp
int cmp_stu_by_name(const void* p1, const void* p2)
{
return strcmp(((struct Stu*)p1)->name, ((struct Stu*)p2)->name);
}
void test3()
{
struct Stu arr[] = { {"zhangsan",20},{"lisi",38},{"wangwu",18} };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), cmp_stu_by_name);
}
int main()
{
test3();
return 0;
}
使用回调函数,模拟实现qsort(采用冒泡的方式)。
Swap(char* e1, char* e2, size_t width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *e1;
*e1 = *e2;
*e2 = tmp;
e1++;
e2++;
}
}
void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* p1, const void* p2))
{
int i = 0;
for (i = 0; i < sz - 1; i++)
{
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (cmp((char*)base + j * width, (char*)base + (j + 1) * width)> 0)
{
Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
}
}
}
}
#include
#include
Swap(char* e1, char* e2, size_t width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *e1;
*e1 = *e2;
*e2 = tmp;
e1++;
e2++;
}
}
void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* p1, const void* p2))
{
int i = 0;
for (i = 0; i < sz - 1; i++)
{
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (cmp((char*)base + j * width, (char*)base + (j + 1) * width)> 0)
{
Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
}
}
}
}
void print_arr(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int cmp_int(const void* p1, const void* p2)
{
return *(int*)p1 - *(int*)p2;
}
void test1()
{
int arr[] = { 3,1,5,7,9,2,4,0,8,6 };
int sz = sizeof(arr) / sizeof(arr[0]);
print_arr(arr, sz);
bubble_sort(arr, sz, sizeof(arr[0]), cmp_int);
print_arr(arr, sz);
}
int main()
{
test1();
return 0;
}
#include
#include
#include
Swap(char* e1, char* e2, size_t width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *e1;
*e1 = *e2;
*e2 = tmp;
e1++;
e2++;
}
}
void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* p1, const void* p2))
{
int i = 0;
for (i = 0; i < sz - 1; i++)
{
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (cmp((char*)base + j * width, (char*)base + (j + 1) * width)> 0)
{
Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
}
}
}
}
struct Stu
{
char name[20];
int age;
};
int cmp_stu_by_age(const void* p1, const void* p2)
{
return ((struct Stu*)p1)->name - ((struct Stu*)p2)->name;
}
void test3()
{
struct Stu st[] = { {"zhangsan",18},{"lisi",35},{"wangwu",15} };
int sz = sizeof(st) / sizeof(st[0]);
bubble_sort(st, sz, sizeof(st[0]), cmp_stu_by_age);
//打印数组内容
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%s %d\n", st[i].name, st[i].age);
}
}
int main()
{
test3();
return 0;
}
#include
#include
#include
Swap(char* e1, char* e2, size_t width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *e1;
*e1 = *e2;
*e2 = tmp;
e1++;
e2++;
}
}
void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* p1, const void* p2))
{
int i = 0;
for (i = 0; i < sz - 1; i++)
{
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (cmp((char*)base + j * width, (char*)base + (j + 1) * width)> 0)
{
Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
}
}
}
}
struct Stu
{
char name[20];
int age;
};
int cmp_stu_by_name(const void* p1, const void* p2)
{
return strcmp(((struct Stu*)p1)->name, ((struct Stu*)p2)->name);
}
void test2()
{
struct Stu st[] = { {"zhangsan",18},{"lisi",35},{"wangwu",15} };
int sz = sizeof(st) / sizeof(st[0]);
bubble_sort(st, sz, sizeof(st[0]), cmp_stu_by_name);
//打印数组内容
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%s %d\n", st[i].name, st[i].age);
}
}
int main()
{
test2();
return 0;
}