Open In App

Java Program to Sort Items By Weight

Given two array items and weights which denotes items and their respective weights. Both arrays are of equal length. For every index ‘i’, items[i] and weights[i] represent the ith item name and its weight respectively. Your task is to return a new array of items sorted in decreasing order by their weights.

Note: Each item has a unique positive integer weight.



Examples of Sorting Items by Weight

Input: items = [“Laptop”, “TV”, “Phone”, “Watch”]
weights = [500, 1000,250, 50]
Output : [“TV”, “Laptop”, “Phone”, “Watch”]
Explanation: Here the items are sorted based on their weight in decreasing order

Input: items = [“Banana”, “Mango”, “Apple”]
weights = [50, 100, 60]
Output : [“Mango”, “Apple”, “Banana”]



Sort Items By Weight by using the Priority Queue

The idea is to put all the items along with their respective weights into a Priority queue and then sort items in decreasing order by their weights using a custom comparator.

Follow the steps given below to implement the approach:

Below is the implementation of the above approach.




// Java Program to Sort Items 
// By Weight
import java.io.*;
import java.util.PriorityQueue;
  
// Driver Class
public class SortByWeight {
  
    // Pair Class
    // Used to bind item and weight
    // together
    public static class Pair {
        String item;
        int weight;
  
        Pair(String item, int weight)
        {
            this.item = item;
            this.weight = weight;
        }
    }
  
    public static String[] sort(String[] items,
                                int[] weights)
    {
        // initialize a priority queue
        // it use a custom comparator to sort the pairs
        // based on the weight
        // attribute in descending order
        PriorityQueue<Pair> pq = new PriorityQueue<>(
            (a, b) -> Integer.compare(b.weight, a.weight));
  
        // add pair in pq
        for (int i = 0; i < items.length; i++) {
            pq.add(new Pair(items[i], weights[i]));
        }
  
        // initialize ans array
        String[] ans = new String[items.length];
  
        // initialize idx
        int idx = 0;
  
        // remove pair and place in ans array
        // while priority queue is not empty
        while (!pq.isEmpty()) {
  
            // remove pair from pq
            Pair p = pq.remove();
  
            // add item in ans array
            ans[idx] = p.item;
            idx++;
        }
        return ans;
    }
  
    // main function
    public static void main(String[] args)
    {
        String[] items
            = { "Laptop", "TV", "Phone", "Watch" };
        int[] weight = { 500, 1000, 250, 50 };
  
        String[] ans = sort(items, weight);
  
        for (String x : ans) {
            System.out.print(x + " ");
        }
    }
}

Output
TV Laptop Phone Watch 

Complexity of the above program:

Time Complexity : O(N * log(N)), Where N is the number of items
Space Complexity : O(N)


Article Tags :