Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is qsort() in C?

Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

qsort() is a standard C library function that uses the QuickSort algorithm to sort arrays. It is a part of the stdlib.h header file in C. Below is the declaration of the qsort() function:

void qsort (void* base, size_t num, size_t size, int(* comparator)(const void*, const void*));

The qsort() function has no return value. It sorts the array pointed by base by reordering the elements of the array.

Parameters of the qsort() function

  • base: Pointer to the first element of the array
  • num: Number of elements in the array
  • size: Size (in bytes) of each element of the array
  • comparator: Function that compares two elements of the array

Comparator

The comparator function is declared as follows:

int comparator (const void * num1, const void * num2);

The comparator function returns an integer by comparing num1 and num2 as follows:

  • Greater than 0: if value pointed by num1 is higher than the value pointed by num2
  • Equal to 0: if value pointed by num1 is equivalent to the value pointed by num2
  • Less than 0: if value pointed by num1 is lower than the value pointed by num2

Code

Consider the code snippet below, which uses qsort() to sort an array:

#include <stdio.h>
#include <stdlib.h>
int comparator (const void * num1, const void * num2) {
return (*(int*)num1 - *(int*)num2);
}
void printArray(int arr[], int arrSize) {
for (int i=0; i<arrSize; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[] = {12, 56, 2, 9, 1};
int arrSize = 5;
printf("Array before sorting: \n");
printArray(arr, arrSize);
qsort(arr, arrSize, sizeof(int), comparator);
printf("\nArray after sorting: \n");
printArray(arr, arrSize);
return 0;
}

Explanation

  • Lines 4-6: The comparator function returns the difference of num1 and num2 passed to it as parameters. This return value determines the location of num1 with respect to num2 in the sorted array.
  • Lines 8-12: The printArray function takes an array and its size as parameters and prints the elements of the array.
  • Lines 22: qsort() is used to sort array arr that is created in line 16. qsort() sorts arr in ascending order using the comparator function declared in line 4.

RELATED TAGS

c

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring