C++ Program for sorting Digits of a given Number

Prince KumarPrince Kumar
3 min read

In this article we are going to learn about that how can we sort the digits of a number in increasing order or decreasing order when we are given a Number 'N'.

Let's understand with the help of an example given below: -

Let you be given a number N = 983712

Example 1: Ascending Order(Increasing Order)

Result = 123789

Example 2: Descending Order(Decreasing Order)

Result = 987321

Algorithm to solve the Problem

Step 1: First you will extract all the digits of a number.

Step 2: Store all the extracted digits into a vector.

Step 3: And then use the sort() function to sort the digits of a number.

Step 4: After that join all digits from index 0 to (count of digit - 1) from the vector to which you had applied the sort() function.

Step 5: Finally Print the sorted number that you would have obtained from step 4.

Let's see the implementation for the above approach: -

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

int sortingDigits(int n){
    // array of the digits of a number
    vector<int> digits;
    int count = 0;
    while(n != 0){
        digits.push_back(n%10);
        n = n/10;      
        count++;  
    }
    // Sorting the digits of number by using the sort() function
    sort(digits.begin(), digits.end());
    int sortedNumber = 0;
    // Here we are obtaining the number from the sorted digits 
    for(int i = 0; i < digits.size(); sortedNumber += (digits[i]*pow(10, count-1)), count--, i++);
    return sortedNumber;
}

void printSortedNumber(int number){
    cout << "Sorted Number in Ascending Order: ";
    cout << number;
    cout << endl;
}

signed main(){
    int number1 = 312;
    int sortedNumber1 = sortingDigits(number1);
    printSortedNumber(sortedNumber1);

    int number2 = 9782647;
    int sortedNumber2 = sortingDigits(number2);
    printSortedNumber(sortedNumber2);

    int number3 = 22221211;
    int sortedNumber3 = sortingDigits(number3);
    printSortedNumber(sortedNumber3);

    return 0;
}

Output:

Sorted Number in Ascending Order: 123

Sorted Number in Ascending Order: 2467789

Sorted Number in Ascending Order: 11122222

Time Complexity: O(nlogn)

Auxiliary Space: O(nlogn)

Let's see the implementation for Descending Order(Decreasing Order)

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

int sortingDigits(int n){
    // array of the digits of a number
    vector<int> digits;
    int count = 0;
    while(n != 0){
        digits.push_back(n%10);
        n = n/10;      
        count++;  
    }
    // Sorting the digits of number by using the sort() function
    sort(digits.begin(), digits.end(), greater<int>());
    int sortedNumber = 0;
    // Here we are obtaining the number from the sorted digits 
    for(int i = 0; i < digits.size(); sortedNumber += (digits[i]*pow(10, count-1)), count--, i++);
    return sortedNumber;
}

void printSortedNumber(int number){
    cout << "Sorted Number in Descending Order: ";
    cout << number;
    cout << endl;
}

signed main(){
    int number1 = 312;
    int sortedNumber1 = sortingDigits(number1);
    printSortedNumber(sortedNumber1);

    int number2 = 9782647;
    int sortedNumber2 = sortingDigits(number2);
    printSortedNumber(sortedNumber2);

    int number3 = 22221211;
    int sortedNumber3 = sortingDigits(number3);
    printSortedNumber(sortedNumber3);

    return 0;
}

Output:

Sorted Number in Descending Order: 321

Sorted Number in Descending Order: 9877642

Sorted Number in Descending Order: 22222111

Time Complexity: O(nlogn)

Auxiliary Space: O(nlogn)

0
Subscribe to my newsletter

Read articles from Prince Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prince Kumar
Prince Kumar