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 before the element pointed by p2 0 The element pointed by p1 is equivalent to the element pointed by p2 >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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- C qsort() vs C++ sort()
- Count number of pairs with the given Comparator
- Sort an array of pairs using Java Pair and Comparator
- Sort an Array of dates in ascending order using Custom Comparator
- Function Overloading vs Function Overriding in C++
- How to call function within function in C or C++
- Difference between Virtual function and Pure virtual function in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- log() function in C++
- exp() function C++
- arc function in C
- fma() function in C++
- div() function in C++
- Inline function in C
- regex_iterator() function in C++ STL
- valarray cos() function in C++
- valarray sin() function in C++
- valarray exp() function in C++
- valarray tan() function in C++
- valarray pow() function in C++