2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The bubble sortIn ascending orderEach traversal will remove the unsorted collectionLarger values are moved to the back(or move the smaller ones to the front) until they are in order.
#include <stdio.h>
#include <stdbool.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// 要排序的数组 数组大小
int bubbleSort(int* arr, int sz)
{
// 1.n个数中,每次交换前需要比较两个数字,则比较次数为n - 1
for (int i = 0; i < sz - 1; ++i)
{
bool flag = true; // 3.检查是否有序
// 2.在 “1.” 的基础之上,i每循环一次,必定有一个数排好到后面,则 “- i" 来优化
for (int j = 0; j < sz - 1 - i; ++j)
{
if (arr[j] > arr[j + 1])
{
flag = false; // 3.表示无序
swap(&arr[j], &arr[j + 1]);
}
}
if (flag == true) // 3.有序直接退出循环
{
break;
}
}
}
int main()
{
int arr[10] = { 3, 9, 2, 7, 8, 5, 6, 1, 10, 4 };
int sz = 10;
bubbleSort(arr, sz);
for (int i = 0; i < sz; ++i)
{
printf("%d ", arr[i]);
}
return 0;
}
The above C language bubble sort code only supports integer sorting. Here it is expanded toGeneric sorting of sequential typesCode.
Refer to the C language built-in qsort sort:
The function can be obtained as:
void swap(char* a, char* b, size_t width)
{
for (int i = 0; i < width; ++i)
{
char temp = *(a + i);
*(a + i) = *(b + i);
*(b + i) = temp;
}
}
// 要排序的数组 数组大小 每个元素宽度 比较的函数指针
int bubbleSort(void* base, size_t sz, size_t width, int (*compare)(const void* e1, const void* e2))
{
// 1.n个数中,每次交换前需要比较两个数字,则比较次数为n - 1
for (int i = 0; i < sz - 1; ++i)
{
bool flag = true; // 3.检查是否有序
// 2.在 “1.” 的基础之上,i每循环一次,必定有一个数排好到后面,则 “- i" 来优化
for (int j = 0; j < sz - 1 - i; ++j)
{
if (compare((char*)base + j * width, (char*)base + j * width + width) > 0)
{
flag = false; // 3.表示无序
swap((char*)base + j * width, (char*)base + j * width + width, width);
}
}
if (flag == true) // 3.有序直接退出循环
{
break;
}
}
}
In fact, we only need toTwo places have been significantly changed, we can get the general sorting:
Change the comparison method toFunction PointersIn this way, users canWrite your own comparison type function(Not only built-in types, but also struct types are allowed, but the premise iscontinuously)
If the user sorts integers, the compare method written by the user is (for reference only, the method is not unique):
int cmp(const void* e1, const void* e2)
{
// (int*)e1 表示将泛型指针转为整型指针
// *((int*)e1) 表示对整型指针解引用从而得到整型的数
// 两整型的数相减,为正则e1大,为负则e2大,为0则相等
return *((int*)e1) - *((int*)e2);
}
Here you just need to change the exchange method to byte by byte.
Then swap should be changed to:
void swap(char* a, char* b, size_t width)
{
// a 和 b 表示两个数开始的地址
// a + i 表示 a 元素第 i 块字节的地址,同理于b
// *(a + i) 表示 a 元素第 i 块字节的内容,同理于b
// 通过一个字节一个字节的交换,确保内容不会丢失
for (int i = 0; i < width; ++i)
{
char temp = *(a + i);
*(a + i) = *(b + i);
*(b + i) = temp;
}
}
Complete code:
#include <stdio.h>
#include <stdbool.h>
void swap(char* a, char* b, size_t width)
{
for (int i = 0; i < width; ++i)
{
char temp = *(a + i);
*(a + i) = *(b + i);
*(b + i) = temp;
}
}
// 要排序的数组 数组大小 每个元素宽度 比较的函数指针
int bubbleSort(void* base, size_t sz, size_t width, int (*compare)(const void* e1, const void* e2))
{
// 1.n个数中,每次交换前需要比较两个数字,则比较次数为n - 1
for (int i = 0; i < sz - 1; ++i)
{
bool flag = true; // 3.检查是否有序
// 2.在 “1.” 的基础之上,i每循环一次,必定有一个数排好到后面,则 “- i" 来优化
for (int j = 0; j < sz - 1 - i; ++j)
{
if (compare((char*)base + j * width, (char*)base + j * width + width) > 0)
{
flag = false; // 3.表示无序
swap((char*)base + j * width, (char*)base + j * width + width, width);
}
}
if (flag == true) // 3.有序直接退出循环
{
break;
}
}
}
int cmp(const void* e1, const void* e2) // 对整形
{
return *((int*)e1) - *((int*)e2);
}
int cmp1(const void* e1, const void* e2) // 对字符
{
return *((char*)e1) - *((char*)e2);
}
int cmp2(const void* e1, const void* e2) // 对浮点
{
double num1 = *(double*)e1;
double num2 = *(double*)e2;
// double 返回与 int 冲突会影响,只需更改一下返回逻辑
return num1 > num2 ? 1 : -1;
}
int main()
{
int arr[10] = { 3, 9, 2, 7, 8, 5, 6, 1, 10, 4 };
int sz = 10;
bubbleSort(arr, sz, sizeof(int), cmp);
for (int i = 0; i < sz; ++i)
{
printf("%d ", arr[i]);
}
printf("n");
char arr1[10] = { 3, 9, 2, 7, 8, 5, 6, 1, 10, 4 };
double arr2[10] = { 3.1, 9.4, 2.9, 7.8, 8.8, 5.1, 6.2, 1.0, 10.1, 4.4 };
bubbleSort(arr1, sz, sizeof(char), cmp1);
bubbleSort(arr2, sz, sizeof(double), cmp2);
for (int i = 0; i < sz; ++i)
{
printf("%d ", arr1[i]);
}
printf("n");
for (int i = 0; i < sz; ++i)
{
printf("%.2lf ", arr2[i]);
}
printf("n");
return 0;
}
Complete code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
void swap(char* a, char* b, size_t width)
{
for (int i = 0; i < width; ++i)
{
char temp = *(a + i);
*(a + i) = *(b + i);
*(b + i) = temp;
}
}
// 要排序的数组 数组大小 每个元素宽度 比较的函数指针
int bubbleSort(void* base, size_t sz, size_t width, int (*compare)(const void* e1, const void* e2))
{
// 1.n个数中,每次交换前需要比较两个数字,则比较次数为n - 1
for (int i = 0; i < sz - 1; ++i)
{
bool flag = true; // 3.检查是否有序
// 2.在 “1.” 的基础之上,i每循环一次,必定有一个数排好到后面,则 “- i" 来优化
for (int j = 0; j < sz - 1 - i; ++j)
{
if (compare((char*)base + j * width, (char*)base + j * width + width) > 0)
{
flag = false; // 3.表示无序
swap((char*)base + j * width, (char*)base + j * width + width, width);
}
}
if (flag == true) // 3.有序直接退出循环
{
break;
}
}
}
typedef struct Student
{
char name[20];
int age;
char id[10];
} Student;
int cmpAge(const void* e1, const void* e2)
{
return ((Student*)e1)->age - ((Student*)e2)->age;
}
int cmpId(const void* e1, const void* e2)
{
return strcmp(((Student*)e1)->id, ((Student*)e2)->id);
}
int main()
{
Student arr[5] = {
{.name = "张三", .age = 20, .id = "1" },
{.name = "李四", .age = 21, .id = "2" },
{.name = "王二", .age = 18, .id = "3" },
{.name = "麻子", .age = 30, .id = "4" } };
int sz = 4;
bubbleSort(arr, sz, sizeof(Student), cmpAge);
printf("以年龄排序:n");
for (int i = 0; i < sz; ++i)
{
printf("%s ", arr[i].name);
printf("%d ", arr[i].age);
printf("%sn", arr[i].id);
}
printf("n");
bubbleSort(arr, sz, sizeof(Student), cmpId);
printf("以ID排序:n");
for (int i = 0; i < sz; ++i)
{
printf("%s ", arr[i].name);
printf("%d ", arr[i].age);
printf("%sn", arr[i].id);
}
printf("n");
return 0;
}
The above codeInvalid for discontinuous data, such as each element of the linked list isStored with pointer connection, the compare function and swap function need to be changed to solve this problem.