Sorting algorithms are used a lot in computer science.
They help put things in the right order and solve many problems that come up when writing code.
Sorting algorithms make it easier to change the data, which makes our lives easier.
What is Sorting Array C++
Sorting in C++ is the process of putting the elements of an array in a way that makes sense.
This list can go from least to most or from most to least.
Sorting an unsorted array helps solve a lot of problems, like finding the least or most important element, etc.
Sorting things makes it easier to look for something specific in a group of things and to analyze them as a whole.
Categories of Sorting in C++
The Categories of Sorting in C++ are:
- Internal Sorting
- External Sorting
In internal sorting, the data to be sorted is already in the main memory or RAM, and the sorting process also happens in the main memory.
Bubble sort, insertion sort, and selection sort are all types of internal sorting.
When the internal sorting method is used, all of the data that needs to be sorted can be changed at once in the main memory.
In external sorting, the data is not always in the RAM because it is so big.
So the RAM or main memory gets filled up in small chunks. The Merge sort is an example of an external sort.
When all of the data that needs to be sorted can’t fit in memory at once and some of it needs to be kept in auxiliary memory like a hard disk, a floppy disk, a magnetic tape, etc., external sorting methods are used.
Types of Sorting Techniques in C++
std::sort() in C++ STL
C++ STL has a function called sort that works the same way. It sorts a vector or an array (items with random access).
It usually needs two parameters: the first is the point in the array or vector where the sorting should start, and the second is the length of the array or vector up to which it should be sorted.
The third parameter is optional and can be used, for example, if we want to put the elements in order based on how they are written.
How To Sort Array C++ in Ascending Order?
In this program, we will learn how to sort the numbers or elements of an integer array in C++ in ascending order.
This program will read the total number of elements (N) and make sure that the value of N is between 1 and N.
It will then read N integer values (as array elements) and print the input elements in order (After sorting elements in Ascending Order).
Example Program:
How to write a C++ program to sort an array in Ascending order.
// C++ program to demonstrate default behaviour of
// sort() in STL.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
/*Here we take two parameters, the beginning of the
array and the length n upto which we want the array to
be sorted*/
sort(arr, arr + n);
cout << "\nArray after sorting using "
"default sort is : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
Program Output:
Array after sorting using default sort is :
0 1 2 3 4 5 6 7 8 9
How To Sort Array C++ in Descending Order?
sort() has a third parameter that tells it how to arrange the items to be sorted.
We can pass the “greater()” function to sort in descending order.
This function compares two things in a way that puts the ones that are better first.
Example Program:
C++ program to demonstrate descending order sort using.
// C++ program to demonstrate descending order sort using
// greater<>().
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n, greater<int>());
cout << "Array after sorting : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
Explanation:
Only sort the array in the range given: To solve these kinds of problems, we only need to include the range in the sort function.
Program Output:
Array after sorting :
9 8 7 6 5 4 3 2 1 0
How To Sort in Particular Order?
We can also write our own comparator function and pass it as the third parameter.
This “comparator” function gives back a value that can be converted to bool.
This value basically tells us if the “first” argument should come before the “second” argument.
For example, in the code below, suppose that the “compareInterval” function is given the intervals “6,8” and “1,9” as arguments (comparator function).
Now that i1.first = 6 and i2.first = 1, our function returns “false,” which tells us that the “first” argument shouldn’t come before the “second” argument.
Instead, the “first” argument should come after the “second” argument, so “1,9” will be sorted before “6,8.”
Example Program:
// A C++ program to demonstrate
// STL sort() using
// our own comparator
#include <bits/stdc++.h>
using namespace std;
// An interval has a start
// time and end time
struct Interval {
int start, end;
};
// Compares two intervals
// according to starting times.
bool compareInterval(Interval i1, Interval i2)
{
return (i1.start < i2.start);
}
int main()
{
Interval arr[]
= { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } };
int n = sizeof(arr) / sizeof(arr[0]);
// sort the intervals in increasing order of
// start time
sort(arr, arr + n, compareInterval);
cout << "Intervals sorted by start time : \n";
for (int i = 0; i < n; i++)
cout << "[" << arr[i].start << "," << arr[i].end
<< "] ";
return 0;
}
Program Output:
Intervals sorted by start time :
[1,9] [2,4] [4,7] [6,8]
Conclusion
In Conclusion, We have successfully discussed the C++ Programming Tutorial about Sort Array and its example.
Also we learned how to sort an array in ascending and descending order, I hope this simple tutorial will help you a lot.