[C] Sorting algorithm qsort

There are many sorting methods: selection sort, bubble sort, merge sort, quick sort, etc. By the name, you know that quick sort is currently recognized as a better sorting algorithm. Because it is very fast, the system also implements this algorithm in the library, which is convenient for us to use. This is the qsort function (quicksort in full). It is provided in the ANSI C standard, and its declaration is in the stdlib.h file. It is written according to the dichotomy, and its time complexity is n*log(n)

  Function: Use quick sort routines to sort
  Header file: stdlib.h
  Usage: void qsort(void* base,size_t num,size_t width,int(__cdecl*compare)(const void*,const void*)); 
  Parameters: 1 The array to be sorted, and the result after sorting is still placed in this array.
      2 The number
        of elements to be sorted in the array. 3 The space occupied by each element (in bytes).
             4 A pointer to a function to determine the order of sorting. Define a comparison function)

 

  qsort requires a comparison function defined by itself. The comparison function makes qsort more versatile. With the comparison function qsort, you can sort arrays, strings, structures and other structures in ascending or descending order.


  For example, the comparison function int cmp(const void *a, const void *b) has two elements as parameters (the format of the parameters cannot be changed) and returns an int value. The function of the comparison function cmp is to specify the size of the element to qsort Comparison of.

 

 

Several common comparison functions cmp in qsort
 

One, sort the int type array


int num[100];
int cmp_int(const void* _a, const void* _b) //The parameter format is fixed
{     int* a = (int*)_a; //Forced type conversion     int* b = (int*)_b;     return *a-*b;  }



qsort(num,100,sizeof(num[0]),cmp_int); 

 

  It can be seen that the parameter list is two null pointers, and now he is going to point to your array elements. So convert to your current type, and then take the value. The default ascending order (from small to large), if you want to sort in descending order, return *b-*a.

 

 

2. Sort the char type array (same as int type)


char word[100];
int cmp_char(const void* _a, const void* _b) //The parameter format is fixed
{     char* a = (char*)_a; //Forced type conversion     char* b = (char*)_b;     return *a-*b;  }



qsort(word,100,sizeof(word[0]),cmp_char); 

 

 

Third, sort the double array


double in[100];
int cmp_double(const void* _a, const void* _b) //The parameter format is fixed
{     double* a = (double*)_a; //Forced type conversion     double* b = (double*)_b;     return *a> *b? 1: -1; //Special attention }



qsort(in,100,sizeof(in[0]),cmp_double); 

 

  The ternary operator must be used for floating point or double type, because if you use subtraction like integer type, if it is two close numbers, it may return a small decimal number (greater than -1, less than 1 ), and the return value of cmp is int type, so the decimal will be returned to 0, the system considers it to be equal, and loses the original size relationship

 

 

Fourth, sort the strings


char word[100][10];
int cmp_string(const void* _a, const void* _b) //The parameter format is fixed
{     char* a = (char*)_a; //Forced type conversion     char* b = (char* )_b;     return strcmp(a,b); }



qsort (Word, 100, sizeof (Word [0]), cmp_string);
--------------------- 
Author: zhao888789 
Source: CSDN 
Original: https: // blog.csdn.net/zhao888789/article/details/79186619 
Copyright statement: This article is the original article of the blogger, please attach the link to the blog post if you reprint it!

Guess you like

Origin blog.csdn.net/m0_37362454/article/details/89326610