#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stu
{
char name[20];
int age;
};
// 冒泡排序 -> 不能对任意类型进行排序
void bubble_sort(int *p,int s)
{
// 趟数
for (int i = 0; i < s - 1; i++)
{
// 每趟
for (int j = 0; j < s - i - 1; j++)
{
// 若需对其他类型进行排序,仅比较和交换部分需要修改
if (*(p + j) > *(p + j + 1))
{
// 交换
int temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
}
void print_arr(int* p, int s)
{
for (int i = 0; i < s; i++)
{
printf("%d ", *(p+i));
}
printf("\n");
}
// void qsort(void* base, // base中存放的是待排序数据中第一个对象的地址
// size_t num, // 排序数据元素的个数
// size_t size, // 排序数据中一个元素的大小,单位是字节
// int (*cmp)(const void* el, const void* e2) ); // 是用来比较待排序数据中的2个相邻元素的函数,由用户自定义
int cmp_int(const void* e1, const void* e2)
{
return *((int*)e1) - *((int*)e2);
}
int cmp_char(const void* e1, const void* e2)
{
return strcmp((char*)e1, (char*)e2); // <string.h> 比较字符串,使用库函数 strcmp,本质比较的是ASCII码的大小
}
int sort_by_name(const void* e1, const void* e2)
{
return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name); // <string.h>
}
int sort_by_age(const void* e1, const void* e2)
{
return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
void test_int_sort()
{
int arr[10] = { 2,4,5,7,6,8,9,1,10,3 };
int length = sizeof(arr) / sizeof(arr[0]);
//bubble_sort(arr, length);
//print_arr(arr, length);
// 用库函数 qsort (需引用头文件<stdlib.h>)
qsort(arr, length, sizeof(arr[0]), cmp_int);
print_arr(arr, length);
}
void test_char_sort()
{
char ch[] = { 'f','a','m','i' };
int length = sizeof(ch) / sizeof(ch[0]);
qsort(ch, length, sizeof(ch[0]), cmp_char);
//print_arr(ch, length); //不能这么写
}
void test_struct_sort()
{
struct Stu stu[3] = { {"zhangsan",35},{"lisi",40},{"wangwu",30}};
int length = sizeof(stu) / sizeof(stu[0]);
qsort(stu, length, sizeof(stu[0]), sort_by_name);
qsort(stu, length, sizeof(stu[0]), sort_by_age);
}
// 模拟实现 qsort 函数
// 交换函数:逐字节进行交换!
void Swap(char* buf1, char* buf2, int width)
{
for (int i = 0; i < width; i++)
{
char temp = *buf1;
*buf1 = *buf2;
*buf2 = temp;
buf1++;
buf2++;
}
}
void bubble_qsort(void* base, int length, int width, int (*cmp)(const void* e1, const void* e2))
{
// 趟数
for (int i = 0; i < length - 1; i++)
{
// 每趟
for (int j = 0; j < length - i - 1; 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 test_int_bubble_qsort()
{
int arr[10] = { 2,4,5,7,6,8,9,1,10,3 };
int length = sizeof(arr) / sizeof(arr[0]);
bubble_qsort(arr, length, sizeof(arr[0]), cmp_int);
print_arr(arr, length);
}
void test_char_bubble_qsort()
{
char ch[] = { 'f','a','m','i' };
int length = sizeof(ch) / sizeof(ch[0]);
bubble_qsort(ch, length, sizeof(ch[0]), cmp_char);
}
void test_struct_bubble_qsort()
{
struct Stu stu[3] = { {"zhangsan",35},{"lisi",40},{"wangwu",30} };
int length = sizeof(stu) / sizeof(stu[0]);
bubble_qsort(stu, length, sizeof(stu[0]), sort_by_name);
bubble_qsort(stu, length, sizeof(stu[0]), sort_by_age);
}
int main()
{
// 测试库函数 qsort
//test_int_sort();
//test_char_sort();
//test_struct_sort();
// 测试模拟库函数 qsort 实现的 bubble_qsort
//test_int_bubble_qsort();
//test_char_bubble_qsort();
test_struct_bubble_qsort();
return 0;
}
C-库函数 qsort 的使用及模拟实现
于 2022-04-06 17:20:05 首次发布