le mie informazioni di contatto
Posta[email protected]
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
E l'ordinamento delle bolleIn ordine crescenteOgni volta che viene attraversato, la raccolta non ordinata verrà eliminataI valori più grandi vengono spostati in secondo piano(o spostare quelli piccoli in primo piano) finché non vengono ordinati.
#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;
}
Il codice di ordinamento a bolle del linguaggio C sopra supporta solo l'ordinamento di numeri interi. Qui è estesoOrdinamento sequenziale genericocodice.
Fare riferimento all'ordinamento qsort integrato nel linguaggio C:
La funzione può essere ottenuta come:
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 effetti, dobbiamo solo farloDue dei suoi posti sono stati significativamente modificati, puoi ottenere un ordinamento generale:
Cambia il metodo di confronto inpuntatore di funzionemodo, in modo che gli utenti possanoScrivi la tua funzione di tipo confronto(Non include solo i tipi incorporati, ma anche quelli definiti da struct, ma la premessa ècontinuamente)
Se l'utente ordina gli interi, il confronto scritto da lui stesso è (solo per riferimento, il metodo non è univoco):
int cmp(const void* e1, const void* e2)
{
// (int*)e1 表示将泛型指针转为整型指针
// *((int*)e1) 表示对整型指针解引用从而得到整型的数
// 两整型的数相减,为正则e1大,为负则e2大,为0则相等
return *((int*)e1) - *((int*)e2);
}
Qui devi solo cambiare il metodo di scambio in uno scambio byte per byte.
Quindi lo swap dovrebbe essere modificato in:
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;
}
}
Codice completo:
#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;
}
Codice completo:
#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;
}
Il codice sopraNon valido per dati discontinui, come lo è ogni elemento dell'elenco collegatomemorizzato tramite connessione puntatore, la funzione di confronto e quella di scambio devono essere modificate per essere risolte.