1 Back-tune function
1.1 Problem leads
When we want to implement the sort, ascending and descending, are written dead in the program, if you want to change only
The original code, then if the program is given in the form of a library. And so what?
#include <stdio.h>
void selectsort (int *p, int n)
{for
(int i=0; i<n-1 i + +)
{for
(int j=i +1; j<n; J + +)
{
if (P[i] < p[j])
{
P[i] = p[i]^p[j];
P[J] = p[i]^p[j];
P[i] = p[i]^p[j];
}}} int main (void)
{
int arr[10] = {6,5,4,3,2,1,7,8,9,0};
Selectsort (arr,10);
for (int i=0; i<10; i++)
{
printf ("%d\n", Arr[i]);
return 0;
}
1.2 callback (function as parameter)
To solve the problem of not modifying the original code or the library file, we provide a callback function for the sort function. ·
#include <stdio.h> int callbackcompare (int a,int b) {return a<b?1:0;} void selects
ORT (int *p, int n,int (*PF) (Int,int)) {for (int i=0; i<n-1; i + +) {for (int j=i+1; j<n; j + +)
{if (PF (P[i],p[j])) {P[i] = p[i]^p[j];
P[J] = P[i]^p[j];
P[i] = P[i]^p[j];
int main (void) {int arr[10] = {6,5,4,3,2,1,7,8,9,0};
Selectsort (Arr,10,callbackcompare);
for (int i=0; i<10; i++) {printf ("%d\n", Arr[i]);
return 0; }