Dada una array arr[] de N enteros, la tarea es ordenar la array según los cubos de cada elemento.
Ejemplos:
Entrada: arr[] = { 4, -1, 0, -5, 6 }
Salida: -5 -1 0 4 6Entrada: arr[] = { 12, 3, 0, 11 }
Salida: 0 3 11 12
Enfoque: la idea es usar la función Comparator con una función de clasificación incorporada() para clasificar la array de acuerdo con los cubos de sus elementos. A continuación se muestra la función de comparación utilizada:
bool comparator_function(int a, int b) { x = pow(a, 3); y = pow(b, 3); return x < y; }
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Comparator function which returns // a^3 is less than b^3 bool cmp(int a, int b) { int x = pow(a, 3); int y = pow(b, 3); return x < y; } // Function to sort the cubes of array bool sortArr(int arr[], int n) { // Sort the array sort(arr, arr + n, cmp); // Print the array for (int i = 0; i < n; i++) { cout << arr[i] << " "; } } // Driver Code int main() { // Given array int arr[] = { 4, -1, 0, -5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); // Function Call sortArr(arr, n); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG { // Function to sort the cubes of array static void sortArr(int arr[], int n) { Integer[] ar = new Integer[n]; for (int i = 0; i < n; i++) ar[i] = arr[i]; // Sort the array Arrays.sort(ar, new Comparator<Integer>() { public int compare(Integer a, Integer b) { int x = (int)Math.pow(a, 3); int y = (int)Math.pow(b, 3); return (x < y) ? -1 : 1; } }); // Print the array for (int i = 0; i < n; i++) { System.out.print(ar[i] + " "); } } // Driver code public static void main(String[] args) { // Given array int arr[] = { 4, -1, 0, -5, 6 }; int n = arr.length; // Function Call sortArr(arr, n); } } // This code is contributed by offbeat
Python3
# Python3 program for the above approach # Function to sort the cubes of array def sortArr(arr, n): # Make a list of tuples in # the form(cube of (num), num) arr = [(i * i * i, i) for i in arr]; # Sort the array according to # the their respective cubes arr.sort() # Print the array for i in range(n): print(arr[i][1], end = " "); # Driver Code if __name__ == "__main__" : # Given array arr = [ 4, -1, 0, -5, 6 ]; n = len(arr); # Function Call sortArr(arr, n); # This code is contributed by AnkitRai01
C#
// C# program for the above approach using System; using System.Collections; class compare : IComparer { // Call CaseInsensitiveComparer.Compare public int Compare(Object x, Object y) { return ( new CaseInsensitiveComparer()).Compare(x,y); } } class GFG{ // Function to sort the cubes of array static void sortArr(int []arr, int n) { int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = arr[i]; IComparer cmp = new compare(); // Sort the array Array.Sort(ar, cmp); // Print the array for (int i = 0; i < n; i++) { Console.Write(ar[i] + " "); } } // Driver code public static void Main(String[] args) { // Given array int []arr = {4, -1, 0, -5, 6}; int n = arr.Length; // Function Call sortArr(arr, n); } } // This code is contributed by gauravrajput1
Javascript
<script> //Javascript implementation to check whether // K times of a element is present in // the array // Function to sort the cubes of array function sortArr(arr, n) { // Sort the array arr.sort( function( a , b){ var x = Math.pow(a,3); var y = Math.pow(b,3); if(x > y) return 1; if(x < y) return -1; return 0; }); // Print the array for (var i = 0; i < n; i++) { document.write(arr[i] + " "); } } // Driver program to test above var arr = [ 4, -1, 0, -5, 6 ]; var n = arr.length; sortArr(arr, n); // This code is contributed by shivani. </script>
Producción:
-5 -1 0 4 6
Complejidad de tiempo: O(N*log N) , donde N es el número de elementos en la array.