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;
}