Jump to content

Sorting algorithms/Counting sort: Difference between revisions

Added XPL0
(Added BBC BASIC)
(Added XPL0)
Line 1,605:
300, 1, -2, 3, -4, 5, -6, 7, -8, 100, 11
-8, -6, -4, -2, 1, 3, 5, 7, 11, 100, 300
</pre>
 
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codes;
 
proc CountingSort(Array, Min, Max, Size); \Sort Array
int Array, Min, Max, Size; \minimum, maximum values, number of elements
int Count, I, Z;
[Count:= Reserve((Max-Min+1)*4); \Reserve Count with 4 bytes per integer
for I:= 0 to Max-Min do Count(I):= 0; \initialize Count with 0
for I:= 0 to Size-1 do \for each number count its occurrences
Count(Array(I)-Min):= Count(Array(I)-Min) + 1;
Z:= 0;
for I:= Min to Max do
while Count(I-Min) > 0 do
[Array(Z):= I;
Z:= Z+1;
Count(I-Min):= Count(I-Min) - 1;
];
];
 
int A, I;
[A:= [3, 1, 4, 1, -5, 9, 2, 6, 5, 4];
CountingSort(A, -5, 9, 10);
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )];
]</lang>
 
{{out}}
<pre>
-5 1 1 2 3 4 4 5 6 9
</pre>
 
772

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.