1.使用qsort函数进行排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>//void qsort(void* base, //指向待排序数组的第一个元素的指针
// size_t num, //base指向数组中的元素个数
// size_t size,//base指向的数组中一个元素的大小,单位是字节
// int (*cmp)(const void*, const void*) //函数指针 - 传递函数的地址int compare_int(const void* p1, const void* p2)
{return *(int*)p2 - *(int*)p1;//降序排列//return *(int*)p1 - *(int*)p2;//升序排列
}
void print(int arr[], int num)
{for (int i = 0; i < num; i++){printf("%d", arr[i]);}
}
void test1()
{int arr[] = { 9, 7 ,8 ,6, 4 ,3 ,5 ,2 ,1 ,0 };int num = sizeof(arr) / sizeof(arr[0]);int size = sizeof(arr[0]);qsort(arr, num, size, compare_int);print(arr, num);
}struct stu
{char name[20];int age;
};
int compare_name(const void* p1, const void* p2)
{return strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name);//升序排列//return strcmp(((struct stu*)p2)->name, ((struct stu*)p1)->name);//降序排列}
int compare_age(const void* p1, const void* p2)
{return ((struct stu*)p1)->age - ((struct stu*)p2)->age;//升序排列//return ((struct stu*)p2)->age - ((struct stu*)p1)->age;//降序排列}
void test2()
{struct stu arr[] = { {"zhangsan",20} , {"lisi",18} , {"wangwu",25} };int num = sizeof(arr) / sizeof(arr[0]);int size = sizeof(arr[0]);qsort(arr, num, size, compare_name);for (int i = 0; i < num; i++){printf("%s ", arr[i].name);}
}
void test3()
{struct stu arr[] = { {"zhangsan",20} , {"lisi",18} , {"wangwu",25} };int num = sizeof(arr) / sizeof(arr[0]);int size = sizeof(arr[0]);qsort(arr, num, size, compare_age);for (int i = 0; i < num; i++){printf("%d ", arr[i].age);}
}int main()
{//test1();//test2();test3();return 0;
}
2.模仿qsort的功能实现一个通用的冒泡排序
(1).普通冒泡排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count = 0;
void bubble(int *arr[], int sz)
{int i = 0;int flag = 1;// 假设已经排好序了for (i = 0; i < sz - 1; i++){int j = 0;for (j = 0; j < sz - 1-i; j++){if (arr[j] > arr[j + 1]){flag = 0;int temp = arr[j];arr[j] = arr[j + 1];arr[j+1] = temp;}count++;}if (flag == 1){break;}}return;
}
int main()
{int arr[10] = {0};int* p = &arr[0];int sz = sizeof(arr) / sizeof(arr[0]);for (int i = 0; i < sz; i++){scanf("%d", &arr[i]);}bubble(arr, sz);for (int i = 0; i < sz; i++){printf("%d ", *(p + i));}printf("\ncount=%d", count);return 0;
}
这个冒泡排序只能对整形数组里的数值进行排序,而对字符数组,结构体里的值无法排序。
(2).通用冒泡排序
void swap(char *a, char* b,size_t size)
{for (int i = 0; i < size; i++){char temp = *a;*a = *b;*b = temp;a++;b++;}
}
int compare_int(const void* p1, const void* p2)
{return *(int*)p1 - *(int*)p2;
}struct stu
{char name[20];int age;
};
int compare_name(const void* p1, const void* p2)
{return strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name);//升序排列//return strcmp(((struct stu*)p2)->name, ((struct stu*)p1)->name);//降序排列}
int compare_age(const void* p1, const void* p2)
{return ((struct stu*)p1)->age - ((struct stu*)p2)->age;//升序排列//return ((struct stu*)p2)->age - ((struct stu*)p1)->age;//降序排列}void bubble_sort(void* base, size_t num, size_t size, int(*compare)(const void* ,const void* ))
{for (int i = 0; i < num - 1; i++){for (int j = 0; j < num - 1 - i; j++){if (compare((char*)base + j * size, (char*)base + (j + 1) * size) > 0){swap((char*)base + j * size, (char*)base + (j + 1) * size, size);}}}
}
void print(int arr[], int num)
{for (int i = 0; i < num; i++){printf("%d ", arr[i]);}
}void test1()
{int arr[] = { 9, 7 ,8 ,6, 4 ,3 ,5 ,2 ,1 ,0 };int num = sizeof(arr) / sizeof(arr[0]);int size = sizeof(arr[0]);bubble_sort(arr, num, size, compare_int);print(arr, num);
}void test2()
{struct stu arr[] = { {"zhangsan",20} , {"lisi",18} , {"wangwu",25} };int num = sizeof(arr) / sizeof(arr[0]);int size = sizeof(arr[0]);qsort(arr, num, size, compare_name);for (int i = 0; i < num; i++){printf("%s ", arr[i].name);}
}void test3()
{struct stu arr[] = { {"zhangsan",20} , {"lisi",18} , {"wangwu",25} };int num = sizeof(arr) / sizeof(arr[0]);int size = sizeof(arr[0]);qsort(arr, num, size, compare_age);for (int i = 0; i < num; i++){printf("%d ", arr[i].age);}
}int main()
{//test1();//test2();test3();return 0;
}