Stable sort for descending order


In this article, we will discuss what is meant by stable sorting and how can we sort an array in descending order keeping in mind that the sorting algorithm should be stable.

Let us first discuss about what are the features of a stable sort algorithm −

A sorting algorithm is called stable if it keeps the original order of items with the same value in the input data when they are sorted. So, if there are two or more items with the same value, a stable sorting algorithm will not change their relative positions in the sorted output.

Stable sorting algorithms are helpful when dealing with complex data structures such as records, where maintaining the original order of items with the same value is crucial. This is because a stable sort algorithm ensures that items with equal values keep their original order in the sorted output. Some examples of stable sorting algorithms that can be used in such scenarios include Merge Sort, Insertion Sort, and Bubble Sort.

Problem Statement

An array of n elements is assigned to us and we have to sort the given array in descending order in such a way that the original order of items with the same value in the input data is maintained when they are sorted.

Input: Nums [] = { 5, 3, 1, 3, 2 }

If we sort the given array nums using stable sort algorithm in descending order, the array so obtained is −

Result: { 5, 3, 3, 2, 1 }

One of the method to solve this could be by implementing bubble sort for descending order.

Example

The code for such approach is given below −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void sort(vector<int>&nums, int size){
   for (int iterator = size ; iterator >= 0; iterator--)
      for (int it = size ; it > size - iterator ; it--)
         if (nums[it] > nums[it - 1])
            swap(nums[it] , nums[it-1]);
}
int main(){
   vector<int> numbers = { 5, 3, 1, 3, 2  };
   sort(numbers, 5);
   cout<<" The given array after sorting in descending order using Stable sort method is: "<< endl;
   for (int iterator = 0; iterator < numbers.size(); ++iterator) {
      cout<<  numbers[iterator] << " ";
   }
   return 0;
}

Output

The given array after sorting in descending order using Stable sort method is: 
5 3 3 2 1

Example

Implementing Using Merge Sort

As we know, stable sorting can also be done using merge sort method, another implementation using merge sort is given below −

The code goes here −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

// this function is used to sort the array
void merge( vector<int>& nums, int left, int midelement, int temp) {
   vector<int> temparr(temp - left + 1);
   int iterator = left, j = midelement + 1, k = 0;
    
   while (iterator <= midelement && j <= temp) {
      if (nums[iterator] >= nums[j]) {
         temparr[k++] = nums[iterator++];
      } else {
         temparr[k++] = nums[j++];
      }
   }    
   while (iterator <= midelement){
      temparr[k++] = nums[iterator++];
   }    
   while (j <= temp){
      temparr[k++] = nums[j++];
   }    
   for (int p = 0; p < k; ++p) {
      nums[left + p] = temparr[p];
   }
}
void m_sort_helper( vector<int>& nums, int l, int t ) {
   if (l < t) {
      int midelement = (l + t) / 2;
      m_sort_helper ( nums, l, midelement );
      m_sort_helper( nums, midelement + 1, t );
      merge( nums, l, midelement, t );
   }
}
void merge_sort( vector<int>& nums ){
   m_sort_helper(nums, 0, nums.size() - 1);
}
int main() {
   vector<int> nums = {5, 3, 1, 3, 2};
   merge_sort( nums );
   cout<<" The given array after sorting in descending order using Stable sort method is: "<< endl;
   for (int iterator = 0; iterator < nums.size(); ++iterator ) {
      cout << nums[ iterator ] << " ";
   }
   return 0;
}

Output

The given array after sorting in descending order using Stable sort method is: 
5 3 3 2 1

Example

Using inbuilt library functions

Stable sort can also be performed using inbuilt library functions.

std::stable_sort is a function in C++ that can be used to sort elements in a container such as a vector or an array. The function sorts the elements in a stable manner, which means that equal elements retain their relative order after the sorting process is complete.

The function takes three arguments − the beginning and end iterators of the container to be sorted, and a comparison function that defines the sorting order. You can use it to sort elements in ascending or descending order.

The code for such approach is given below −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
   std::vector<int> nums = {5, 3, 1, 3, 2};
   std::stable_sort(nums.begin(), nums.end(), std::greater<int>());
   cout<<" The given array after sorting in descending order using Stable sort method is: "<< endl;
   for (int num : nums) {
      std::cout << num << " ";
   }    
   return 0;
}

Output

The given array after sorting in descending order using Stable sort method is: 
5 3 3 2 1

Conclusion

In this article, we discussed different methods in which can perform stable sort operation in descending order also, we got to know about stable sort and why it is important to sort an array in stable order.

Updated on: 05-Oct-2023

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements