sort() Function in Cpp

 sort() Function in Cpp

INTRODUCTION

In this tutorial, we are going to learn about the sort() method in C++ and its applications.

The STL in C++ provides the sort() function which is used to sort the array or vector elements.

Generally, the sort() function in C++ takes two parameters, the starting index, and the ending index. Starting index resembles that from which index the sorting function should take place up to the ending index. There is also a third parameter in the sort() function which is optional and is used to sort the given array or vector elements in a lexicographical manner.

 sort() Function in Cpp

Let’s look at a sample program implementing the sort() function

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int arr[] = { 1, 4, 7, 3, 5, 6, 0, 8, 2, 9};
    int n = sizeof(arr) / sizeof(arr[0]);
    sort(arr, arr + n);
    cout << "array after sorting :\n";
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    return 0;
}

The output of the program is.

In the above program, we included < bits/stdc++.h > header file which contains all the standard libraries in C++. in some cases when time plays a vital role, the inclusion of this header file will help you to get an optimal solution in an efficient time. We initialized an array with some random elements and we calculated the size of the array by using the sizeof() function. After passing the sort function, the elements of the array get sorted. We can see the sorted elements in the output console.

See also  Module introduction & what we will learn throughout this module

There is another function that is used to place the greater values first, the greater() function is used to place the greatest element in the set of the array or vector elements. By using this greater() function, we can simply arrange the elements in descending order.

Let’s look at the implementation of the greater() function.

#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;
}

The output of the program is.

NOTE: we can use the reverse() function also in order to arrange the array elements in descending order.

The sort() function is only based on the starting index value and the ending index value of the array or vector. So we can partially sort the array or vector elements according to our wishes.

Let’s look at a sample program implementing partial sorting of the array or vector elements.

#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+3, arr + n);
    cout << "Array after sorting : \n";
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    return 0;
}

The output of the program is.

In the above program, we passed the index values from the third index to the last index. So the sort function doesn’t care about the remaining elements that are out of the index range and sorted only the elements bound inside the index.

See also  Features of C++ 11

That’s it from this tutorial. Hope you guys found it interesting. We have learned about the sort() function and its applications and in the next article, we will look at sorting the elements in a particular order. Happy Coding!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh