#define _CRT_SECURE_NO_WARNINGS#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h>typedef struct//define student structure {Char name[20];char number [20];int score;} STUDENT;INT&NBSP;STRUCT_CMP (CONST&NBSP;VOID&NBSP;*ELEM1,&NBSP;CONST&NBSP;VOID&NBSP;*ELEM2) {assert (ELEM1); ASSERT (ELEM2);//Assert return ((* (student*) elem1) .score - (* (student*) elem2). score);// Determines the size}int int_cmp (CONST&NBSP;VOID&NBSP;*ELEM1,&NBSP;CONST&NBSP;VOID&NBSP;*ELEM2)//integer comparison function by comparing the student's score { ASSERT (ELEM1); assert (ELEM2);//Assert return (* (int *) elem1 - * (int *) elem2);// Elem1 and ELEM2 are the addresses of two int variables, so convert elem1 and elem2 to int* type,//And then truncate, access two int variables, return a value of two int variable, if the first number is greater than the second// Returns an integer equal to 0, less than the returned negative number. }INT&NBSP;STR_CMP (CONST&NBSP;VOID&NBSP;*ELEM1,&NBSP;CONST&NBSP;VOID&NBSP;*ELEM2) {assert (ELEM1); assert (ELEM2); /Assert return strcmp ((char*) * (int *) elem1, (char*) * (int *) elem2);//elem1 and elem2 are character pointers that hold string addresses. So you need to access 4 bytes,//Make it strongType to int*, and then truncate the reference to the string pointer, and finally cast its coercion type to (char *).} VOID&NBSP;SWP (Void *elem1, void *elem2, size_t width)//size_t width is the space occupied by the type, Because do not know what type of Exchange is, //so you need to pass in one more parameter (representing the size of the type). The {assert (ELEM1) is then exchanged in bytes one by one, assert (ELEM2),//Assert size_t i = 0;//byte-by-bit switching, and then exchange two variables for (i = 0; i < width; i++) {char tmp = * ((char*) elem1 + i); * (( char*) elem1 + i) = * ((char*) elem2 + i); * ((char*) elem2 + i) = tmp;}} Void bubble (Void *arr, size_t num, size_t width, int (*compare) (const VOID&NBSP;*ELEM1,&NBSP;CONST&NBSP;VOID&NBSP;*ELEM2)) //int (*compare) (Const void *elem1, &NBSP;CONST&NBSP;VOID&NBSP;*ELEM2 is a comparison function pointer {assert (ARR); assert (compare);//Assert Size_t i = 0, j = 0;for (i = 0; i < num - 1; i++) {for (j = 0; j < num - 1 - i; j++) {if (compare ((char*) arr + j*width), (char*) arr + (j + 1) *width)//Because you do not know the type of function to compare, use the size of the type as a function parameter, //and unified conversion to (char *) for byte-by-bit conversion (char*) ARR&NBSP;+&NBSP;J*WIDTH{SWP ((char*) arr + j*width, (char*) arr + (j + 1) *width, width);}}} Int main () {char *arr1[] = { "AMDGDJ", "Lsansk", "AAAAA", "UEOJJFDH", "Dcsaouid" }; Int sz = sizeof (arr1) / sizeof (arr1[0]); Bubble (arr1, sz, sizeof (arr1[0)), &NBSP;STR_CMP);int i = 0;for (i = 0; i < sz; i++) {printf ( "%s\n", arr1[i]);} Int arr2[] = { 1,3,7,7,6,4,9,4,3,8,5 };sz = sizeof (ARR2) / sizeof (arr2[0]); Bubble (Arr2, sz, sizeof (arr2[0]), int_cmp);for (i = 0; i < sz; i++) {printf ("%d ", arr2[i]);} printf ("\ n"); student arr3[] = { {"Zhang San", "201312030120", 94},{" John Doe "," 201312030118 ", 98},{" Pock "," 201312030116 ", 96} };sz = sizeof (ARR3) / sizeof (arr3[0]); Bubble (Arr3, sz, sizeof (arr3[0]), struct_cmp);for (i = 0; i < sz; i++) {printf ("%s %s %d\n", arr3[i].name, arr3[i].number, arr3[i]. Score);} System ("pause"); return 0;}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/77/3F/wKioL1ZlszLR5-UJAAAeqBU-S0Y669.png "title=" 111. PNG "alt=" Wkiol1zlszlr5-ujaaaeqbu-s0y669.png "/>
The callback function implements the various sorts of bubbling method