Callback functions implement sorting of shaped arrays and strings

Source: Internet
Author: User
Tags function prototype strcmp

(a) bubble sort implements sorting of shaped arrays

Previously, we would only use the bubble sort method to sort the entire array of rows, a simple two for loop, the outer loop to control the number of cycles, the inner loop control the number of comparisons, like the following code, can be implemented:

#include < stdio . h >

int Main ()

{

int Arr[10] = {11,9,8,7,6,5,4,3,2,1} ;

int i=0;

int J = 0;

int =

for (j=0;j<10;j+ +)

   {  for(i=0;i<Ten-J;i++)

if (Arr[i]>arr[i+1])

{

tmp = Arr[i];

Arr[i] = arr[i+1];

arr[i+1] = tmp;

}

   }

for (i=0;i<10;i+ +)

printf ("%d", Arr[i]);

return 0;

Based on this, we can use the callback function to implement the sorting of the shaped array and the string simultaneously.


(b) What is a callback function

A callback function is a function that is called through a function pointer (void (*perfect) (int)). If you pass the pointer (address) of the function as a parameter to another function void Mycallback (void (*perfect) (int), int n), when the pointer is used to invoke the function it points to, we say this is a callback function.

Popular point of this can be understood, you go to a shop to buy things, just what you want to have no goods, so you left your phone in the shop clerk, after a few days the store has goods, the clerk called you, and then you received a call to the store to pick up the goods. In this example, your phone number is called a callback function, you leave the phone to the clerk called the registration callback function, the store later in the goods called triggered the callback associated event, the clerk called you called Call callback function, you go to the store to fetch goods called response callback event. The answer is complete.

Why use a callback function?

Because the caller can be separated from the callee. The caller does not care who the callee is, all it needs to know, but there is a called function that has a certain prototype, some restrictions (such as a return value of int).

Here's a simple example of a callback function that I think is easier to understand than the other complex code:

#include <stdio.h>
#include <stdlib.h>
void Perfect (int n)
{
int i=1;
int count=0;
for (i=1;i<n;i++)
{

if (0==n%i)
{
Count+=i;
}
}
if (count==n)
printf ("%d is the end number \ n";
else printf ("%d is not an end number \ n";
}
void Mycallback (void (*perfect) (int), int n)
{
Perfect (n);
}

int main ()
{
int n;
printf ("Please enter a positive integer \ n");
scanf ("%d", &n);

Mycallback (Perfect,n);
return 0;

}

(iii) The callback function implements sorting of shaped arrays and strings

Bubble sort can be used to sort data of any type, the function prototype is as follows:

void Bubble (void *base, int count, int length, int (*cmp) (const void *,const void *))

which

void *base: The first address of the array to be sorted

int count: The number of elements to be sorted in the array

int length: The size of each element occupying space

Int (*cmp) (const void *,const void *): A pointer to a comparison function that determines the order of the sort

The function prototypes for compare are:

int Cmp_char (const void *p1,const *p2)

{

Assert (p1);

ASSERT (P2);

Return strcmp ((char *) (* (int *) p1), (char *) (* (int *) p2));

}

The following code writes out the comparison function for the character array and the Shaping array:

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

#include <string.h>

/* Integer array comparison function */

int cmp_int (const void *P1, const void *P2)

{

Assert (p1);//Check the validity of pointers

ASSERT (P2);

The exchange is not exchanged as long as the >0 is satisfied (* (int *) p1 > * (int *) p2))

Return (* (int *) p1 > * (int *) p2)? 1:-1;

if (* (int *) p1 > * (int *) p2)

return 1;

else if (* (int *) P1 = = * (int *) p2)

return 0;

else return-1;

}

/* Character array comparison function */

int Cmp_char (const void *p1,const *p2)

{

Assert (p1);

ASSERT (P2);

Return strcmp ((char *) (* (int *) p1), (char *) (* (int *) p2)),//Convert the P1 coercion type to shaping, and then truncate the reference because the string is being compared, so it is forced again to convert the type to a character type

}

/* Swap function */

void swap (void *p1, void *p2, int size)

{

int i = 0;

Assert (p1);

ASSERT (P2);

for (i = 0; i < size; i++)

{

Char temp = * ((char *) p1+i);

* ((char *) p1+i) = * ((char *) p2+i);

* ((char *) p2+i) = temp;

}

}

/* Bubble Sort */

void Bubble (void *base, int count, int length, int (*cmp) (const void *,const void))//Use callback function to implement

{

int i = 0;

int j = 0;

ASSERT (base);

for (i = 0; i < count-1; i++)

{

for (j = 0; J < count-i-1; j + +)

{

if (CMP (char*) base + length*j, (char*) base + length* (j + 1)) >0)

Swap ((char*) base + length*j, (char*) base + length* (j + 1), length);

}

}

}


int main ()

{

int arr[] = {2, 3, 6, 7, 0, 9, 1, 5, 4, 8};

Char *str[] = {"CDEFC", "Dcbar", "Aefva", "BEKSF", "Cbsle", "CDEFG"};

int len = sizeof (arr)/sizeof (arr[0]);

int size = sizeof (str)/sizeof (str[0]);

int I, J;

Bubble (arr, len,sizeof (int *), cmp_int);

Bubble (str, size,sizeof (char *), Cmp_char);

for (i = 0; i < len; i++)

{

printf ("%d", arr[i]);

}

printf ("\ n");

for (j = 0; J < size; J + +)

{

printf ("%s\n", Str[j]);

}

System ("pause");

return 0;

}







Callback functions implement sorting of shaped arrays and strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.