Previous pointer explanation , Mentioned qsort function , This function is used to sort , And then I did it myself qsort function , Just share it as a note :
#include<stdio.h> #include<string.h> // realization bubble_sort Function programmer , Does he know the data type to be sorted in the future : I don't know .
// The programmer doesn't know : Two element types to compare . void Swap(char* buf1, char* buf2, int width) { int i = 0;
for (i = 0; i < width; i++) { char tmp = *buf1; *buf1 = *buf2; *buf2 = tmp; buf1
++; buf2++; } } void bubble_sort(void* base, int sz, int width, int (*cmp)(void*
e1, void* e2)) { int i = 0; // Number of trips . for (i = 0; i < sz - 1; i++) { // Logarithm of each comparison .
int j = 0; for (j = 0; j < sz - 1 - i; j++) { // Comparison of two elements . if (cmp((char*)base + j
* width, (char*)base + (j + 1) * width) > 0) { // exchange Swap((char*)base + j * width
, (char*)base + (j + 1) * width, width); } } } } int cmp_int(void* e1, void* e2)
{ // Comparing two integer values return *(int*)e1 - *(int*)e2; } void test1() { int arr[10] = { 9,8,
7,6,5,4,3,2,1,0 }; int sz = sizeof(arr) / sizeof(arr[0]);
// use bubble_sort A programmer must know what data he is sorting . // You should know how to compare the elements in the array to be sorted . bubble_sort(arr, sz,
sizeof(arr[0]), cmp_int); int i = 0; for (i = 0; i < sz; i++) { printf("%d ",
arr[i]); } printf("\n"); } struct Stu { char name[20]; int age; }; //int
cmp_stu_by_age(void* e1, void* e2) //{ // return ((struct Stu*)e1)->age -
((struct Stu*)e2)->age; //} int cmp_stu_by_name(void* e1, void* e2) {
// Comparing names is comparing strings . // String comparison cannot be used directly ><= To compare , Should use strcmp function .
//strcmp The header file corresponding to the function is #include<string.h>. return strcmp(((struct Stu*)e1)->name, ((
struct Stu*)e2)->name); } void test2() { struct Stu s[3] = { {"zhangsan",20},{
"lisi",30},{"wangwu",10} }; int sz = sizeof(s) / sizeof(s[0]); //bubble_sort(s,
sz, sizeof(s[0]), cmp_stu_by_age); bubble_sort(s, sz, sizeof(s[0]),
cmp_stu_by_name); int i = 0; for (i = 0; i < sz; i++) { printf("%8s-%2d\n", s[i]
.name, s[i].age); } printf("\n"); } int main() { test1(); test2(); return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq