Ranking Function in F #

I wrote an algorithm for ranking an array.

let rankFun array =
    let arrayNew = List.toArray array
    let arrayLength = Array.length arrayNew
    let rankedArray = Array.create arrayLength 1.0
    for i in 0 .. arrayLength - 2 do
        for j in (i+1) .. arrayLength - 1 do
            if arrayNew.[i] > arrayNew.[j] then
                rankedArray.[i] <- rankedArray.[i] + 1.0

            elif arrayNew.[i] < arrayNew.[j] then
                rankedArray.[j] <- rankedArray.[j] + 1.0

            else 
                rankedArray.[i] <- rankedArray.[i] + 0.0
    rankedArray

I wanted to ask you, what do you think about performance? I used for loops, and I was wondering if you think there is another way better than this. Before moving on to this, I sorted my array, preserving the original indexes, rating, and only then resorting to each rank to its original position, which was inconvenient in terms of performance. Now I got to this improved version and was looking for some feedback. Any ideas?

Edit: Duplicate elements must have the same rank .;)

Thank you in advance. :)

+4
source share
1 answer

, , , . , , , , , . .

. F # , .

( , , 1). , .

let getRankings array =
    let rankTable =
        Set.ofArray array
        |> Seq.mapi (fun i n -> n, i + 1)
        |> Map.ofSeq
    array |> Array.map (fun n -> rankTable.[n])

, , array. , .

, , O (n * log (n)), for-loops - O (n ^ 2). (. : Big O.) 10000 100 .

(BTW, else rankedArray.[i] <- rankedArray.[i] + 0.0, , . - , .)

+4

Source: https://habr.com/ru/post/1680333/


All Articles