Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to use sorting function? Find an example of sorting an array of integers. .
How to use sorting function? Find an example of sorting an array of integers. .
First, define a comparison function.

Boolean cmp(int a, int b)

{

If (a>b) returns true.

Returns false

}

Then when calling sort, you just need to write your own defined comparison function in the third parameter, such as

sort(a,a+n,CMP);

Is an array of integers.

The sample program is as follows (by the way, despise the person who wants to score above):

# include & lt algorithm & gt

# include & ltiostream & gt

Use namespace std

int a[ 10];

Boolean cmp(int a, int b)

{

If (a>b) returns true.

Returns false

}

int main()

{

for(int I = 0; I<5; i++)

scanf("%d ",a+I);

sort(a,a+5,CMP);

for(int I = 0; I<5; i++)

printf("%d ",a[I]);

System ("suspended");

Returns 0;

}