Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Inbuilt Sort #333

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Sorting/Inbuilt Sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#INBUILT SORT IN C++ (ESSENTIAL FOR COMPETITIVE PROGRAMMING)

THEORY

SYNTAX
#include<algorithm>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

first: An random access iterator pointing to the first element in the range to be sorted.

last: An random access iterator pointing to the past last element in the range to be sorted.

comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements.

COMPARATOR FUNCTION
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 sort() can be used for any type (including user defined types) and can be used to obtain any desired order (increasing, decreasing or any other).

Lets say we need to sort the students based on marks in ascending order. The comparator function will look like:
int comparator(const void *p, const void *q)
{
int l = ((struct Student *)p)->marks;
int r = ((struct Student *)q)->marks;
return (l - r);
}



EXAMPLE
vector<int> myvector (myints, myints+8);
sort (myvector.begin(), myvector.begin()+4);

TIME COMPLEXITY
O(nlogn)