Sort an array of Roman Numerals in ascending order

Given an array arr[] of N Roman Numerals, the task is to sort these Roman Numerals in ascending order.

Examples:

Input: arr[] = { “MCMIV”, “MIV”, “MCM”, “MMIV” }
Output: MIV MCM MCMIV MMIV
Explanation:
The roman numerals in their corresponding decimal system are:
MIV: 1004
MCM: 1900
MCMIV: 1904
MMIV: 2004

Input: arr = { “MV”, “MIV”, “MCM”, “MM” }
Output: MIV MV MCM MM
Explanation:
The roman numerals in their corresponding decimal system are:
MIV 1004
MV 1005
MCM 1900
MM 2000

Approach: The idea to solve this problem is to store each element with its Roman to Integer value in a vector pair and then sort all the elements of the vector according to Roman to Integer value stored.



  • Iterate the given roman number.
  • Convert the roman number to the decimal and store them as a pair in a vector.
  • Sort the vector based on the decimal numbers stored in them.
  • Finally, print the roman numbers in the sorted order.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to sort an array of
// Roman Numerals in ascending order
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the value
// of a Roman symbol
int value(char r)
{
    // I in roman is equal to
    // 1 in decimal
    if (r == 'I')
        return 1;
  
    // V in roman is equal to
    // 5 in decimal
    if (r == 'V')
        return 5;
  
    // X in roman is equal to
    // 10 in decimal
    if (r == 'X')
        return 10;
  
    // L in roman is equal to
    // 50 in decimal
    if (r == 'L')
        return 50;
  
    // C in roman is equal to
    // 100 in decimal
    if (r == 'C')
        return 100;
  
    // D in roman is equal to
    // 500 in decimal
    if (r == 'D')
        return 500;
  
    // M in roman is equal to
    // 1000 in decimal
    if (r == 'M')
        return 1000;
  
    return -1;
}
  
// Function to return the decimal value
// of a roman numaral
int romanToDecimal(string& str)
{
    // Initialize result
    int res = 0;
  
    // Traverse given input
    for (int i = 0; i < str.length(); i++) {
  
        // Getting value of symbol s[i]
        int s1 = value(str[i]);
  
        if (i + 1 < str.length()) {
  
            // Getting value of symbol s[i+1]
            int s2 = value(str[i + 1]);
  
            // Comparing both values
            if (s1 >= s2) {
  
                // Value of current symbol
                // is >= the next symbol
                res = res + s1;
            }
            else {
  
                // Value of current symbol
                // is < the next symbol
                res = res + s2 - s1;
                i++;
            }
        }
        else {
            res = res + s1;
        }
    }
    return res;
}
  
// Function to sort the array according to
// the increasing order
void sortArr(string arr[], int n)
{
    // Vector to store the roman to integer
    // with respective elements
    vector<pair<int, string> > vp;
  
    // Inserting roman to integer
    // with respective elements in vector pair
    for (int i = 0; i < n; i++) {
        vp.push_back(make_pair(
            romanToDecimal(
                arr[i]),
            arr[i]));
    }
  
    // Sort the vector, this will sort the pair
    // according to the increasing order.
    sort(vp.begin(), vp.end());
  
    // Print the sorted vector content
    for (int i = 0; i < vp.size(); i++)
        cout << vp[i].second << " "
             << vp[i].first << "\n";
}
  
// Driver code
int main()
{
    string arr[] = { "MCMIV", "MIV",
                     "MCM", "MMIV" };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    sortArr(arr, n);
  
    return 0;
}

chevron_right


Output:

MIV 1004
MCM 1900
MCMIV 1904
MMIV 2004

Time Complexity: O(N * log(N)), where N is the number of elements in the array.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.