C library - qsort() function



The C stdlib library qsort() function is a sorting function, which is used to sort an array either in ascending or descending order. It is known as quick sort.

This function is based on the quick sort algorithm which is one of the fastest and most efficient sorting algorithm.

Syntax

Following is the C library syntax of the qsort() function −

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

Parameters

This function accepts following parameters −

  • base − It represents pointer to the first element of the array to be sorted.

  • nitems − It represents number of element in the array.

  • size − It represents size of each element in the array.

  • compare − It represent a function pointer to a comparison function that compares two elements.

Return Value

This function does not returns any values.

Example 1

In this example, we create a basic c program to demonstrate the use of qsort() function.

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

// Comparison function
int compare(const void* a, const void* b) {
   return (*(int*)a - *(int*)b);
}

int main() {
   int arr[] = {10, 5, 4, 6, 9};
   int n = sizeof(arr) / sizeof(arr[0]);

   qsort(arr, n, sizeof(int), compare);

   printf("Following is the sorted array: ");
   
   int i;
   for (i = 0; i < n; ++i) {
      printf("%d ", arr[i]);
   }
   printf("\n");
   return 0;
}

Following is the output −

Following is the sorted array: 4 5 6 9 10

Example 2

The below c program sorts the character of an array. Using the qsort() function.

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

// Comparison function
int compare(const void *arg1, const void *arg2){
   return strcasecmp(*(const char **)arg1, *(const char **)arg2);
}

int main(void){
   char *colors[] = {"B", "D", "A", "W", "Z", "X", "M", "O"};
   int i;
   // Size of the array
   int size = sizeof(colors) / sizeof(colors[0]);

   printf("Original array elements:\n");
   for (i = 0; i < size; i++)
   {
      printf("%s ", colors[i]);
   }

   printf("\n\n");

   // Use qsort to sort
   qsort(colors, size, sizeof(char *), compare);
   printf("Following is the sorted array: ");

   for (i = 0; i < size; ++i)
   {
      printf("%s ", colors[i]);
   }
   return 0;
}

Output

Following is the output −

Original array elements:
B D A W Z X M O

Following is the sorted array: A B D M O W X Z

Example 3

Let's create another c program to sort the string of an array. Using the qsort() function.

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

// Comparison function
int compare(const void *arg1, const void *arg2){
   return strcasecmp(*(const char **)arg1, *(const char **)arg2);
}

int main(void){
   char *colors[] = {"Vivek", "Aman", "Shriansh", "Tapas"};
   int i;
   // Size of the array
   int size = sizeof(colors) / sizeof(colors[0]);

   printf("Original array elements:\n");
   for (i = 0; i < size; i++)
   {
      printf("%s ", colors[i]);
   }

   printf("\n");

   // Use qsort to sort
   qsort(colors, size, sizeof(char *), compare);
   printf("Following is the sorted array: ");

   for (i = 0; i < size; ++i)
   {
      printf("%s ", colors[i]);
   }
   return 0;
}

Output

Following is the output −

Original array elements:
Vivek Aman Shriansh Tapas
Following is the sorted array: Aman Shriansh Tapas Vivek
Advertisements