C language implements bubble sorting to arrange the elements in the array!

First draw a picture to understand the idea of ​​bubble sorting:

 Write the program according to the method in the figure :

 冒泡排序函数
void maopao(int arr[], int sz)
{
	int i = 0;
	//循环的趟数
	for (i = 0; i < sz - 1; i++)
	{
		int j = 0;

		// 1趟的循环(比较相邻的数大小排序)
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}

	}
}

// 打印数组函数
void print(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

int main()
{
	int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);


	print(arr, sz);  // 调用打印数组函数
	maopao(arr, sz); //调用冒泡排序函数
	print(arr, sz); // 调用打印数组函数


	return 0;
}

After writing it myself, I found that there is a library function qsort() that is also used for sorting, so let's learn it:

This library function requires four parameters :

1. void* base, //base stores the address of the first object in the data to be sorted

2. size_t num, // the number of data elements to be sorted

3. size_t width, // the size of an element in the data to be sorted, in bytes

4. i nt(__cdecl* compare)(const void* elem1, const void* elem2) // function pointer A function used to compare the two elements of the data center to be sorted

 Next is the code implementation:

#include<stdio.h>
#include<stdlib.h>

// 两个元素的比较函数
int* compare(const void* elem1, const void* elem2)
{
	return *(int*)elem1 - *(int*)elem2;    //返回第一个强制转换成整形指针类型的指针解引用 减去 第二个额强制转换成整型指针类型的指针解引用
}

// 打印数组函数
void print(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

int main()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);

	print(arr, sz);
	qsort(arr, sz, sizeof(arr[0]), compare);
	print(arr, sz);

	return 0;
}

Next, use the qsort() library function to sort the structure you wrote yourself:

#include<stdio.h>

struct STU
{
	char name[30];
	int age;
};

int sort_age(const void* elem1, const void* elem2)
{
	return ((struct STU*)elem1)->age - ((struct STU*)elem2)->age;
}

int sort_name(const void* elem1, const void* elem2)
{
	return strcmp(((struct STU*)elem1)->name , ((struct STU*)elem2)->name);
}

int main()
{
	struct STU s[] = {
   
   {"环环", 25},{"峰峰", 20},{"花花", 18}};
	int sz = sizeof(s) / sizeof(s[0]);

	//按照年龄来排序
	//qsort(s, sz, sizeof(s[0]), sort_age);

	//按照名字来排序
	qsort(s, sz, sizeof(s[0]), sort_name);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s %d \n", s[i], s[i].age);
	}

	return 0;
}

The qsort() library function is a versatile sorting function. Next, we need to write a qsort() function by ourselves, look at the code:

#include<string.h>
#include<stdio.h>
//模拟qsort()冒泡排序
void jiaohuan(char* p1, char* p2, int width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
		p1++;
		p2++;
	}
}


void maopao(void* star, 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*)star + j * width, (char*)star + (j + 1) * width) > 0)
			{
				jiaohuan((char*)star + j * width, (char*)star + (j + 1) * width, width);
			}
		}
	}

}

The above two functions realize all the functions of the qsort() library function, and the next step is to use the handwritten function to sort the structure data. Let's look at the code:

#include<string.h>
#include<stdio.h>
//模拟qsort()冒泡排序

// 交换函数  一个字节一个字节的交换  交换的宽度为传过来的数据的宽度
void jiaohuan(char* p1, char* p2, int width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
        //一个字节一个字节的交换
		char tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
		p1++;
		p2++;
	}
}

// 冒泡排序函数
void maopao(void* star, 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(回调函数的结果  大于 零) 回调函数就是比较两个指针中值的大小
			if (cmp((char*)star + j * width, (char*)star + (j + 1) * width) > 0)
			{
                // 调用交换函数(传入要调换的两个数的指针和数据宽度)
				jiaohuan((char*)star + j * width, (char*)star + (j + 1) * width, width);
			}
		}
	}

}

//创建结构体
struct STU
{
	char name[20];
	int age;
};


// 打印函数
void print(struct STU s[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s:%d ", s[i].name, s[i].age);
	}
	printf("\n");
}


// 比较姓名的回调函数(想要降序排列就把return后面的e1和e2对调)
int sort_name(const void* e1, const void* e2)
{
	return strcmp(((struct STU*)e1)->name, ((struct STU*)e2)->name);

}

// 比较年龄的回调函数(想要降序排列就把return后面的e1和e2对调)
int sort_age(const void* e1, const void* e2)
{
    // 把e2强制转换成结构体指针类型再强制转换成整型指针类型
	return (int*)((struct STU*)e2)->age - (int*)((struct STU*)e1)->age;
}

//主函数
int main()
{
	struct STU s[] = { {"张强",46},{"小李",38},{"芳华",56} ,{"郭华",16} };//初始化结构体
	int sz = sizeof(s) / sizeof(s[0]);  //计算结构体的字节数

	print(s, sz);  //调用打印函数
	//maopao(s, sz, sizeof(s[0]), sort_name); // 按姓名排序结构体
	maopao(s, sz, sizeof(s[0]), sort_age);  //按年龄排序结构体
	print(s, sz);  //调用打印函数
	return 0;
}

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/131939012