0% found this document useful (0 votes)
84 views8 pages

Counting Sort - Good

This article visualizes, designs, and analyzes the counting sort algorithm. Counting sort works by counting the number of occurrences of each unique element in an array and storing those counts in an auxiliary array. It then maps the counts as indices to sort the elements. Counting sort runs in O(n) time complexity and O(n) space complexity, where n is the size of the input array. It is a stable sorting algorithm but not an in-place algorithm since it uses an auxiliary array.

Uploaded by

yt peek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views8 pages

Counting Sort - Good

This article visualizes, designs, and analyzes the counting sort algorithm. Counting sort works by counting the number of occurrences of each unique element in an array and storing those counts in an auxiliary array. It then maps the counts as indices to sort the elements. Counting sort runs in O(n) time complexity and O(n) space complexity, where n is the size of the input array. It is a stable sorting algorithm but not an in-place algorithm since it uses an auxiliary array.

Uploaded by

yt peek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

levelup.gitconnected.

com

Visualizing, Designing, and


Analyzing the Counting Sort
Algorithm.
Vikram Gupta

4-6 minutes

Sorting Algorithms

A complete analysis of Counting Sort


Algorithm.

Counting sort

This article is about visualizing, designing, and analyzing


the Counting Sort algorithm.
What Is Counting sort?

Counting sort is a non-comparison based sorting


algorithm that sorts the elements of an array by counting
the number of occurrences of each unique element in the
array. The count of each unique element is stored in an
auxiliary(count) array and the sorting is done by mapping
the count as an index of the auxiliary(count) array.

Counting sort works with whole numbers (non-negative


integers(0,1,...,n))as array indices start with 0, but it can
be modified to sort negative integers also.

Let’s Visualize the Counting Sort Algorithm


by Taking An example:

1. Find out the maximum element from the given array.

step 1

2. Initialize a count array of length max+1 with all elements


as 0. This array will be used for storing the count of the
elements of the input array.

step 2
3. Store the count of each unique element of the input
array to the count array at their respective indices.

Eg. The count of element 5 of the input array is 1, then at


index 5 in count array 1 is stored. Similarly, the count of
element 2 in the input array is 2, hence at index 2 in count
array 2 is stored.

step 3

4. Store the cumulative sum of the elements of the count


array. This will help in placing the elements of the input
array at the correct index in the output array.

step 4

5. Find the index of each element of the input array in the


count array. This gives the cumulative index. Place the
element at the index=(count-1) position in the output
array. Then decrement the count value by one.
step 5

Now Let’s Design the Counting Sort


Algorithm :

countingSort(inputArray, outputArray)
find the largest element in input array:- max
initialize the count array with all elements as zeros
for j = 0 to (size of input array -1)
find the total count of each unique element and
store the count at jth index in count array
for i = 0 to max
find the cumulative sum and store it in count array
itself
for j = (size-1) down to 0
copy the elements from the input array to the output
array
using the indices from count array.And each time an
element
is copied, decrement the corresponding count value.

Counting sort program

Complexity Analysis:

Time Complexity:

There are five ‘for’ loops in the counting sort method. The
number of times these loops are executed are listed
below:

• The first ‘for’ loop executes Θ(n) times.

• The second ‘for’ loop executes Θ(max) times.

• The third ‘for’ loop executes Θ(n) times.

• The fourth ‘for’ loop executes Θ(max) times.

• The fifth ‘for’ loop executes Θ(n) times.

Finally, we can say the total number of executions is →


Θ(n)+Θ(max)+Θ(n)+Θ(max)+Θ(n)=Θ(n+max) where n is
the size of the input array and max denotes the largest
element present in the input array.

In most of the scenarios, max will be the same as input


array size(n). Hence the counting sort algorithm has Θ(n)
complexity.

Note: Counting sort beats the lower bound of Ω(n lg n) of


comparison based sorting algorithm because it is not a
comparison sort.

Space Complexity:
The space complexity of the Counting sort is O(n). The
larger the range of elements, the larger is the space
complexity.

Is Counting Sort Stable?

Definition of stable : the relative order of elements with


the same value is maintained.

Counting sort is a stable algorithm.

As you can see in the above counting sort example the


elements with the same values preserved their
orders(positions) in the output array.

Is Counting Sort In-place?

It uses extra space for sorting the array elements (in this
case two arrays(counting and output arrays) are used),
hence it is not an In-place sorting algorithm.

Counting sort is not an In-place sorting algorithm.

That’s all for this article. Thank you for reading this article.
I hope, you have understood the Counting-sort algorithm
and its time and space complexities. You may read my
other useful articles on sorting data structure.

If you enjoyed this article, you can support my writing


directly by leaving a tip below or, even better, joining
Medium here. You’ll get access to all of my articles (that
include Java programming, Design Patterns, Data
Structures, Algorithms, SQL, GIT, Linux, Best

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy