Qsort usage)

Source: Internet
Author: User

Quick sorting qsort is really powerful

I. Sorting int-type Arrays

Int num [100];

Int CMP (const void * a, const void * B)
{
Return * (int *) A-* (int *) B;
}

Qsort (Num, 100, sizeof (Num [0]), CMP );

2. Sort char array (same as int type)

Char word [100];

Int CMP (const void * a, const void * B)
{
Return * (char *) A-* (int *) B;
}

Qsort (word, 100, sizeof (word [0]), CMP );

Iii. Sort double-type Arrays

Double in [100];

Int CMP (const void * a, const void * B)
{
Return * (double *) A> * (double *) B? 1:-1;
}

Qsort (in, 100, sizeof (in [0]), CMP );

4. sorting the struct at a level

Struct sample
{
Double data;
Int Other;
} S [100]

// Sort by data values from small to large struct

Int CMP (const void * a, const void * B)
{
Return (* (sample *) a). Data <(* (sample *) B). Data? -1: 1;
}

Qsort (S, 100, sizeof (s [0]), CMP );

5. List struct

Struct sample
{
Int X;
Int y;
} S [100];

// Sort by X from small to large, and sort by Y from large to small when X is equal

Int CMP (const void * a, const void * B)
{
Struct sample * c = (sample *);
Struct sample * D = (sample *) B;
If (c-> X! = D-> X) return C-> X-D-> X;
Else return D-> Y-C-> Y;
}

Qsort (S, 100, sizeof (s [0]), CMP );

6. Sort strings

Struct sample
{
Int data;
Char STR [100];
} S [100];

// Sort the string 'str' in alphabetical order.

Int CMP (const void * a, const void * B)
{
Return strcmp (* (sample *) A)-> STR, (* (sample *) B)-> Str );
}

Qsort (S, 100, sizeof (s [0]), CMP );

Append a complete vertex code to sort the string two-dimensional array:

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

Char s [1, 2001] [2, 1001];

Int CMP (const void * a, const void * B ){
Return strcmp (char *) A, (char *) B );
}

Int main (){
Int I, N;
Scanf ("% d", & N );
Getchar ();
For (I = 0; I <n; I ++) gets (s [I]);
Qsort (S, N, 1001 * sizeof (char), CMP );
For (I = 0; I <n; I ++) puts (s [I]);
Return 0;
}

Quick sorting qsort is really powerful

I. Sorting int-type Arrays

Int num [100];

Int CMP (const void * a, const void * B)
{
Return * (int *) A-* (int *) B;
}

Qsort (Num, 100, sizeof (Num [0]), CMP );

2. Sort char array (same as int type)

Char word [100];

Int CMP (const void * a, const void * B)
{
Return * (char *) A-* (int *) B;
}

Qsort (word, 100, sizeof (word [0]), CMP );

Iii. Sort double-type Arrays

Double in [100];

Int CMP (const void * a, const void * B)
{
Return * (double *) A> * (double *) B? 1:-1;
}

Qsort (in, 100, sizeof (in [0]), CMP );

4. sorting the struct at a level

Struct sample
{
Double data;
Int Other;
} S [100]

// Sort by data values from small to large struct

Int CMP (const void * a, const void * B)
{
Return (* (sample *) a). Data <(* (sample *) B). Data? -1: 1;
}

Qsort (S, 100, sizeof (s [0]), CMP );

5. List struct

Struct sample
{
Int X;
Int y;
} S [100];

// Sort by X from small to large, and sort by Y from large to small when X is equal

Int CMP (const void * a, const void * B)
{
Struct sample * c = (sample *);
Struct sample * D = (sample *) B;
If (c-> X! = D-> X) return C-> X-D-> X;
Else return D-> Y-C-> Y;
}

Qsort (S, 100, sizeof (s [0]), CMP );

6. Sort strings

Struct sample
{
Int data;
Char STR [100];
} S [100];

// Sort the string 'str' in alphabetical order.

Int CMP (const void * a, const void * B)
{
Return strcmp (* (sample *) A)-> STR, (* (sample *) B)-> Str );
}

Qsort (S, 100, sizeof (s [0]), CMP );

Append a complete vertex code to sort the string two-dimensional array:

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

Char s [1, 2001] [2, 1001];

Int CMP (const void * a, const void * B ){
Return strcmp (char *) A, (char *) B );
}

Int main (){
Int I, N;
Scanf ("% d", & N );
Getchar ();
For (I = 0; I <n; I ++) gets (s [I]);
Qsort (S, N, 1001 * sizeof (char), CMP );
For (I = 0; I <n; I ++) puts (s [I]);
Return 0;
}

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.