If you have no idea what it is, google/wikipedia is your friend. Put 2 integer arrays in and off you go!
SCAR Code:
function SpearmansRank(arr1, arr2: TIntegerArray): Extended;
var
sort, rank: array[0..1] of TIntegerArray;
r, s, d2, n: Integer;
begin
if High(arr1) <> High(arr2) then
begin
Writeln('SpearmansRank: Array lengths differ; exiting');
Exit;
end;
if high(arr1) = -1 then
begin
Writeln('SpearmansRank: Arrays empty; exiting');
Exit;
end;
SetArrayLength(sort[0], High(arr1) + 1);
SetArrayLength(sort[1], High(arr1) + 1);
SetArrayLength(rank[0], High(arr1) + 1);
SetArrayLength(rank[1], High(arr1) + 1);
for r := 0 to High(arr1) do
begin
sort[0][r] := arr1[r];
sort[1][r] := arr2[r];
end;
BubbleSort(sort[0]);
BubbleSort(sort[1]);
for r := 0 to High(arr1) do
begin
for s := 0 to High(arr1) do
begin
if arr1[r] = sort[0][s] then
begin
rank[0][r] := s;
Break;
end;
end;
end;
for r := 0 to High(arr1) do
begin
for s := 0 to High(arr1) do
begin
if arr2[r] = sort[1][s] then
begin
rank[1][r] := s;
Break;
end;
end;
end;
for r := 0 to High(arr1) do
IncEx(d2, Round(Sqr(rank[0][r] - rank[1][r])));
n := High(arr1) + 1;
Result := 1 - ((6 * d2)/(n * (Sqr(n) - 1)))
end;
Haven't tested it extensively, but run a couple through it and it worked fine. Also, it doesn't detect multiple values with the same rank yet, so it may give slightly off results when inputting identical values