Sorting algorithms/Counting sort: Difference between revisions

Content added Content deleted
(→‎{{header|Tcl}}: Small improvements)
(Modula-3)
Line 155: Line 155:
}
}
}</lang>
}</lang>

=={{header|Modula-3}}==
<lang modula3>MODULE Counting EXPORTS Main;

IMPORT IO, Fmt;

VAR test := ARRAY [0..7] OF INTEGER {80, 10, 40, 60, 50, 30, 20, 70};

PROCEDURE Sort(VAR a: ARRAY OF INTEGER; min, max: INTEGER) =
VAR range := max - min + 1;
count := NEW(REF ARRAY OF INTEGER, range);
z := 0;
BEGIN
FOR i := FIRST(a) TO LAST(a) DO
count[a[i] - min] := count[a[i] - min] + 1;
END;
FOR i := min TO max DO
WHILE (count[i - min] > 0) DO
a[z] := i;
INC(z);
count[i - min] := count[i - min] - 1;
END;
END;
END Sort;

BEGIN
IO.Put("Unsorted: ");
FOR i := FIRST(test) TO LAST(test) DO
IO.Put(Fmt.Int(test[i]) & " ");
END;
IO.Put("\n");
Sort(test, 10, 80);
IO.Put("Sorted: ");
FOR i := FIRST(test) TO LAST(test) DO
IO.Put(Fmt.Int(test[i]) & " ");
END;
IO.Put("\n");
END Counting.</lang>
Output:
<pre>
Unsorted: 80 10 40 60 50 30 20 70
Sorted: 10 20 30 40 50 60 70 80
</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==