Language Construct - Struct-Comparators
Language Construct - Struct-Comparators
PREREQUISITES:
1) Language Constructs - struct
2) Sorting
3) STL - sort()
4) User Defined Functions
MOTIVATIONAL PROBLEM:
Let us take the same problem as we took while learning Language Construct- structs.
We have a school of N students. And we have to accept the following details for each student:
Name of the student, Roll number of the student, class of the student, Marks in Physics ,
Chemistry and Computer Science. We are also asked to calculate and store the average marks
for each student.
I hope you remember how we defined a ‘student’ data structure(using struct command) and we
had an array of N elements where each element of the array was a ‘student’ data structure we
defined.
If you do not know how to organise data this way,
I would highly recommend you to go through Language Construct - structs tutorial.
WHAT’S A COMPARATOR?
You can think of a comparator as a user defined function with a bool return type. It takes as
arguments, two elements. These elements can be anything. It can be integers, strings or even
our own data structure student.
Inside this function we specify how do we want the elements to be sorted.
The following code is for the comparator(comp) which will help us sort the array A which
contains the details of N students according to the Physics marks:
****Note that if we want to sort an array which has ‘student’ data structure as its elements using
a comparator, we must pass two ‘student’ data structures as the two arguments of the
comparator function(comp()). If instead, we want to sort an array which has int as its elements,
using a comparator, the two arguments of the comparator function must be ints.
Soln.:
bool comp(student X, student Y)
{
return X.ch>Y.ch;
}
Notice the > sign, if we want to sort in decreasing order of some details.
GOLDEN RULE!
We can make it a formula that if we are asked to sort according to increasing order use the <
sign.
If we are asked to sort according to decreasing order we use the > sign.
Make sure you do not change the order of the first and second parameters.
In that case the symbols(>,<) would be reversed!
EXERCISE:
1. Now let’s do a quick exercise:
Can you write a comparator that sorts an int array in descending order of values. Call the
sort() from main() with appropriate syntax.
Link: https://ideone.com/Kb5ms6
2. Now can you write only the comparator to sort the student array in descending order of
average marks and break ties according to decreasing order of Physics marks. In other
words a student with higher average is placed before a student with lower average. If
two students have the same average the one who secured higher Physics marks will be
placed above the one with lower Physics marks. You may assume that the student data
structure has the following variables:
name
roll
standard
ph
ch
comp
avg
Write only the comparator.
Soln.: https://ideone.com/cVT9Cx
This type of problem is very common in Competitive Programming world. Practice this a
couple of times before you get comfortable with it.
CONCLUSION
Now you completely understand how to sort data structures according to some feature using the
STL sort(). You also know how to create your own data structure. Congratulations!