PROCEDURE Sort(VAR items: Items; nofitems: INTEGER);
VAR
myitems: Items; (* local copy of items *)
count: ARRAY maxitems OF INTEGER;
i, j: INTEGER; (* indices of items and myitems *)
BEGIN
(* initialize count array *)
i := 0;
WHILE i < nofitems DO
count[i] := 0; INC(i);
END;
(* work on a local copy of items *)
myitems := items;
(* compare all items with each other and count *)
i := nofitems-1;
WHILE i >= 1 DO
j := i-1;
WHILE j >= 0 DO
IF myitems[j] <= myitems[i] THEN
INC(count[i]);
ELSE
INC(count[j]);
END;
DEC(j);
END;
DEC(i);
END;
(* update items according to count *)
i := 0;
WHILE i < nofitems DO
items[count[i]] := myitems[i]; INC(i);
END;
END Sort;
|