C Language Library Function--qsort

Article Directory

header file

<stdlib.h>

Function declaration

void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));

Parameter declaration

base:Array to sort
 nmemb: Number of elements in the array
 size: Each array element takes up memory space and can be obtained using sizeof
 compar: A comparison function that compares two elements of an array.When the first parameter value of this comparison function is less than, equal to, or greater than the second parameter value, the return values of this comparison function should be less than, equal to, or greater than zero, respectively.

That means you want to implement a function like this (ascending order):
int cmp(const void *a, const void *b)
If a > b, return > 0
 Returns 0 if a == b
 If a < b, return <0
 The relationship between a and b here is only logical, not a comparison of values, so sorting can be more than numbers, but also characters.

Some common examples

1. Sort one-dimensional arrays

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

int cmp ( const void *a , const void *b){
        return *(int *)a - *(int *)b;
}

int main()
{
   int array[20] = {0};
   //Generate a random one-dimensional array
   int i = 0;
   for (i=0;i<20;i++){
       array[i] = rand()%20;
   }
   
   for (i=0;i<20;i++){
       printf("%d\n",array[i]);
   }
   
   qsort(array,20,sizeof(int),cmp);
   printf("sort result:\n");
   for (i=0;i<20;i++){
       printf("%d\n",array[i]);
   }
   return 0;
}

2. Sorting a two-dimensional array

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

int comp(const void *a,const void *b){
     return ((int *)a)[0]-((int *)b)[0];
}

int main()
{
    int array[20][5] = {{0}};
    //Generate a random two-dimensional array
    int i = 0,j = 0;
    for (i=0;i<20;i++){
        for (j=0;j<5;j++){
            array[i][j] = rand()%20;
        }
    }
    
    for (i=0;i<20;i++){
        for (j=0;j<5;j++){
            printf("%-5d",array[i][j]);
        }
        printf("\n");
    }

    qsort(array,20,sizeof(int)*5,comp);    
    printf("sort result:\n");
    for (i=0;i<20;i++){
        for (j=0;j<5;j++){
            printf("%-5d",array[i][j]);
        }
        printf("\n");
    }    
    return 0;
}

3. Sort arrays of char type

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

int cmp( const void *a , const void *b){
        return *(char *)a - *(char *)b;
}

int main()
{
    char word[30] = {0};
    
    sprintf(word,"%s","happy");
    printf("%s\n",word);
    
    qsort(word,5,sizeof(char),cmp);    
    printf("%s\n",word);
    
    return 0;
}

4. Sort arrays of type double

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

int cmp( const void *a , const void *b){
        return *(double *)a > *(double *)b ? 1 : -1;
}

int main()
{
    int i = 0;
    double num[20] = {0};
    
    /*Generate 0-20 random decimals*/
    for (i=0; i<20; i++){
        num[i] = (double)(rand()%20) * (double)(rand()%20)/(double)20;
    }
    
    for (i=0; i<20; i++){
        printf("%.2f\n",num[i]);
    }
    printf("sort result:\n");
    qsort(num,20,sizeof(num[0]),cmp);
    for (i=0; i<20; i++){
        printf("%.2f\n",num[i]);
    }    
    
    return 0;
}

5. Primary Sorting of Structures

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

struct NUMBER{
  int data;
  char *other;  
}num[20];

int cmp( const void *a , const void *b){
        return (*(struct NUMBER *)a).data > (*(struct NUMBER *)b).data ? 1:-1;
}

int main()
{
    int i = 0;
    
    /*Generate 0-20 random decimals*/
    for (i=0; i<10; i++){
        num[i].data = rand()%10;
    }
    
    for (i=0; i<10; i++){
        printf("%d\n",num[i].data);
    }
   
    printf("sort result:\n");
    qsort(num,10,sizeof(num[0]),cmp);
    for (i=0; i<10; i++){
        printf("%d\n",num[i].data);
    }    
 
    return 0;
}

6. Secondary Sorting of Structures

struct In{
int x;
int y;
}s[100];

//Sort x from smallest to largest, and y from largest to smallest when x is equal

int cmp( const void *a , const void *b){
struct In *c = (In *)a;
struct In *d = (In *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}
qsort(s,100,sizeof(s[0]),cmp);

7. Sort strings

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

struct NUMBER{
  char str[100];
  char *other;  
}num[5];

int cmp( const void *a , const void *b){
        return strcmp((*(struct NUMBER*)a).str,(*(struct NUMBER*)b).str);
}

int main()
{
    int i = 0;

    /*Generate 20 strings*/
    strcpy(num[0].str,"aaa");
    strcpy(num[1].str,"bbb");
    strcpy(num[2].str,"ccc");
    strcpy(num[3].str,"aaa");
    strcpy(num[4].str,"ccc");
    
    for (i=0; i<5; i++){
        printf("%s\n",num[i].str);
    }
   
    printf("sort result:\n");
    qsort(num,5,sizeof(num[0]),cmp);
    for (i=0; i<5; i++){
        printf("%s\n",num[i].str);
    }    
 
    return 0;
}
164 original articles were published. 6. Visits 7467
Private letter follow

Keywords: less

Added by shab620 on Mon, 27 Jan 2020 03:37:50 +0200