利用冒泡排序法模拟qsort函数,列如对int数组排序,struct数组排序,字符串排序,
<span style="font-family: Arial, Helvetica, sans-serif;">在以下函数参数里为了方便接收实参,形参给出了void *这种形式,</span><span style="font-family: Arial, Helvetica, sans-serif;">至于函数的实现部分必须要强制转换为需要的类型。</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
int Compare_int(const void* num1, const void* num2){
return *(int *)num1 - *(int *)num2;
}
int Compare_struct(const void* stu1, const void* stu2){
return ((pstu)stu1)->age - ((pstu)stu2)->age;
}
int Compare_str(const void* str1, const void* str2){
return ( strcmp (*(char**)str1, *(char**)str2) );
}
</pre><pre name="code" class="objc">
以下函数是为了交换满足要求的相邻元素。但是每个元素的具体类型并不知道,但可以通过每个元素的所占的字节数从而交换满足要求的相邻元素
void Swap(char *buf1, char *buf2, int width){
<span style="white-space:pre"> </span>int i = 0;
<span style="white-space:pre"> </span>for (i = 0; i < width; i++){
<span style="white-space:pre"> </span>char tmp = buf1[i];
<span style="white-space:pre"> </span>buf1[i] = buf2[i];
<span style="white-space:pre"> </span>buf2[i] = tmp;
<span style="white-space:pre"> </span>}
}
//利用冒泡排序法模拟qsort函数函数指针com参数为元素的首地址,Swap函数将满足要求的数据进行交换,
void my_qsort(void*base,int size,int width,int(*com)(const void*, const void*)){
<span style="white-space:pre"> </span>int i = 0, j = 0;
<span style="white-space:pre"> </span>for (i = 0; i < size - 1; i++){
<span style="white-space:pre"> </span>for (j = 0; j < size - 1 - i; j++){
<span style="white-space:pre"> </span>if (com((char *)base + j*width, (char *)base + (j + 1)*width)>0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>Swap((char *)base + j*width, (char *)base + (j + 1)*width, width);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
//代码如下
<pre name="code" class="objc">#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Stu{
char name[10];
int age;
double score;
}stu,*pstu;
int Compare_int(const void* num1, const void* num2){
return *(int *)num1 - *(int *)num2;
}
int Compare_struct(const void* stu1, const void* stu2){
return ((pstu)stu1)->age - ((pstu)stu2)->age;
}
int Compare_str(const void* str1, const void* str2){
return ( strcmp (*(char**)str1, *(char**)str2) );
}
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 my_qsort(void*base,int size,int width,int(*com)(const void*, const void*)){
int i = 0, j = 0;
for (i = 0; i < size - 1; i++){
for (j = 0; j < size - 1 - i; j++){
if (com((char *)base + j*width, (char *)base + (j + 1)*width)>0)
{
Swap((char *)base + j*width, (char *)base + (j + 1)*width, width);
}
}
}
}
int main(){
int arr1[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
int int_sz = sizeof(arr1) / sizeof(arr1[0]);
my_qsort(arr1, int_sz, sizeof(int), Compare_int);
for (int i = 0; i < int_sz; i++)
printf("%-3d", arr1[i]);
printf("\n");
stu stu1[] = { { "zhangsan", 12, 56 }, { "lisi", 9, 78 }, { "wangmazi", 14, 67 } };
int struct_sz = sizeof(stu1) / sizeof(stu1[0]);
my_qsort(stu1, struct_sz, sizeof(stu), Compare_struct);
for (int i = 0; i < struct_sz; i++)
printf("%-10s,%2d,%15f\n", stu1[i].name, stu1[i].age, stu1[i].score);
char *str1[] = {"aaaa","ccccc","bbbbbb","ddddd"};
int str_sz = sizeof(str1) / sizeof(str1[0]);
my_qsort(str1, str_sz, sizeof(char *), Compare_str);
for (int i = 0; i < str_sz; i++)
printf("%s\n",str1[i]);
system("pause");
return 0;
}