Counting Sort
Counting Sort
195-201
© Research India Publications. http://www.ripublication.com
Siddharth Srivastava 1
Research Scholar, Department of Computer Science and Engineering,
Indian Institute of Technology, Kanpur (UP), India
Shachi Mall
Assistant Professor, Department of Computer Science and Engineering
Madan Mohan Malaviya University of Technology, Gorakhpur, India.
Orcid Id: orcid.org/0000-0002-4443-4885
195
International Journal of Applied Engineering Research ISSN 0973-4562 Volume 13, Number 1 (2018) pp. 195-201
© Research India Publications. http://www.ripublication.com
Algorithm of Counting Sort vii. The line by line explanation of the above algorithm
has been discussed in Table I. The input array has
Counting Sort(integer input[], integer output[], integer max { been defined as given below:
// input[] is input array containing +ve integers in input[]={4,3,5,2,1,5,2,5,6} (Example value)
unsorted order.
// output[] is array which is produced as result of
execution of counting sort and contains Table 1: The input array
all elements of input array in sorted order. Line Explanation Example
// max is maximum or greatest integer present in Number
input array. Line 1 store length of n=9 (from example value)
input array in
1. integer n=input.length(); //Store length of input array in
variable ‘n’
variable ‘n’.
Line 2 mapping[max] Create mapping[] array of size
2. integer mapping[max]; //array of size max. ranging from 0 to max. where
3. for(integer i=0;i<=max;i++) max=6. (from example value)
4. mapping[i]=0; //initialize each element of array to 0. Line 3- Initialize mapping[]={0,0,0,0,0,0,0}
5. For(integer i=1;i<=n;i++) Line 4 elements of
mapping[]
6. mapping[input[i]]=mapping[input[i]]+1; array to zero.
Line 5- Loop to store After execution of Loop value
/* Loop to store frequency of occurrence of element in Line 6 frequency of of mapping[]={0,1,2,1,1,2,1}.
“input[]” array to array “mapping[]”using element of occurrence of Inde 0 1 2 3 4 5 6
“input[]” array as index to array “mapping[]”.*/ element in x of
7. For(integer i=1;i<=max;i++) “input[]” array arra
to array y
8. mapping[i]=mapping[i-1]+mapping[i]; “mapping[]” map
//This Loop is used to produce “running sum” which using element ping
tell that how many elements are less of “input[]” []
than or equal to ‘i’.// array as index Val 0 1 2 1 1 3 1
9. For(integer i=n; i>=1;i--) to array ue
“mapping[]”. stor
10. {output[mapping[input[i]]]=mapping[input[i]];
ed
11. mapping[input[i]]=mapping[input[i]]-1; in
12. } arra
/* This loop produces the output array which contains all y
elements of input array in sorted map
order. Explanation of Above Listed Algorithm */ ping
[]
Line 7- This Loop is After execution of Loop value
The above algorithm is the counting sort algorithm. The Lines Line 8 used to of mapping[]={0,1,3,4,5,8,9}.
bracketed in // or */ are comment lines. The algorithm is produce Inde 0 1 2 3 4 5 6
discussed with help of example. This algorithm Variable “running sum” x of
Description as follows: which tell that arra
i. input[]:- represents array which gets the input by the how many y
user and stores +ve integers ranging from 0 to max in elements are map
unsorted way. less than or ping
ii. Output[]:- This array represents an array which is equal to ‘i’. []
produced as output of execution of counting sort Val 0 1 3 4 5 8 9
algorithm on input[] array. It stores all elements of ue
input[] array in sorted order. stor
ed
iii. max: This variable is used to store highest integer
element present in input[] array. in
arra
iv. n: This a variable used to store size of input[] array. y
v. mapping[]: This is an intermediate array, using map
which input[] array is mapped to output[] array in ping
sorted manned. []
vi. i: Loop variable used for loop iteration. Line 9- Produces After execution of loop value
Line 11 output[] array of output[]={1,2,2,3,4,5,5,5,6}
196
International Journal of Applied Engineering Research ISSN 0973-4562 Volume 13, Number 1 (2018) pp. 195-201
© Research India Publications. http://www.ripublication.com
COUNTING SORT ALGORITHM and learning aspect)? How is the enterprise assessed
Having the knowledge of original counting sort, it becomes by the shareholders (financial aspects)?
easy to understand the modified counting sort. The basic idea
behind the counting sort is simple. The new algorithm uses 1) Modified Counting Sort Algorithm
two arrays namely “mapping_plus[]” and “mapping_minus[]”
instead of “mapping[] which simply maps positive number to
“output[]” array. The “mapping_plus[]” array stores the The following is the Modified Algorithm for negative
frequency of occurrence of positive integers present in numbers:
“input[]” array whereas “mapping_minus[]” array stores the
Algorithm of Modified Counting Sort
frequency of occurrence of negative integers present in
“input[]” array. This concept is shown pictorially in Figure 1. Modified_Version_of_Counting_Sort(integer input[], integer
output[], integer max, integer min)
To understand the concept let us take an example. Suppose {/* input[]: An array used to store input given by the
input array is input[]={3,-1,-2,0,2}and this is to be sorted in user which is only integer values.
ascending order. The working of the new designed algorithm output[]: An array used to store result after
is discussed step by step below. Following is the complete execution of modified version of counting sort.
algorithm steps and process. max: A variable contains highest value present in
“input[]” array.
Steps Process min: A variable contains lowest value present in
1) Create mapping_plus[] and mapping_minus[] arrays “input[]” array.
of size 3 and 2 respectively. Because in input[] array */
highest element is 3 and lowest is -2. Since size of 1. if(min<0)
array cannot be negative so we initialize size of
mapping_minus[] as 2 i.e. we take mod of lowest 2. min=min*(-1) //making min +ve
number present in array input[] if lowest number is 3. integer mapping_plus[max],mapping_minus[min]
negative. If lowest number is non-negative then /* arrays of size max and min used to store frequency
algorithm works as simple counting sort algorithm. of +ve and –ve integers present
2) Store frequencies of occurrence of +ve and –ve in “input[]” array respectively */
numbers in respective mapping arrays.i.e. 4. for(integer i=0;i<=max;i++)
mapping_plus[]={1,0,1,1} and
mapping_minus[]={0,1,1}. 5. mapping_plus[i]=0; // initialize +ve mapping array
to 0.
3) Now first perform “running sum” on –ve mapping
array from i=mapping_minus.length to 0. This make 6. for(integer i=0;i<=min;i++)
mapping_minus[]={2,2,1}. Then perform running 7. mapping_minus[i]=0; //initialize –ve mapping array
sum on +ve mapping array from i=0 to to 0.
mapping_plus.length. This will make
8. for(integer i=1;i<=input.length;i++)
mapping_plus[]={1,1,2,3}.Now add
mapping_minus[0]. To all elements of 9. {if(input[i]>=0)
mapping_plus[]. This will make mapping plus as 10. mapping_plus[input[i]]=mapping_plus[input[i]]+1;
mapping_plus[]={3,3,4,5}. This is done to make the
11. else if(input[i]<0)
running sum consistent since –ve integers are
followed by positive integers in sorting (ascending 12. mapping_minus[input[i]*-
order). Since we use two arrays one for mapping –ve 1]=mapping_minus[input[i]*-1]+1;
and other for mapping +ve numbers so positive array 13. }
must have idea that how many –ve elements are
/* Loop to store frequency of occurrence of +ve and –
there. This is done using step shown above.
ve integers present in “input[]”
4) Now map output array using +ve and –ve mapping array into “mapping_plus[]” and “mapping_minus[]”
arrays to get output[] array in sorted order. How this arrays respectively.*/
is done? This step will be clear when reader go
14. for(integer i=min-1;i>=0;i--)
through algorithm shown next.enterprises, the in-
house business processes, the customer satisfaction, 15.
the measures to improve the financial results. It mapping_minus[i]=mapping_minus[i]+mapping_mi
allows to answer four most important questions for nus[i+1];
the enterprise: how is it assessed by the clients /*Loop to perform running sum on –ve mapping array
(client's aspect)? Which processes could provide the from min to 0.*/
enterprise with the unique position (in-house aspect)?
How to achieve further improvements (innovation 16. for(integer i=1;i<=max;i++)
197
International Journal of Applied Engineering Research ISSN 0973-4562 Volume 13, Number 1 (2018) pp. 195-201
© Research India Publications. http://www.ripublication.com
17. mapping_plus[i]=mapping_plus[i]+mapping_plus[i- ix. The line by line explanation of the above algorithm
1]; has been discussed in Table 2. The input array has
/*Loop to perform running sum on +ve mapping array been defined as given below:
from 0 to max.*/ input[]={3,-1,-2,0,2}; (Example Value)
18. for(integer i=0;i<=max;i++)
19. mapping_plus[i]=mapping_plus[i]+mapping_minus[ Table 2: Explanation of the above Algorithm
0];
/*Loop to add running sum of 0th position of –ve Line Explanation Example
mapping array to +ve mapping array Number
elements in order to make +ve mapping running sum
consistent. */ Line 1- Check if max=3 and min=-2 (from
Line 2 min<0 then example value) since min<0 so
20. for(integer i=1;i<input.length;i++) take mod of min=2 which is nothing but
21. { if(input[i]>=0) min i.e. mod(-2) or mod(min)
22. { output[mapping_plus[input[i]]]=input[i]; min=min*-1.
23. mapping_plus[input[i]]=mapping_plus[input[i]]-1; Line 3 mapping_plus Create +ve and –ve mapping
[max] & array of size ranging from 0 to
24. } mapping_min max and 0 to min respectively.
25. else if(input[i]<0) us[min] Using these arrays integers are
26. {output[mapping_minus[input[i]*-1]]=input[i]; mapped to output array in
future steps. where max=3 and
27. mapping_minus[input[i]*- min=2. (from example value &
1]=mapping_minus[input[i]*-1]-1; Line 1-Line 2)
28. } Line 4- Initialize mapping_plus[]={0,0,0,0} &
29. } Line 7 elements of mapping_minus[]={0,0,0}
/*Loop to produce “output[]” array which stores all mapping_plus
elements of “input[]” array but in [] and
sorted order.*/ mapping_min
us[] arrays to
} zero.
Line 8- Loop to store After execution of Loop value
2) Explanation of new Designed Algorithm Line 13 frequency of of mapping_plus[]={0,0,1,1}.
The following is the description of variables used in the newly occurrence of
designed algorithm. Variable Description: element in
“input[]” Index of 0 1 2 3
i. input[]:- This represents an array which takes input array to array
by the user and stores +ve and –ve integers ranging arrays mapping_pl
from min to max in unsorted way. “mapping_pl us[]
ii. Output[]:-This represents an array which is us[]” & Value 1 0 1 1
produced as output of execution of negative “mapping_mi stored in
numbers. This gives a new version of Counting Sort nus[]” using array
algorithm on input[] array. It stores all elements of element of mapping_pl
your array in sorted order. “input[]” us[]
array as index
iii. max: variable used to store highest integer element
to array
present in input[] array.
“mapping_pl Index of array 0 1 2
iv. min: variable used to store lowest integer element us[]” and mapping_min
present in input[] array. “mapping_mi us[]
v. input.length: used to represent size of input[] array nus[]” arrays. Value stored 0 1 1
(say n=input.length). “mapping_pl in array
us[]” array mapping_min
vi. mapping_plus[]: intermediate array using which +ve
stores us[]
elements of input[] array are mapped to output[]
frequency of
array in sorted manned.
occurrence of
vii. mapping_minus[]: intermediate array using which - +ve integers
ve elements of input[] array are mapped to output[] and
array in sorted manned. “mapping_mi
viii. i: Loop variable used for loop iteration. nus[]” array
198
International Journal of Applied Engineering Research ISSN 0973-4562 Volume 13, Number 1 (2018) pp. 195-201
© Research India Publications. http://www.ripublication.com
199
International Journal of Applied Engineering Research ISSN 0973-4562 Volume 13, Number 1 (2018) pp. 195-201
© Research India Publications. http://www.ripublication.com
Negative mapping 1 -3 2 -2 4 1 0 -1
array“mapping_minus
Input Array
[]”
Positive mapping
array
“mapping_plus[]”
-3 -2 -1 0 1 1 2 4
Output Array
-3
2
-2
Figure 1: Shows concept used in Innovative of counting sort algorithm.
4
1
0
-1
200
International Journal of Applied Engineering Research ISSN 0973-4562 Volume 13, Number 1 (2018) pp. 195-201
© Research India Publications. http://www.ripublication.com
Figure 2: Snapshot of counting sort can sort an array of numbers consisting positive as well as negative numbers.
REFERENCES
201