qsort() in C

qsort() is a pre-defined standard function in the C library. We can use this function to sort an array in ascending or descending order. It internally uses the quick sort algorithm, hence the name qsort. It can sort an array of any data type, including strings and structures. It works well and is efficient to implement. There is a function sort() in C++ similar to qsort() in C. In aspects like running time, safety, and flexibility, sort() outdoes qsort().

This tutorial explains the function qsort() with examples. C standard did not specify the complexity of the function, but as internally it follows the quick sort algorithm, its average time complexity is tentatively taken to be O(n*logn). The function is defined in the stdlib header file; hence we need to include it before using it.

Syntax of the function:

array: The array to be sorted.

number: Number of elements in the array that we want to sort

size: Size of an individual element of the array

function: Custom comparison function we need to write in a specified format:

Specified format of the function:

  • qsort() calls the compare() function for every two elements in the array.
  • Arguments a and b are two void pointers to point to the two elements to be compared.
  • we must write the body of compare() in the way that it should return:
    1. 0 if two elements are equal
    2. -1 or any other negative integer if the first element is less than the second element
    3. 1 or any other positive number if the first element is greater than the second.
  • The name of the comparing function can be anything, but the name must be exactly given as an argument to the qsort() function.
  • const void* a means a is a void pointer whose value is fixed. Before using, we need to typecast a void pointer to some data type.

Now, we will explore the functions for sorting arrays of different data types.

1. Sorting Integers:

Output:

Enter the size of the array to be sorted: 5
Enter elements into the array: 98 34 89 0 2
The sorted array:
[0, 2, 34, 89, 98]

Understanding:

In these two lines:

int a = *(int*) num1;

int b = *(int*) num2;

The input array is of type <int>. Hence, we must typecast the void pointers into integer pointers before performing any operations to allocate the required memory. We stored the values the two pointers are pointing at in two other integer variables, a and b. Then, we compared both values using the comparison operators.

Instead of using two more temporary variables, we can write a one-line code:

  • If a==b, 0 is returned; if a > b, a positive integer is returned; if a < b, a negative integer is returned.

2. Sorting strings

Output:

Enter the size of the array to be sorted: 5
Enter elements into the array: hi hello how are you
The sorted array:
[are, hello, hi, how, you]

Understanding:

  • We have an array of strings. The difference between an integer array and a string array is that:
    1. An integer array is a collection of integers
    2. A string array is a collection of character arrays/character pointers.
  • Hence, in the compare function, we need to typecast the void pointers to (char**)a and not (char*)a.
    [[string 1], [string 2]?]
    When we use char*, it points to the array, and then, to point to a string in the array, we need a double pointer.
  • We used the strcmp() function here. The function is defined in the string.h header file. We need to include it first.
  • The function returns:
    1. 0 if both strings are the same
    2. 1 if the ASCII value of a character in the string is greater than the corresponding character in the second string
    3. -1 if the ASCII value of a character in the string is less than the corresponding character in the second string.

3. Sorting an Array of Structure

Output:

Original array:
[[1, 7], [4, 0], [9, 4], [8, 8], [2, 4]]
Sorted array:
[[1, 7], [2, 4], [4, 0], [8, 8], [9, 4]]

Understanding:

We declared an array of type Structure, meaning every element in the array is an array of structure elements. In the above program, the structure has two integer elements. The task is to sort the array with respect to the first Structure element, and if any two first elements are equal, we need to sort it using the second element.

Example:

[[1, 2], [3, 4], [1, 4]]

Sorted array: [[1, 2], [1, 4], [3, 4]]

We used the rand() function to generate random elements in the array. In the compare() function, we need to typecast the two pointers to type structure.

qsort() in C

The specialty of using qsort() is the custom compare function that we can design the way we want. We can also sort a few elements in an array and leave the rest unsorted.


Next TopicQuiz game in C




Latest Courses