Sort the array according to their cubes of each element

Given an array arr[] of N integers, the task is to sort the array according to the cubes of each element.

Examples:

Input: arr[] = { 4, -1, 0, -5, 6 }
Output: -5 -1 0 4 6

Input: arr[] = { 12, 3, 0, 11 }
Output: 0 3 11 12

Approach: The idea is to use Comparator function with inbuilt sort function() to sort the array according to the cubes of their element. Below is the comparator function used:



bool comparator_function(int a, int b)
{
    x = pow(a, 3);
    y = pow(b, 3);
    return x < y;
}

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

-5 -1 0 4 6

Time Complexity: O(N*log N), where N is the number of element in the array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, offbeat

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.