Commonly used sorting function in C language - qsort

Article directory

1. Introduction to qsort

Two, qsort detailed explanation

3. Type

Int

double

character

string

4. Example code

1. Introduction to qsort

原型:void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));

Explanation in C plusplus:

Sort the elements of an array

Sort the elements pointed to by in the array, each element is byte long, use this function to determine the order.

The sorting algorithm used by this function compares pairs of elements by calling the specified function with pointers to them as arguments.

The function returns no value, but modifies the contents of the array pointed to by reordering the elements of the array (as defined).

The order of equivalent elements is undefined.


Two, qsort detailed explanation

头文件:#include<stdlib.h>
原型:void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*))

base: It can be an array or a pointer (note: this pointer should point to an array);

size_t num: array length, according to cplusplus definition, he is unsigned  int

size_t size: The bytes occupied by the array elements, according to the definition of cplusplus, it is unsigned  int

compare: Pointer to a function that compares two elements.
Call this function repeatedly to compare two elements. It should follow the following prototype: qsort

int compar (const void* p1, const void* p2);

That is, it can also be a function name.

The code is as follows (example):

/* qsort example */#include <stdio.h>      
/* printf */#include <stdlib.h>     /* qsort */
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}//此处为整形递增排序
int main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}

focus:

The most important part of this function is int (*compar)(const void*, const void*) ;

Because the main changes are in this part, including the data type and ascending and descending order. In fact, other parts can be filled in according to the rules. Among them, size_t size can be calculated by using sizeof() to calculate the size of one of the array elements.


3. Type

Int

int compare (const void * a, const void * b)
{
return *(double*)a >*(double *)b ? 1 : -1;//升序
return *(double*)a <*(double *)b ? 1 : -1;//降序
}

double

int compare (const void * a, const void * b)
{
return *(double*)a >*(double *)b ? 1 : -1;//升序
return *(double*)a <*(double *)b ? 1 : -1;//降序
}

character

int compare (const void * a, const void * b)
{
   return *(char *)a - *(char *)b;//升序
return *(char *)b - *(char *)a;//降序
}

string

int compare (const void * a, const void * b)
{
return strcmp(*(const char**)a, *(const char**)b);//升序
return strcmp(*(const char**)b, *(const char**)a);//降序   
}

4. Example code

#include <stdio.h>      
#include <stdlib.h>     /* qsort */

//Int
int compare1(const void* a, const void* b)
{
    return (*(int*)a - *(int*)b);//升序
    //return ( *(int*)b - *(int*)a );//降序
}

//Double
int compare2(const void* a, const void* b)
{
    return *(double*)a > *(double*)b ? 1 : -1;//升序
    //return *(double*)a < *(double*)b ? 1 : -1;//降序
}

//字符
int compare3(const void* a, const void* b)
{
    return *(char*)a - *(char*)b;//升序
    //return *(char*)b - *(char*)a;//降序
}

//字符串
int compare4(const void* a, const void* b)
{
    return strcmp(*(const char**)a, *(const char**)b); // 升序
    //return strcmp(*(const char**)b, *(const char**)a);//降序   
}

int main()
{
    int values1[] = { 40, 10, 100, 90, 20, 25 };
    double values2[] = {1.2,0.42,0.2,8,3.14,15.2};
    char values3[] = { 'e', 's', 'a', 'w', 'k', 'l' };
    char* values4[] = { "nobody","everything","anything","nonthing","anybody","something" };
    int n;

    qsort(values1, 6, sizeof(int), compare1);
    qsort(values2, 6, sizeof(double), compare2);
    qsort(values3, 6, sizeof(char), compare3);
    qsort(values4, 6, sizeof(char*), compare4);

    printf("Int:");
    for (n = 0; n < 6; n++)
        printf("%d ", values1[n]);
    printf("\n");

    printf("double:");
    for (n = 0; n < 6; n++)
        printf("%.2lf ", values2[n]);
    printf("\n");

    printf("字符:");
    for (n = 0; n < 6; n++)
        printf("%c ", values3[n]);
    printf("\n");

    printf("字符串:");
    for (n = 0; n < 6; n++)
        printf("%s ", values4[n]);
    printf("\n");
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_64038246/article/details/130535965