Professional Documents
Culture Documents
Count Sort
Count Sort
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
Create a Count Array, which has as many index numbers as the highest
value in the Input Array.
Initialize the Count Array with 0.
Index 0 1 2 3 4 5 6 7 8 9
Values
Count Array
Highest value was 9, so
the “count array” has 0-
to-9 indexes.
Create a Count Array, which has as many index numbers as the highest
value in the Input Array.
Initialize the Count Array with 0.
Index 0 1 2 3 4 5 6 7 8 9
Values
Count Array
Highest value was 9, so
the “count array” has 0-
to-9 indexes.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 0 0 0 0 0 0 0 0 0
Count Array
Use the count array to store the number of occurrence of each element.
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
Index 0 1 2 3 4 5 6 7 8 9
Values 0 0 0 0 0 0 0 0 0 0
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 0 0 0 0 1 0 0 0 0
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 0 1 0 0 1 0 0 0 0
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 1 1 0 0 1 0 0 0 0
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 1 2 0 0 1 0 0 0 0
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 1 2 0 0 1 0 0 0 1
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 1 2 0 1 1 0 0 0 1
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 1 0 0 0 1
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 1 1 0 0 1
Count Array
Use the count array to store the number of occurrence of each element.
Index 0 1 2 3 4 5 6 7 8
Values 5 2 1 2 9 4 1 6 5
Input Array
+1
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
(Modified)
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 4 0 1 2 1 0 0 1
(Modified)
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 4 4 1 2 1 0 0 1
(Modified)
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 4 4 5 2 1 0 0 1
(Modified)
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 4 4 5 7 1 0 0 1
(Modified)
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 4 4 5 7 8 0 0 1
(Modified)
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 4 4 5 7 8 8 0 1
(Modified)
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 4 4 5 7 8 8 8 1
(Modified)
Count Array
Modify the count array by adding the value of the previous index in the current index
(Cumulative). The updated count array now tells us about the position of each
element/number in the sorted array.
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 2 0 1 2 1 0 0 1
Count Array
+
Index 0 1 2 3 4 5 6 7 8 9
Values 0 2 4 4 5 7 8 8 8 9
(Modified)
Count Array
Create an Output Array (having the same size as the Input Array) to store
the elements in a sorted order. We will sort the numbers with the help of the
Count Array.
Create an Output Array (having the same size as the Input Array) to store
the elements in a sorted order. We will sort the numbers with the help of the
Count Array.
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8 9
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8
Index 0 1 2 3 4 5 6 7 8
Here is the Code for Count Sort, implementing the same example as
discussed in the previous slides:
#include<iostream>
using namespace std;
int main()
{
int Input_Array[9] = {5,2,1,2,9,4,1,6,5} ; // creating and initializing an Input Array
Time Complexity:
O (N+K)
Space Complexity:
O (K)
Where,
N = No. of Elements
K = Range of Elements (Highest value – Lowest Value)
Cases of Count Sort