Horje
qsort() Function in C

The qsort() function in C is a predefined function in the stdlib.h library that is used to sort an array of items in ascending order or descending order. It stands for “quick sort,” as it implements the quicksort algorithm for sorting which is one of the fastest and most efficient algorithm to sort elements of an array.

Syntax of qsort() in C

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

Parameters of qsort() 

The qsort() function takes four parameters:

  • *base: Pointer to the first element of the array.
  • num: Number of elements in the array.
  • size: Size of each element in an array.
  • *compare: Function pointer to a comparison function that compares two elements.

Return Value of qsort() 

 The qsort() function does not return any value as it sorts the array in place by modifying the original array only.

qsort() Comparator Function

The most important part of the qsort() is the comparator function that is passed as a parameter to the qsort() function. The comparator function takes two pointers as arguments that are type casted to const void* and it contains the logic to compare two elements to get the desired order i.e the order in which we want to sort the elements (ascending, descending or any other).

Declaration of Comparator Function

returnType comparatorName(const void* ptr1, const void* ptr2);

Here, ptr1 and ptr2 are the pointer to the elements to be compared.

The comparator function should have the same signature as above and should return the following values:

  • Less than zero (<0): If the element pointed by ptr1 should be positioned before the element pointed by p2.
  • Zero (0): If the element pointed by p1 is equal to the element pointed by p2.
  • Greater than zero (>0): If the element pointed by p1 should be positioned after the element pointed by p2.

Note: We can use qsort() to sort arrays of any data type, integers, strings and complex structures, by customizing the comparison function accordingly.

Example of qsort() in C

Input: 
arr = [4, 2, 5, 3, 1]

Output:
Sorted Array: 1 2 3 4 5

The below program demonstrates how we can sort a given array using qsort() function in C.

C++
// C Program to sort an array using qsort() function in C

#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[] = { 4, 2, 5, 3, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);

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

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

    return 0;
}

Output
Sorted array: 
1 2 3 4 5 

Time Complexity: O(n log n), where n is the number of elements in the array.
Auxilliary Space: O(log n), due to the stack space used by the recursive calls.




Reffered: https://www.geeksforgeeks.org


C Language

Related
Zig - A Brief Introduction Zig - A Brief Introduction
Array of Pointers to Strings in C Array of Pointers to Strings in C
When to Use Enum Instead of Define in C? When to Use Enum Instead of Define in C?
How to Write a Struct to a Binary File in C? How to Write a Struct to a Binary File in C?
How to Create a Circular Linked List in C? How to Create a Circular Linked List in C?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14