📌  相关文章
📜  分别对每个数字进行排序后对给定的数组进行排序

📅  最后修改于: 2022-05-13 01:56:09.106000             🧑  作者: Mango

分别对每个数字进行排序后对给定的数组进行排序

给定一个大小为N的数组arr ,任务是对数组中每个元素的数字进行排序,然后按非递减顺序对数组进行排序。排序后打印数组。

例子:

方法:按照以下步骤解决此问题:

  1. 创建一个函数toString ,它将接受整数N作为参数,并将N以字符串的形式返回。
  2. 在函数toString中传递数组arr的每个元素以将其转换为字符串。对该字符串进行排序并将其转换回整数。在此之后,将每个元素替换为数组arr中转换后的整数。
  3. 根据上述观察对数组arr进行排序并打印答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
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


输出
14 34 39 145 

时间复杂度: O(N)
辅助空间: O(1)