How to rank elements of an integer array based on their sorted order in VB.NET

1 Answer

0 votes
' How to rank elements of an integer array based on their sorted order in VB.NET
' ------------------------------------------------------------------------------
' We will:
' 1. Print the original array
' 2. Create a copy of the array
' 3. Sort the copy
' 4. Build a map (Dictionary) from value → rank
' 5. Apply ranks back to the original order

Imports System
Imports System.Collections.Generic

Module MyModule

    ' ------------------------------------------------------------
    ' Function 1: Print the array
    ' ------------------------------------------------------------
    ' Prints the contents of an integer array in a friendly format.
    Sub PrintArray(arr As Integer())
        Console.WriteLine("Array: [" & String.Join(", ", arr) & "]")
    End Sub

    ' ------------------------------------------------------------
    ' Function 2: Create a copy of the array
    ' ------------------------------------------------------------
    ' Returns a new array with the same elements as the input.
    Function CopyArray(arr As Integer()) As Integer()
        Dim copy(arr.Length - 1) As Integer
        Array.Copy(arr, copy, arr.Length)
        Return copy
    End Function

    ' ------------------------------------------------------------
    ' Function 3: Sort the array copy
    ' ------------------------------------------------------------
    ' Sorts the given array in ascending order (in-place).
    Sub SortArray(arrCopy As Integer())
        Array.Sort(arrCopy)
    End Sub

    ' ------------------------------------------------------------
    ' Function 4: Build a map of value → rank
    ' ------------------------------------------------------------
    ' Ranking rules:
    ' - Lowest value gets rank 1
    ' - Equal values share the same rank
    ' - Rank increases only when encountering a new unique value
    Function BuildRankMap(sortedArr As Integer()) As Dictionary(Of Integer, Integer)

        Dim map As New Dictionary(Of Integer, Integer)()

        Dim rank As Integer = 1                       ' First unique value gets rank 1
        Dim previous As Integer = sortedArr(0)        ' Track last unique value

        map(previous) = rank                          ' Assign rank to first element

        ' Loop through sorted array and assign ranks
        For i As Integer = 1 To sortedArr.Length - 1

            ' If new unique value, increase rank
            If sortedArr(i) <> previous Then
                rank += 1
            End If

            map(sortedArr(i)) = rank
            previous = sortedArr(i)
        Next

        Return map
    End Function

    ' ------------------------------------------------------------
    ' Function 5: Apply ranks to original array order
    ' ------------------------------------------------------------
    ' Builds a new array where each element is replaced by its rank.
    Function ApplyRanks(original As Integer(),
                        map As Dictionary(Of Integer, Integer)) As Integer()

        Dim ranked(original.Length - 1) As Integer

        For i As Integer = 0 To original.Length - 1
            ranked(i) = map(original(i))  ' Replace value with rank
        Next

        Return ranked
    End Function

    ' ------------------------------------------------------------
    ' Main ranking function
    ' ------------------------------------------------------------
    ' Orchestrates the whole process:
    ' 1. Print original array
    ' 2. Copy array
    ' 3. Sort the copy
    ' 4. Build rank map
    ' 5. Apply ranks and print result
    Sub RankArray(arr As Integer())

        PrintArray(arr)

        If arr Is Nothing OrElse arr.Length = 0 Then
            Return
        End If

        ' Step 1: Copy array
        Dim arrCopy As Integer() = CopyArray(arr)

        ' Step 2: Sort the copy
        SortArray(arrCopy)

        ' Step 3: Build rank map
        Dim rankMap As Dictionary(Of Integer, Integer) = BuildRankMap(arrCopy)

        ' Step 4: Apply ranks to original order
        Dim ranked As Integer() = ApplyRanks(arr, rankMap)

        Console.WriteLine("Rank:  [" & String.Join(", ", ranked) & "]")
    End Sub

    ' ------------------------------------------------------------
    ' MAIN METHOD
    ' ------------------------------------------------------------
    Sub Main()

        Dim arr As Integer() = {33, 99, 10, 25, 47, 11, 77}

        RankArray(arr)

    End Sub

End Module



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

 



answered Jun 21 by avibootz

Related questions

...