Standard C library provides qsort() that can be used for sorting an array. As the name suggests, the function uses QuickSort algorithm to sort the given array. Following is prototype of qsort()
The key point about qsort() is comparator function comparator. The comparator function takes two arguments and contains logic to decide their relative order in sorted output. The idea is to provide flexibility so that qsort() can be used for any type (including user defined types) and can be used to obtain any desired order (increasing, decreasing or any other).
The comparator function takes two pointers as arguments (both type-casted to const void*) and defines the order of the elements by returning (in a stable and transitive manner
int comparator(const void* p1, const void* p2); Return value meaning 0 The element pointed by p1 goes after the element pointed by p2 Source: http://www.cplusplus.com/reference/cstdlib/qsort/
For example, let there be an array of students where following is type of student.
Lets say we need to sort the students based on marks in ascending order. The comparator function will look like:
See following posts for more sample uses of qsort().
Given a sequence of words, print all anagrams together
Box Stacking Problem
Closest Pair of Points
Following is an interesting problem that can be easily solved with the help of qsort() and comparator function.
Given an array of integers, sort it in such a way that the odd numbers appear first and the even numbers appear later. The odd numbers should be sorted in descending order and the even numbers should be sorted in ascending order.
The simple approach is to first modify the input array such that the even and odd numbers are segregated followed by applying some sorting algorithm on both parts(odd and even) separately.
However, there exists an interesting approach with a little modification in comparator function of Quick Sort. The idea is to write a comparator function that takes two addresses p and q as arguments. Let l and r be the number pointed by p and q. The function uses following logic:
1) If both (l and r) are odd, put the greater of two first.
2) If both (l and r) are even, put the smaller of two first.
3) If one of them is even and other is odd, put the odd number first.
Following is C implementation of the above approach.
Output array is 9 7 5 3 1 2 4 6 8
Given an array of integers, sort it in alternate fashion. Alternate fashion means that the elements at even indices are sorted separately and elements at odd indices are sorted separately.
This article is compiled by Aashish Barnwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C qsort() vs C++ sort()
- Sort an array of pairs using Java Pair and Comparator
- Count number of pairs with the given Comparator
- Sort an Array of dates in ascending order using Custom Comparator
- Comparator Class in C++ with Examples
- What happens when a virtual function is called inside a non-virtual function in C++
- Function Overloading vs Function Overriding in C++
- Difference between Virtual function and Pure virtual function in C++
- How to call function within function in C or C++
- Difference between virtual function and inline function in C++
- Write a one line C function to round floating point numbers
- Does C support function overloading?
- How can I return multiple values from a function?
- What is the purpose of a function prototype?
- How to declare a pointer to a function?
- C function to Swap strings
- Can we call an undeclared function in C++?
- What is evaluation order of function parameters in C?
- Can we use function on left side of an expression in C and C++?
- Declare a C/C++ function returning pointer to array of integer pointers