How to resolve the algorithm Sorting algorithms/Counting sort step by step in the zkl programming language
How to resolve the algorithm Sorting algorithms/Counting sort step by step in the zkl programming language
Table of Contents
Problem Statement
Implement the Counting sort. This is a way of sorting integers when the minimum and maximum value are known.
The min and max can be computed apart, or be known a priori.
Note: we know that, given an array of integers, its maximum and minimum values can be always found; but if we imagine the worst case for an array that can hold up to 32 bit integers, we see that in order to hold the counts, an array of up to 232 elements may be needed. I.E.: we need to hold a count value up to 232-1, which is a little over 4.2 Gbytes. So the counting sort is more practical when the range is (very) limited, and minimum and maximum values are known a priori. (However, as a counterexample, the use of sparse arrays minimizes the impact of the memory usage, as well as removing the need of having to know the minimum and maximum values a priori.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Counting sort step by step in the zkl programming language
Source code in the zkl programming language
fcn countingSort(array, min, max){ // modifies array
count:=(max - min + 1).pump(List().write,0); // array of (max - min + 1) zeros
foreach number in (array){
count[number - min] += 1;
}
z:=-1;
foreach i in ([min .. max]){
do(count[i - min]){ array[z += 1] = i }
}
array
}
array:=List(4, 65, 2, -31, 0, 99, 2, 83, 182, 1);
countingSort(array,(0).min(array), (0).max(array)).println();
You may also check:How to resolve the algorithm Pascal's triangle/Puzzle step by step in the Raku programming language
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the Prolog programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the GAP programming language
You may also check:How to resolve the algorithm Collections step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Range expansion step by step in the Crystal programming language