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. :)
source
share