📌  相关文章
📜  通过反转数组中的数字对数组进行排序

📅  最后修改于: 2021-09-06 11:29:33             🧑  作者: Mango

给定一个由N 个非负整数组成的数组arr[] ,任务是根据它们的倒数对这些整数进行排序。

例子:

做法:思路是将每个元素与其反向存储在一个向量对中,然后根据存储的反向对向量中的所有元素进行排序。最后,按顺序打印元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
// Function to return the
// reverse of n
int reversDigits(int num)
{
    int rev_num = 0;
    while (num > 0) {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
 
// Function to sort the array according to
// the reverse of elements
void sortArr(int arr[], int n)
{
    // Vector to store the reverse
    // with respective elements
    vector > vp;
 
    // Inserting reverse with elements
    // in the vector pair
    for (int i = 0; i < n; i++) {
        vp.push_back(
            make_pair(reversDigits(arr[i]),
                      arr[i]));
    }
 
    // Sort the vector, this will sort the pair
    // according to the reverse of elements
    sort(vp.begin(), vp.end());
 
    // Print the sorted vector content
    for (int i = 0; i < vp.size(); i++)
        cout << vp[i].second << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 12, 10, 102, 31, 15 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    sortArr(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
     
// Function to return the
// reverse of n
static int reversDigits(int num)
{
    int rev_num = 0;
    while (num > 0)
    {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
   
// Function to sort the array according
// to the reverse of elements
static void sortArr(int arr[], int n)
{
     
    // Vector to store the reverse
    // with respective elements
    ArrayList vp = new ArrayList<>();
   
    // Inserting reverse with elements
    // in the vector pair
    for(int i = 0; i < n; i++)
    {
        vp.add(new int[]{reversDigits(arr[i]),
                                      arr[i]});
    }
   
    // Sort the vector, this will sort the pair
    // according to the reverse of elements
    Collections.sort(vp, (a, b) -> a[0] - b[0]);
   
    // Print the sorted vector content
    for(int i = 0; i < vp.size(); i++)
        System.out.print(vp.get(i)[1] + " ");
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 12, 10, 102, 31, 15 };
    int n = arr.length;
     
    sortArr(arr, n);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation of the approach
 
# Function to return the
# reverse of n
def reversDigits(num) :
 
    rev_num = 0;
    while (num > 0) :
        rev_num = rev_num * 10 + num % 10;
        num = num // 10;
 
    return rev_num;
 
# Function to sort the array according to
# the reverse of elements
def sortArr(arr, n) :
 
    # Vector to store the reverse
    # with respective elements
    vp = [];
 
    # Inserting reverse with elements
    # in the vector pair
    for i in range(n) :
        vp.append((reversDigits(arr[i]),arr[i]));
 
    # Sort the vector, this will sort the pair
    # according to the reverse of elements
    vp.sort()
 
    # Print the sorted vector content
    for i in range(len(vp)) :
        print(vp[i][1],end= " ");
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 12, 10, 102, 31, 15 ];
    n = len(arr);
 
    sortArr(arr, n);
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function to return the
    // reverse of n
    static int reversDigits(int num)
    {
        int rev_num = 0;
        while (num > 0)
        {
            rev_num = rev_num * 10 + num % 10;
            num = num / 10;
        }
        return rev_num;
    }
      
    // Function to sort the array according to
    // the reverse of elements
    static void sortArr(int[] arr, int n)
    {
       
        // Vector to store the reverse
        // with respective elements
        List> vp = new List>();
      
        // Inserting reverse with elements
        // in the vector pair
        for (int i = 0; i < n; i++)
        {
            vp.Add(new Tuple(reversDigits(arr[i]),arr[i]));
        }
      
        // Sort the vector, this will sort the pair
        // according to the reverse of elements
        vp.Sort();
      
        // Print the sorted vector content
        for (int i = 0; i < vp.Count; i++)
            Console.Write(vp[i].Item2 + " ");
    }
 
  // Driver code
  static void Main()
  {
    int[] arr = { 12, 10, 102, 31, 15 };
    int n = arr.Length;
  
    sortArr(arr, n);
  }
}
 
// This code is contributed by divyesh072019


Javascript


输出:
10 31 12 15 102

时间复杂度:

O(N*log N), 其中 N 是数组的大小

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live