- 调用函数库中的qsort
#include<stdio.h>
#include <string.h>
#include<stdlib.h>
struct Stu
{
char name[20];
int age;
};
int cmp_int(const void*e1, const void*e2)
{
return *(int*)e1 - *(int*)e2;
}
int cmp_stu_name(const void*e1, const void*e2)
{
return strcmp((*(struct Stu*)e1).name, (*(struct Stu*)e2).name);
}
int main()
{
int i = 0;
int arr[] = {1,3,5,7,9,2,4,6,8,0};
struct Stu arr2[] = { {"zhangsan",20}, {"lisi",45}, {"wangwu",12} };
qsort(arr,sizeof(arr)/sizeof(arr[0]),sizeof(arr[0]),cmp_int);
qsort(arr2, sizeof(arr2) / sizeof(arr2[0]), sizeof(arr2[0]), cmp_stu_name);
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
printf("%d ", arr[i]);
}
printf("\n------------------------------------\n");
for (i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++)
{
printf("%s ", arr2[i].name);
}
return 0;
}
运行结果:————————————————————————————————————
分析qsort函数:
void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );
base----Start of target array----目标数组起始地址
num----Array size in elements----元素个数
width-----Element size in bytes----元素的大小(单位:字节)
compare----Comparison function----比较函数
elem1----Pointer to the key for the search----指针指向要比较的元素的地址
elem2--Pointer to the array element to be compared with the key--同上_
————————————————————————————————————
- 模拟实现qsort
#define _CRT_SECURE_N0_WARNINGS 1
#include<stdio.h>
#include <string.h>
#include<stdlib.h>
struct Stu
{
char name[20];
int age;
};
//一个字节一个字节的交换
void Swap(char *buf1,char *buf2,int width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *(buf1 + i);
*(buf1 + i) = *(buf2 + i);
*(buf2 + i) = tmp;
}
}
void bubble_sort(void *base, int sz, int width, int(*cmp)(const void*e1, const void*e2))
{
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);
}
}
}
}
int cmp_int(const void*e1, const void*e2)
{
return *(int*)e1 - *(int*)e2;
}
int cmp_stu_name(const void*e1, const void*e2)
{
return strcmp((*(struct Stu*)e1).name, (*(struct Stu*)e2).name);
}
int cmp_stu_age(const void*e1, const void*e2)
{
return (((struct Stu*)e1)->age-((struct Stu*)e2)->age);
}
int main()
{
int i = 0;
int arr[] = {1,3,5,7,9,2,4,6,8,0};
struct Stu arr2[] = { {"zhangsan",20}, {"lisi",45}, {"wangwu",12} };
bubble_sort(arr,sizeof(arr)/sizeof(arr[0]),sizeof(arr[0]),cmp_int);
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
printf("%d ", arr[i]);
}
printf("\n------------------------------------\n");
bubble_sort(arr2, sizeof(arr2) / sizeof(arr2[0]), sizeof(arr2[0]), cmp_stu_name);
for (i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++)
{
printf("%s ", arr2[i].name);
}
printf("\n------------------------------------\n");
bubble_sort(arr2, sizeof(arr2) / sizeof(arr2[0]), sizeof(arr2[0]), cmp_stu_age);
for (i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++)
{
printf("%s ", arr2[i].name);
}
printf("\n------------------------------------\n");
return 0;
}
运行结果: