//使用qsort排序
#include <stdio.h>
#include <stdlib.h>//qsort的头文件
#include <string.h>
void print(int arr[], int sz)//打印
{
int i = 0;
for (i = 0; i < sz; i++)
printf("%d ", arr[i]);
}
int q_arr(const void* e1, const void* e2)
{
return (*(int*)e1 - *(int*)e2);//升序//这里要强制转换,不然void是没法访问的
//return (*(int*)e2 - * (int*)e1);//降序
}
void test1()
{
int arr[] = { 9,8,7,11,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), q_arr);
print(arr, sz);
}
//结构体
struct Stu//先定义后使用函数放在下面
{
char name[20];
int age;
double score;//成绩
};
void print2(struct Stu* arr, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%s %d %.1lf ", arr->name, arr->age, arr->score);
arr++;
}
}
int q_arr1_age(const void* e1, const void* e2)
{
return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
int q_arr1_name(const void* e1, const void* e2)
{
return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}
void test2()
{
struct Stu arr[3] = { {"zhangsan", 20, 55.5}, {"lisi", 70, 30.6}, {"wangwu", 50, 60.8} };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), q_arr1_age);
//qsort(arr, sz, sizeof(arr[0]), q_arr1_name);
print2(&arr, sz);
}
int main()
{
test1();整形排序
test2();//结构体排序
return 0;
}
冒泡
//使用冒泡排序通用排序
#include <stdio.h>
#include <string.h>
void print(int arr[], int sz)//5
{
int i = 0;
for (i = 0; i < sz; i++)
printf("%d ", arr[i]);
}
void swap(char* buf1, char* buf2, int width)//4
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
int qarr_int(const void* e1, const void* e2)//3把qarr_int的地址给了void bbule
{
return (*(int*)e1 - *(int*)e2);
}
void bbule(void* base, int num, int width, int(*qarr)(const void* e1, const void* e2))//2
{
int i = 0;
for (i = 0; i < num - 1; i++)
{
int j = 0;
for (j = 0; j < num - 1-i; j++)
{
if (qarr((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
{
//交换
swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
}
}
}
}
void test()//1
{
int arr[] = { 9,8,7,11,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
bbule(arr, sz, sizeof(arr[0]), qarr_int);
print(arr, sz);
}
struct Stu//先定义后使用函数放在下面
{
char name[20];
int age;
double score;//成绩
};
void print2(struct Stu* arr, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%s %d %.1lf ", arr->name, arr->age, arr->score);
arr++;
}
}
void swap(char* buf1, char* buf2, int width)//4
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
//按年龄排序
int qarr_int_age(const void* e1, const void* e2)//3把qarr_int的地址给了void bbule
{
return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
//按字母排序
int qarr_name(const void* e1, const void* e2)
{
return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}
void bbule(void* base, int num, int width, int(*qarr)(const void* e1, const void* e2))//2
{
int i = 0;
for (i = 0; i < num - 1; i++)
{
int j = 0;
for (j = 0; j < num - 1 - i; j++)
{
if (qarr((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
{
//交换
swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
}
}
}
}
void test2()
{
struct Stu arr[3] = { {"zhangsan", 20, 55.5}, {"lisi", 70, 30.6}, {"wangwu", 50, 60.8} };
int sz = sizeof(arr) / sizeof(arr[0]);
//bbule(arr, sz, sizeof(arr[0]), qarr_int_age);
bbule(arr, sz, sizeof(arr[0]), qarr_name);
print2(&arr, sz);
}
int main()
{
//test();
test2();
return 0;
}