In this HackerRank Big Sorting problem, Consider an array of numeric strings where each string is a positive number with anywhere from 1 to (10)power 6 digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array.

HackerRank Big Sorting problem solution


Problem solution in Python programming.

#!/bin/python3

import sys


n = int(input().strip())
unsorted = []
unsorted_i = 0
for unsorted_i in range(n):
    unsorted_t = str(input().strip())
    unsorted.append(unsorted_t)
for v in sorted(unsorted, key = lambda v: int(v)):
    print(v)


Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] unsorted = new String[n];
        for(int unsorted_i=0; unsorted_i < n; unsorted_i++){
            unsorted[unsorted_i] = in.next();
        }
        Map<Integer, ArrayList<String>> map = new TreeMap<>();
        for(int i = 0; i < n; i++) {
            ArrayList<String> mapValue = map.get(unsorted[i].length());
            if(mapValue == null) {
                mapValue = new ArrayList<>();
                mapValue.add(unsorted[i]);
                map.put(unsorted[i].length(), mapValue);
            } else {
                mapValue.add(unsorted[i]);
            }
        }
        
        for(ArrayList<String> list : map.values()) {
            Collections.sort(list);
        }
        
        for(Integer i : map.keySet()) {
            System.out.println(Arrays.toString(map.get(i).toArray(new String[0])).replaceAll("[\\[\\],]", "").replaceAll(" ", "\n"));
        }
    }
}


Problem solution in C++ programming.

#include <bits/stdc++.h>

using namespace std;

int main(){
    int n;
    cin >> n;
    vector<string> unsorted(n);
    for(int unsorted_i = 0; unsorted_i < n; unsorted_i++){
       cin >> unsorted[unsorted_i];
    }

    sort(unsorted.begin(), unsorted.end(), [](const string& a, const string& b) {
        if (a.length() != b.length()) {
            return a.length() < b.length();
        }
        return a < b;
    });
    
    for (auto x:unsorted) cout << x << endl;
    
    return 0;
}


Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

static int compare (void const *a, void const *b)
{
    char const *const *pa = a;
    char const *const *pb = b;
    if(strlen(*pa) == strlen(*pb))
        return strcmp(*pa, *pb);
    return strlen(*pa) - strlen(*pb);
}


int main(){
    int nbr;
    scanf("%d", &nbr);
    char **tab = malloc(sizeof(char*) * nbr);
    char *str = (char*)malloc(sizeof(char) * 1000000);
    for(int i = 0; i < nbr; i++)
    {
        scanf("%s", str);
        tab[i] = malloc(sizeof(char) * (strlen(str) + 1));
        sprintf(tab[i],"%s",str);
    }
    qsort(tab, nbr, sizeof(char*), compare);
    for(int i = 0; i < nbr; i++)
        printf("%s\n", tab[i]);
    return 0;
}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

var BigNumber = require('bignumber.js');

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    var unsorted = [];
    for(var unsorted_i = 0; unsorted_i < n; unsorted_i++){
       var big = new BigNumber(readLine());
       unsorted[unsorted_i] = big;
    }
    
    unsorted = unsorted.sort(function (a,b){
       return a.minus(b); 
    });

    for(let i = 0; i < unsorted.length; i++){
        console.log(unsorted[i].toFixed());
    }
}