实现冒泡排序函数的编写,以及qsort内置函数的使用

  1. 时间
    2022年4月3日

  2. 环境
    Microsoft visua studio 2019

  3. 学习内容
    模仿qsort实现一个冒泡排序法,可以排序任意类型,包括整型数组,浮点型数组,还有结构体等

  4. 收获
    编写程序使用到了回调函数内容,熟悉掌握了回调函数用法

  5. 代码
    我们先使用内置函数qsort函数实现冒泡排序进行观察

  • 整型排序
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cmp_int(const void* a, const void* b)
{
    
    
	return *(int*)a - *(int*)b;
}
``int main()
{
    
    //整型排序
	int arr[] = {
    
     9,8,7,6,5,4,3,2,1,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_int);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	return 0;
}

  • 浮点型排序
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int cmp_float(const void* a, const void* b)
{
    
    
	return *(float*)a - *(float*)b;
}
int main()
{
    
    //浮点数排序
	float arr[] = {
    
     9.0,8.0,7.0,6.0,5.0,4.0,3.0,2.0,1.0 };
	int sz= sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_float);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%f ", arr[i]);
	}
	return 0;
}
  • 结构体排序
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int cmp_st_name(const void* a, const void* b)
{
    
    
	return *(((struct student*)a)->name) - *(((struct student*)b)->name);
}
int main()
{
    
    //结构体排序
	struct student st[] = {
    
     {
    
    "zhangsan",20},{
    
    "lisi",30},{
    
    "wanger",10} };
	int sz = sizeof(st) / sizeof(st[0]);
	//qsort(st, sz, sizeof(st[0]), cmp_st_name);//根据首字母排序
	qsort(st, sz, sizeof(st[0]), cmp_st_age);//根据年龄排序
	return 0;
}

这部分我们使用自己编写的函数实现

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct student
{
    
    //创建结构体
	char name[20];
	int age;
};
int cmp_int(const void* a, const void* b)
{
    
    //比较两个整形大小
	return *(int*)a - *(int*)b;
}
int cmp_float(const void* a, const void* b)
{
    
    //比较两个浮点型大小
	return *(float*)a - *(float*)b;
}
int cmp_st_name(const void* a, const void* b)
{
    
    比较结构体大小,以名字首字母排序
	return *(((struct student*)a)->name) - *(((struct student*)b)->name);
}
int cmp_st_age(const void* a, const void* b)
{
    
    以年龄排序结构体
	return (((struct student*)a)->age) - (((struct student*)b)->age);
}
void exchang(char* a, char* b,int wide)
{
    
    //交换两个值
	int i = 0;
	for (i = 0; i < wide; i++)
	{
    
    
		char tmp = *a;
		*a = *b;
		*b = tmp;
		a++;
		b++;
	}
}
void bubble(void* arr, int sz, int wide, int(*cmp)(const void*, const void*))
{
    
    //自己编写的qsort函数
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
    
    //遍历每一趟
		int j = 0;
		for (j = 0; j < sz-1-i; j++)
		{
    
    每一趟进行调整
			if (cmp((char*)arr + j * wide, (char*)arr + (j + 1) * wide)>0)//如果前面的数大于后面的数,则交换两个数的位置
			{
    
    
				exchang((char*)arr + j * wide, (char*)arr + (j + 1) * wide, wide);
			}
		}
	}
}
  • 这个bubble函数使用的方法和qsort内置函数一样

猜你喜欢

转载自blog.csdn.net/m0_62101200/article/details/123938646