Sort the given Array after sorting each number individually
Last Updated :
05 Sep, 2022
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 541
Explanation:
Sorting each element of the array: arr[] = {145, 34, 14, 39}
Sorting the array: arr[] = {14, 34, 39, 145}
Input: arr[] = {3, 1, 9, 4}
Output: 1 3 4 9
Approach: Follow the steps below to solve this problem:
- Create a function toString, which will accept the integer N as an parameter and will return N the form of string.
- Pass each element of array arr in function toString to convert it to string. Sort that string and convert it back to an integer. After this, replace each element with the converted integer in array arr.
- Sort the array arr and print the answer according to the above observation.
Below is the implementation of the above approach.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print array in non-decreasing order
void printArray(int arr[], int N)
{
for (int i = 0; i < N; i++)
cout << arr[i] << " ";
}
// Function to convert an integer to string
string toString(int N)
{
string s;
while (N > 0) {
s += (N % 10) + '0';
N /= 10;
}
if (s.size() == 0) {
s = "0";
}
return s;
}
// Function to sort each element of the array
// in non-descreasing order of its digits
void digitSort(int arr[], int N)
{
// Traversing each element to sort
for (int i = 0; i < N; i++) {
// Converting number to string
string s = toString(arr[i]);
// Sorting string
sort(s.begin(), s.end());
// Converting string to integer
arr[i] = stoi(s);
}
// Sorting array
sort(arr, arr + N);
// Printing array
printArray(arr, N);
}
// Driver Code
int main()
{
int arr[] = { 514, 34, 41, 39 };
int N = sizeof(arr) / sizeof(arr[0]);
digitSort(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print array in non-decreasing order
static void printArray(int arr[], int N)
{
for (int i = 0; i < N; i++)
System.out.print(arr[i] + " ");
}
// Function to convert an integer to String
static String toString(int N)
{
String s = "";
while (N > 0) {
s += String.valueOf((N % 10)) + '0';
N /= 10;
}
if (s.length() == 0) {
s = "0";
}
return s;
}
// Function to sort each element of the array
// in non-descreasing order of its digits
static void digitSort(int arr[], int N)
{
// Traversing each element to sort
for (int i = 0; i < N; i++) {
// Converting number to String
String s = toString(arr[i]);
// Sorting String
s = sort(s);
// Converting String to integer
arr[i] = Integer.valueOf(s);
}
// Sorting array
Arrays.sort(arr);
// Printing array
printArray(arr, N);
}
static String sort(String inputString)
{
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 514, 34, 41, 39 };
int N = arr.length;
digitSort(arr, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# python program for the above approach
# Function to print array in non-decreasing order
def printArray(arr, N):
for i in range(0, N):
print(arr[i], end=" ")
# Function to convert an integer to string
def toString(N):
s = ""
while (N > 0):
s += chr(int(N % 10) + ord('0'))
N /= 10
if (len(s) == 0):
s = "0"
return s
# Function to sort each element of the array
# in non-descreasing order of its digits
def digitSort(arr, N):
# Traversing each element to sort
for i in range(0, N):
# Converting number to string
s = toString(arr[i])
# Sorting string
s = list(s)
s.sort()
s = ''.join(s)
# Converting string to integer
arr[i] = int(s)
# Sorting array
arr.sort()
# Printing array
printArray(arr, N)
# Driver Code
if __name__ == "__main__":
arr = [514, 34, 41, 39]
N = len(arr)
digitSort(arr, N)
# This code is contributed by rakeshsahni
C#
// Java program for the above approach
using System;
using System.Collections;
class GFG
{
// Function to print array in non-decreasing order
static void printArray(int []arr, int N)
{
for (int i = 0; i < N; i++)
Console.Write(arr[i] + " ");
}
// Function to convert an integer to string
static String toString(int N)
{
string s = "";
while (N > 0) {
int val = N % 10;
s += val.ToString();
N /= 10;
}
if (s.Length == 0) {
s = "0";
}
return s;
}
// Function to sort each element of the array
// in non-descreasing order of its digits
static void digitSort(int []arr, int N)
{
int []ans = new int[N];
// Traversing each element to sort
for (int i = 0; i < N; i++) {
// Converting number to string
String s = toString(arr[i]);
char []ch = s.ToCharArray();
Array.Sort(ch);
ans[i] = Int32.Parse(String.Join("",ch));
// Converting string to integer
arr[i] = Int32.Parse(s);
}
// Sorting array
Array.Sort(ans);
// Printing array
printArray(ans, N);
}
// Driver Code
public static void Main()
{
int []arr = { 514, 34, 41, 39 };
int N = arr.Length;
digitSort(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript program for the above approach
// Function to print array in
// non-decreasing order
function printArray(arr, N)
{
for(let i = 0; i < N; i++)
document.write(arr[i] + " ");
}
// Function to convert an integer to string
function toString(N)
{
let s = "";
while (N > 0)
{
s = s + (N % 10).toString();
N = Math.floor(N / 10);
}
if (s.length == 0)
{
s = "0";
}
return s;
}
// Function to sort each element of the array
// in non-descreasing order of its digits
function digitSort(arr, N)
{
// Traversing each element to sort
for(let i = 0; i < N; i++)
{
// Converting number to string
let s = arr[i].toString();
// Sorting string
st = s.split('')
st.sort(function (a, b){
return a.charCodeAt(0) -
b.charCodeAt(0); });
s = '';
for(let i = 0; i < st.length; i++)
{
s = s + st[i];
}
// Converting string to integer
arr[i] = parseInt(s);
}
// Sorting array
arr.sort(function(a, b){ return a - b })
// Printing array
printArray(arr, N);
}
// Driver Code
let arr = [ 514, 34, 41, 39 ];
let N = arr.length;
digitSort(arr, N);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N log N), the inbuilt sort function takes N log N time.
Auxiliary space: O(1), since constant extra space is used.
Similar Reads
Sorting an Array in Bash using Insertion Sort Given an array, arr[] of size N, the task is to sort the array in ascending order using Insertion Sort in bash scripting. Examples: Input: arr[] = {9, 7, 2, 5}Output: 2 5 7 9Explanation: The array in sorted order is {2, 5, 7, 9} Input: arr[] = {3, 2, 1}Output: 1 2 3Explanation: The array in sorted o
2 min read
Sort the given Array in Spiral manner starting from the center Given an array arr[] of size N, the task is to sort the array in descending order starting from the mid of the array having the next largest element at the right of the middle element and the next largest at the left of the middle element and so on. Examples: Input: arr[] = {4, 9, 3, 5, 7}Output: 3
9 min read
Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t
8 min read
Sort an array of large numbers 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[] = {
4 min read
Sort number line given as Array by moving an element at ith index by i steps to right Given an array arr[] of N integers, the task is to find the minimum number of the moves needed to sort the array in ascending order by moving an element at ith index by i steps to the right in a single move. Note: In a step, two numbers can lie in the same position. Examples: Input: N = 4, arr[] = {
7 min read
Arrange given numbers to form the smallest number Given an array arr[] of integer elements, the task is to arrange them in such a way that these numbers form the smallest possible number. For example, if the given array is {5, 6, 2, 9, 21, 1} then the arrangement will be 1212569. Examples: Input: arr[] = {5, 6, 2, 9, 21, 1} Output: 1212569 Input: a
14 min read