Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Counting Sort

The document discusses an innovative counting sort algorithm that can sort both positive and negative numbers in linear time. It provides the basics of counting sort, explains how it works for positive numbers, and then describes the modified algorithm to handle negative numbers as well. The key idea is to map negative numbers to positive numbers to leverage the existing counting sort approach.

Uploaded by

Naveenjot Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Counting Sort

The document discusses an innovative counting sort algorithm that can sort both positive and negative numbers in linear time. It provides the basics of counting sort, explains how it works for positive numbers, and then describes the modified algorithm to handle negative numbers as well. The key idea is to map negative numbers to positive numbers to leverage the existing counting sort approach.

Uploaded by

Naveenjot Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

International Journal of Applied Engineering Research ISSN 0973-4562 Volume 13, Number 1 (2018) pp.

195-201
© Research India Publications. http://www.ripublication.com

An Innovative Counting Sort Algorithm for Negative Numbers

Siddharth Srivastava 1
Research Scholar, Department of Computer Science and Engineering,
Indian Institute of Technology, Kanpur (UP), India

Umesh Chandra Jaiswal


Professor, Department of Computer Science and Engineering
Madan Mohan Malaviya University of Technology, Gorakhpur, 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

Abstract version of counting sort is capable of sorting both negative


Counting sort is basically a linear time sorting algorithm. It and positive numbers in linear time.
sorts given array having integer elements ranging from 0 to K
(where ‘K’ is upper limit of integer which can be present in LITERATURE SURVEY
input array). Counting sort can only be applied to positive
integers and yields running time of ϴ (n+K), where ‘n’ is Introduction to Algorithms by Thomas C Cormen [1] is a
length of input array and ‘K’ is highest integer which may be standard and word wide accepted book for design and analysis
present in input array. Here in this paper we will introduce a of algorithm and it is highlighted in the book that counting
modified version of counting sort which is capable of sorting sort cannot work on negative numbers. Not a single paper is
both positive as well as negative integers, that too in linear available in the literature that deals with negative numbers
time. Due to this the range of integers present in input array using counting sort algorithm. This paper presents a
gets increased from (0 to K) to (-ve integer to +ve integer). redesigned counting sort algorithm that it can sort the negative
The basics of this modification shows that counting sort can numbers given in the list.
easily and efficiently be applied on mix of positive and
negative integers, that too without hampering the running time 1) Basics of Counting Sort
of counting sort which remains linear as in previous case. We
named this modified version of counting sort as negative Counting sort is used to sort given input array (of size say ‘n’)
version of counting sort. We have implemented this sort in our consisting of positive integers (ranging from 0 to max) only.
home-made software in which user can simply type integers in To sort this input array counting sort creates an array which
unsorted order and using negative version of counting sort can be termed as mapping array (of size max, where max is
software sorts the unsorted inputted array. greatest positive integer present in input array). In this
mapping array frequency of occurrence of each element
present in input array is stored. Then some manipulations are
Keywords: Counting Sort, Negative Version of Counting performed on this mapping array (we will see later in this
Sort, Linear time, theta notation, running sum. paper, how this is done). This mapping array is used to
produce an output array which contains all elements present in
input array, but in sorted (ascending or descending) order.
INTRODUCTION Now let’s explain the algorithm of counting sort.
Negative version of counting sort is the modified form of
simple counting sort algorithm. As we all know that counting
sort is incapable of sorting negative numbers because it uses 2) Algorithm of Counting Sort
mapping array or intermediate array (which is used to map This section of the paper has given the original Counting Sort
unsorted input array to sorted output array). This paper algorithm and later on the algorithm is explained with suitable
presents the negative version of counting sort using negative example.
numbers as well as positive numbers; the designed algorithm
will sort the numbers as per the order of the numbers. This
paper first, discusses about the basics of traditional counting
sort and later on explains negative version of counting sort.
Finally, it has been discussed with an example that this new

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

stores nus[]” arrays.


frequency of When value
occurrence of of integer in
–ve integers input[] array
present in is +ve then it
“input[]” is mapped
array. using +ve
Line 14- This Loop is After execution of Loop value mapping
Line 15 used to of mapping_minus[]={2,2,1}. array.
produce Similarly
Index of array 0 1 2 when integer
“running mapping_min
sum” on –ve value in
us[] input[] array
mapping
array which Value stored 2 2 1 is –ve it is
tell that how in array mapped to
many mapping_min output[] array
elements are us[] using –ve
less than or mapping
equal to ‘i’. array.

Line 16- This Loop is After execution of Loop value


Line 17 used to of mapping_plus[]={1,1,2,3}. 3) Analysis of Newly Modified and existing of Counting
produce Index of array Sort
0 1 2 3
“running mapping_plus If we analyse simple counting sort algorithm given in section
sum” on +ve [] 2.1 then we will find that it’s running time is simply ϴ(n), if
mapping length of input array is ‘n’. This analysis can easily be
array which Value stored 1 1 2 3
concluded by viewing core lines of sorting algorithm i.e. from
tell that how in array
line 9 to line 11. Since all other steps of algorithm can easily
many mapping_plus
be computed during input phase i.e. when the system is
elements are []
reading input from input[] array and maximum length of input
less than or is known. Then mapping phase can also be computed. Only
equal to ‘i’. running sum loop and output loop is executed after input data
Line 18- Loop to add After Execution of Loop value is read by the system. Hence time complexity of this
Line 19 value stored of mapping_plus[]={3,3,4,5} algorithm is ϴ(n), a linear time complexity.
in To compute the time complexity of the new designed
mapping_min algorithm, the algorithm discussed in section 3 is to be
[0] to all analysed line by line. If the algorithm is read is carefully, it
elements of will be found that if max and min are given in advance, then
mapping_plus the computation from line 1 to 13 can be performed
[] array. This simultaneously with the reading of input data from array
tells input[]. Loops in line 14,16 and 18 iterates for min, max and
mapping_plus max time respectively. Let us assume that one iteration takes 1
[] array that unit time, then time required for the above mentioned three
these many – loops in (min+ 2*max). Loop in line 20 iterates for
ve numbers input.length time. If the size of array is ‘n’, then
are present in input.length=n, hence loop iterates for n unit of time.
input array Therefore total time complexity is (min+2*max+n) which is
and make linear in nature. If k=min+2*max, the execution time comes
running sum out to be (n+k). If k tends to n then the time complexity is
of +ve array θ(n+n) which is θ(2n) that equals to θ(n). These analysis
consistent. shows that time complexity of new designed algorithm is
Line 20- Loop to map After execution of Loop output linear and similar to existing counting sort algorithm.. Hence
Line 29 values stored array produced is output[]={- new designed version of counting sort executes in linear time
in “input[]” 2,-1,0,2,3} to yield output consisting of positive as well as negative
array to integers in sorted order.
“output[]”
array using
“mapping_pl
us[]” and
“mapping_mi

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

IMPLEMENTATION INNOVATIVE COUNTING SORT


ALGORITHM Table 3: Colour Scheme
This paper presents an innovative counting sort algorithm for Values Colour Schemes
negative numbers. To show the working of this new
algorithm, software has been developed using Java Swing. Values of +ve mapping array Yellow
This software consists of one input field that takes input Value of –ve mapping array Orange
values including negative numbers to sort the input values. To
execute this software, one has to press simulate button. This Value of output array Green
software displays the step by step execution of this algorithm. Initialize,+ve & -ve mapping, White (with input array
Several output elements have been displayed using different Runsum on +ve mapping, elements as gray)
colour scheme as given in Table 3. Runsum on –ve mapping,
update runsum on +ve
mapping, final output
CONCLUSION
Iteration 1, Iteration 2, … so #00FFFF colour (with input
Counting sort algorithm may further be modified for positive on array elements as #00FFFF)
and negative real (decimal) numbers as future extension of the
work carried out for this paper. Table Showing colour combination schemes used in our
software.

Negative mapping 1 -3 2 -2 4 1 0 -1
array“mapping_minus
Input Array
[]”

0 1 2 3 Index of array 0 1 2 3 4 Index of array


0 1 1 1 Values in array 1 2 1 0 1 Values in 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

[1] Thomas H. Cormen, C.E. Leiserson, R.L.


Rivest,C.Strin,“Introduction to Algorithms”,PHI
Publications, 2nd Edition, 2007.
[2] Siddharth Srivastava, “Virtual Technology”, Lambert
Academic Publication, 2012.
[3] H. Schield, “Java The Complete Reference”, Tata
McHill Publication, 7th Edition, 2007.
[4] Edmonds, Jeff (2008), "5.2 Counting Sort (a Stable
Sort)", How to Think about Algorithms,

201

You might also like