Sort an array of large numbers
Last Updated :
29 Mar, 2022
Given an array of numbers where every number is represented as string. The numbers may be very large (may not fit in long long int), the task is to sort these numbers.
Examples:
Input : arr[] = {"5", "1237637463746732323", "12" };
Output : arr[] = {"5", "12", "1237637463746732323"};
Input : arr[] = {"50", "12", "12", "1"};
Output : arr[] = {"1", "12", "12", "50"};
Below is the implementation of the above idea.
C++
// C++ program to sort large numbers represented
// as strings.
#include<bits/stdc++.h>
using namespace std;
// Returns true if str1 is smaller than str2.
bool compareNumbers(string str1, string str2)
{
// Calculate lengths of both string
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
// If lengths are same
for (int i=0; i<n1; i++)
{
if (str1[i] < str2[i])
return true;
if (str1[i] > str2[i])
return false;
}
return false;
}
// Function for sort an array of large numbers
// represented as strings
void sortLargeNumbers(string arr[], int n)
{
sort(arr, arr+n, compareNumbers);
}
// Driver code
int main()
{
string arr[] = {"5", "1237637463746732323",
"97987", "12" };
int n = sizeof(arr)/sizeof(arr[0]);
sortLargeNumbers(arr, n);
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to sort large numbers represented
// as strings.
import java.io.*;
import java.util.*;
class main
{
// Function for sort an array of large numbers
// represented as strings
static void sortLargeNumbers(String arr[])
{
// Refer below post for understanding below expression:
// https://www.geeksforgeeks.org/lambda-expressions-java-8/
Arrays.sort(arr, (left, right) ->
{
/* If length of left != right, then return
the diff of the length else use compareTo
function to compare values.*/
if (left.length() != right.length())
return left.length() - right.length();
return left.compareTo(right);
});
}
// Driver code
public static void main(String args[])
{
String arr[] = {"5", "1237637463746732323",
"97987", "12" };
sortLargeNumbers(arr);
for (String s : arr)
System.out.print(s + " ");
}
}
Python3
# Python3 program to sort large numbers
# represented as strings
# Function for sort an array of large
# numbers represented as strings
def sortLargeNumbers (arr, n):
arr.sort(key = int)
# Driver Code
if __name__ == '__main__':
arr = [ "5", "1237637463746732323",
"97987", "12" ]
n = len(arr)
sortLargeNumbers(arr, n)
for i in arr:
print(i, end = ' ')
# This code is contributed by himanshu77
C#
// C# program to sort large numbers
// represented as strings.
using System;
class GFG
{
// Function for sort an array of large
// numbers represented as strings
static void sortLargeNumbers(String []arr)
{
// Refer below post for understanding
// below expression:
// https://www.geeksforgeeks.org/lambda-expressions-java-8/
for(int i = 0; i < arr.Length - 1; i++)
{
/* If length of left != right, then
return the diff of the length else
use compareTo function to compare values.*/
String left = arr[i], right = arr[i + 1];
if (left.Length > right.Length)
{
arr[i] = right;
arr[i + 1] = left;
i -= 2;
}
}
}
// Driver code
public static void Main()
{
String []arr = {"5", "1237637463746732323",
"97987", "12" };
sortLargeNumbers(arr);
foreach (String s in arr)
Console.Write(s + " ");
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// JavaScript program to sort large numbers
// represented as strings.
// Function for sort an array of large numbers
// represented as strings
function sortLargeNumbers(arr)
{
// Refer below post for understanding
// below expression:
// https://www.geeksforgeeks.org/lambda-expressions-java-8/
for(let i = 0; i < arr.length - 1; i++)
{
/* If length of left != right, then
return the diff of the length else
use compareTo function to compare values.*/
let left = arr[i], right = arr[i + 1];
if (left.length > right.length)
{
arr[i] = right;
arr[i + 1] = left;
i -= 2;
}
}
}
// Driver Code
let arr = ["5", "1237637463746732323",
"97987", "12" ];
sortLargeNumbers(arr);
for (let s in arr)
document.write(arr[s] + " ");
</script>
Output:
5 12 97987 1237637463746732323
Time complexity: O(k * n Log n), Here assumption is that the sort() function uses a O(n Log n) sorting algorithm.
Auxiliary Space: O(1)
Similar Post:
Sorting Big Integers
Similar Reads
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:
12 min read
Sort an array in wave form Given an sorted array arr[] of integers, rearrange the elements into a wave-like array. An array arr[0..n-1] is said to be in wave form if it follows the pattern: \scriptsize arr[0] \geq arr[1] \leq arr[2] \geq arr[3] \leq arr[4] \geq \dots and so on and find the lexicographically smallest one.Note:
4 min read
Sort a nearly sorted (or K sorted) array Given an array arr[] and a number k . The array is sorted in a way that every element is at max k distance away from it sorted position. It means if we completely sort the array, then the index of the element can go from i - k to i + k where i is index in the given array. Our task is to completely s
6 min read
Array Sorting - Practice Problems Sorting an array means arranging the elements of the array in a certain order. Generally sorting in an array is done to arrange the elements in increasing or decreasing order. Problem statement: Given an array of integers arr, the task is to sort the array in ascending order and return it, without u
9 min read
Sort the given Array after sorting each number individually Given an array arr of size N, the task is to sort the digits of each element in the array and then sort the array in non-decreasing order. Print the array after sorting. Examples: Input: arr[] = {514, 34, 41, 39}Output: 41 43 93 541Explanation: Sorting each element of the array: arr[] = {145, 34, 14
6 min read