Sort by qsort

Source: Internet
Author: User

Zookeeper

Bubble and fast sorting are common sorting methods. Here we use the qsort function in the header file for sorting. However, you must first create a CMP function.

 
# Include <stdlib. h> // qsort header file int A [100] = }; // The array struct person to be sorted // struct {char num [20]; char name [100]; int score; int sum;} Man [100];

1. Sort array []

 
Int cmp_1 (const void * a, const void * B) // sort the array a [] {return * (int *) A-* (int *) B ;} // call the function qsort (A, N, sizeof (A [0]), cmp_1); // n is the size of the array, where 9
 
2. sort by the score in the struct
 
Int cmp_2 (const void * a, const void * B) // rank {return (person *) A)-> score-(person *) by the score in the structure *) b)-> score;} // function call qsort (man, N, sizeof (MAN [0]), cmp_2 );
 
 
 
3. sort by sum first. If sum is equal, then sort by score.
 
Int cmp_3 (const void * a, const void * B) // first sort by sum. If sum is equal, then rank by score {struct person * c = (person *); struct person * D = (person *) B; If (c-> sum! = D-> sum) return C-> sum-D-> sum; else return C-> score-D-> score ;}
 
// Function call qsort (man, N, sizeof (MAN [0]), cmp_3 );
 
 
 
4. First sort by string num. If the string num is the same, then sort by string name.
Int cmp_4 (const void * a, const void * B) // first sort by string num. If the string num is the same, then rank {struct person * c = (person *) A by string name; struct person * D = (person *) B; If (strcmp (c-> num, d-> num )! = 0) return strcmp (c-> num, D-> num); else strcmp (c-> name, D-> name );}
 
// Function call qsort (man, N, sizeof (MAN [0]), cmp_4 );
 
 
 
5. sort by score first. If the score is the same, then sort by string num.
 
<PRE class = "CPP" name = "code"> int cmp_5 (const void * a, const void * B) // sort by score first. If the score is the same, {struct person * c = (person *) A; struct person * D = (person *) B; If (c-> score! = D-> score) return C-> score-D-> score; else return strcmp (c-> num, D-> num );} // function call qsort (man, N, sizeof (MAN [0]), cmp_5 );


 


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.