How to rank elements of an integer array based on their sorted order in Java

2 Answers

0 votes
import java.util.Arrays;
import java.util.HashMap;

public class MyClass {
      public static void rank_array(int[] arr) {
        System.out.println("Array: " + Arrays.toString(arr));
        
        if (arr == null || arr.length == 0)
            return;

        int [] arr_copy = Arrays.copyOfRange(arr, 0, arr.length);
        Arrays.sort(arr_copy);

        HashMap<Integer, Integer> map = new HashMap<>();
        int index = 1;
        int item = arr_copy[0];
        map.put(item, index);
        System.out.println(map);

        for (int i = 1; i < arr.length; i++) {
            if (item != arr_copy[i])
                index++;
            map.put(arr_copy[i], index);
            item = arr_copy[i];
            System.out.println(map);
        }

        for (int i = 0; i < arr.length; i++)
            arr_copy[i] = map.get(arr[i]);

        System.out.println("Rank: " + Arrays.toString(arr_copy));
    }
    public static void main(String args[]) {
        int [] arr = {33, 99, 10, 25, 47, 11, 77};
        
        rank_array(arr);
    }
}




/*
run:

Array: [33, 99, 10, 25, 47, 11, 77]
{10=1}
{10=1, 11=2}
{25=3, 10=1, 11=2}
{33=4, 25=3, 10=1, 11=2}
{33=4, 25=3, 10=1, 11=2, 47=5}
{33=4, 25=3, 10=1, 11=2, 77=6, 47=5}
{33=4, 99=7, 25=3, 10=1, 11=2, 77=6, 47=5}
Rank: [4, 7, 1, 3, 5, 2, 6]

*/

 



answered Dec 20, 2021 by avibootz
0 votes
import java.util.Arrays;
import java.util.HashMap;

public class MyClass {

    // ------------------------------------------------------------
    // Function 1: Print the array
    // ------------------------------------------------------------
    public static void printArray(int[] arr) {
        System.out.println("Array: " + Arrays.toString(arr));
    }

    // ------------------------------------------------------------
    // Function 2: Create a copy of the array
    // ------------------------------------------------------------
    public static int[] copyArray(int[] arr) {
        return Arrays.copyOfRange(arr, 0, arr.length);
    }

    // ------------------------------------------------------------
    // Function 3: Sort the array copy
    // ------------------------------------------------------------
    public static void sortArray(int[] arrCopy) {
        Arrays.sort(arrCopy);
    }

    // ------------------------------------------------------------
    // Function 4: Build a map of value → rank
    // ------------------------------------------------------------
    public static HashMap<Integer, Integer> buildRankMap(int[] sortedArr) {

        HashMap<Integer, Integer> map = new HashMap<>();

        int rank = 1;                 // First element gets rank 1
        int previous = sortedArr[0];  // Track last unique value

        map.put(previous, rank);      // Assign rank to first element
        System.out.println(map);

        // Loop through sorted array and assign ranks
        for (int i = 1; i < sortedArr.length; i++) {

            // If new unique value, increase rank
            if (sortedArr[i] != previous) {
                rank++;
            }

            map.put(sortedArr[i], rank);
            previous = sortedArr[i];

            System.out.println(map);  // Show map building step-by-step
        }

        return map;
    }

    // ------------------------------------------------------------
    // Function 5: Apply ranks to original array order
    // ------------------------------------------------------------
    public static int[] applyRanks(int[] original, HashMap<Integer, Integer> map) {
        int[] ranked = new int[original.length];

        for (int i = 0; i < original.length; i++) {
            ranked[i] = map.get(original[i]);  // Replace value with rank
        }

        return ranked;
    }

    // ------------------------------------------------------------
    // Main ranking function
    // ------------------------------------------------------------
    public static void rank_array(int[] arr) {

        printArray(arr);

        if (arr == null || arr.length == 0)
            return;

        // Step 1: Copy array
        int[] arrCopy = copyArray(arr);

        // Step 2: Sort the copy
        sortArray(arrCopy);

        // Step 3: Build rank map
        HashMap<Integer, Integer> rankMap = buildRankMap(arrCopy);

        // Step 4: Apply ranks to original order
        int[] ranked = applyRanks(arr, rankMap);

        System.out.println("Rank: " + Arrays.toString(ranked));
    }

    // ------------------------------------------------------------
    // MAIN METHOD
    // ------------------------------------------------------------
    public static void main(String args[]) {

        int[] arr = {33, 99, 10, 25, 47, 11, 77};

        rank_array(arr);
    }
}



/*
run:

Array: [33, 99, 10, 25, 47, 11, 77]
{10=1}
{10=1, 11=2}
{25=3, 10=1, 11=2}
{33=4, 25=3, 10=1, 11=2}
{33=4, 25=3, 10=1, 11=2, 47=5}
{33=4, 25=3, 10=1, 11=2, 77=6, 47=5}
{33=4, 99=7, 25=3, 10=1, 11=2, 77=6, 47=5}
Rank: [4, 7, 1, 3, 5, 2, 6]

*/

 



answered Jun 20 by avibootz

Related questions

...