C++ Program to Sort the Elements of an Array in Descending Order


Arranging data items in a proper form is an essential task while solving some problems in an efficient way. The element sorting problem is one of the most commonly discussed arranging problem. In this article we will see how to arrange the array elements in descending order (decreasing order of their values) in C++.

There are many different sorting algorithms present in this domain to sort numeric or nonnumeric elements in a given order. In this article we will see only two simple methods of sorting. The bubble sort and the selection sort. Let us see them one by one with proper algorithms and C++ implementation code.

Sort array in descending order using bubble sorting technique

The bubble sorting technique is one of the most common and easier method for sorting elements in an array. This method checks two consecutive elements, if they are in correct order, then skip to the next elements, otherwise interchange them to place them in correct order. Then move towards right and do the same for the other pair of values. The bubble sorting technique has few phases, at the end of each phase, one element is being placed at the correct intended position. Let us see the algorithm for bubble sorting technique.

Algorithm

  • read array A and its size n as input
  • for i ranging from 0 to n-1, do
    • for j ranging from 0 to n - 2, do
      • if A[j] < A[j + 1], then
        • swap A[j] and A[j + 1]
      • end if
    • end for
  • end for

Example

#include <iostream>
using namespace std;
void display( int arr[], int n ){
   for ( int i = 0; i < n; i++ ) {
      cout << arr[i] << ", ";
   }
}
void swap ( int &a, int &b ){
   int temp = a;
   a = b;
   b = temp;
}
void solve( int arr[], int n ){
   int i, j;
   for ( i = 0; i < n; i++ ) {
      for ( j = 0; j < n-1; j++ ) {
         if ( arr[j] < arr[ j+1 ] ) {
            swap( arr[j], arr[ j + 1 ] );
         }
      }
   }
}
int main(){
   int arr[] = {8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84};
   int n = sizeof( arr ) / sizeof( arr[0] );
   cout << "Array before sorting: ";
   display(arr, n);
   solve( arr, n );
   cout << "\nArray After sorting: ";
   display(arr, n);
}

Output

Array before sorting: 8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84, 
Array After sorting: 96, 95, 89, 84, 78, 74, 63, 58, 45, 44, 36, 25, 12, 12, 10, 8, 5, 2, 

Sort array in descending order using selection sorting technique

In the selection sorting technique, we find either minimum element or the maximum element from the given array starting from index i to the end of this array. Assume we are finding maximum element. In each phase, it finds the minimum from index i to end, then place the element at its desired position then again search for next maximum element from the index i + 1 and so on. After completing these phases, the entire array will be sorted accordingly.

Algorithm

  • read array A and its size n as input
  • for i ranging from 0 to n-1, do
    • ind := index of maximum element of A starting from i to n
    • if A[ i ] < A[ ind ], then
      • swap A[ i ] and A[ ind ]
    • end if
  • end for

Example

#include <iostream>
using namespace std;
void display( int arr[], int n ){
   for ( int i = 0; i < n; i++ ) {
      cout << arr[i] << ", ";
   }
}
void swap ( int &a, int &b ){
   int temp = a;
   a = b;
   b = temp;
}
int max_index( int arr[], int n, int s, int e ){
   int max = 0, max_ind = 0;
   for ( int i = s; i < e; i++ ) {
      if ( arr[i] > max ) {
         max = arr[i];
         max_ind = i;
      }
   }
   return max_ind;
}
void solve( int arr[], int n ){
   int i, j, ind;
   for ( i = 0; i < n; i++ ) {
      ind = max_index( arr, n, i, n );
      if ( arr[i] < arr[ ind ] ) {
         swap( arr[i], arr[ ind ] );
      }
   }
}
int main(){
   int arr[] = {8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12,89, 95, 63, 84};
   int n = sizeof( arr ) / sizeof( arr[0] );
   cout << "Array before sorting: ";
   display(arr, n);
   solve( arr, n );
   cout << "\nArray After sorting: ";
   display(arr, n);
}

Output

Array before sorting: 8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84, 
Array After sorting: 96, 95, 89, 84, 78, 74, 63, 58, 45, 44, 36, 25, 12, 12, 10, 8, 5, 2,

Conclusion

Sorting problem is a fundamental problem where we arrange the numbers or other values in a given arrangement logics. There are many different sorting techniques available in this domain, however, in this article we have seen two sorting techniques which are easy to implement and easy to understand. These two methods are bubble sort technique and the selection sorting technique. Using these two methods, we have sorted the set of data in descending (non-increasing) order. These two sorting methods are not much efficient in respect of time, but they are simple to understand. Both of these two methods take O(n2) amount of time, where n is the size of input. The bubble sort can be made faster by a simple checking whether when there is no swap in any phase, the next consecutive phase will not change anything.

Updated on: 14-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements