0% found this document useful (0 votes)
17 views

Count Sort

Count sort is a sorting algorithm that sorts elements by counting the number of occurrences of each unique element in the input array. It involves: 1) Creating an array where the index corresponds to the unique values and the value at each index represents the count of each element 2) Incrementing the count for each element in the input array 3) Modifying the count array to be the cumulative sum, indicating each element's position in the sorted output array

Uploaded by

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

Count Sort

Count sort is a sorting algorithm that sorts elements by counting the number of occurrences of each unique element in the input array. It involves: 1) Creating an array where the index corresponds to the unique values and the value at each index represents the count of each element 2) Incrementing the count for each element in the input array 3) Modifying the count array to be the cumulative sum, indicating each element's position in the sorted output array

Uploaded by

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

Count Sort

A type of Sorting Technique


Presentation Members

 Muhammad Shehrooz (BSE_21208)


 Hamza Sajid (BSE_21219)
 Syed Umair Shahid (BSE_21224)
 Abdul Wahab Mudassar (BSE_21229)
Count Sort

Count Sort is a type of sorting method which:


 Sorts the numbers by using their “counts”.
 It is NOT a comparison method. Instead of comparing numbers with
each other, we rather count the number of occurrences of every
element/number.
Main Process Executed in Count Sort

The main process executed in Count Sort are:


1. Count the number of occurrences for each number/element.
2. Assigning the position to each number/element.
Count Sort Algorithm

The Algorithm of Count sort is defined by the following example:


 Store the User’s values in an Input Array.

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

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 2 4 4 5 7 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values Output 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

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 2 4 4 5 7 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values Output 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

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 2 4 4 5 6 8 8 8 9 Count Array

Subtract 1 from its value


Index 0 1 2 3 4 5 6 7 8

Values Output 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

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 2 4 4 5 6 8 8 8 9 Count Array

Now store in that index of Output Array

Index 0 1 2 3 4 5 6 7 8

Values Output 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

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 2 4 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 5 Output 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

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 2 4 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 5 Output 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

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 2 3 4 5 6 8 8 8 9 Count Array

Subtract 1 from its value

Index 0 1 2 3 4 5 6 7 8

Values 5 Output 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

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 2 3 4 5 6 8 8 8 9 Count Array

Now store in that index of Output Array

Index 0 1 2 3 4 5 6 7 8

Values 5 Output 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

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 2 3 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 2 5 Output 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

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 2 3 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 2 5 Output 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

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 1 3 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 2 5 Output 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

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 1 3 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 5 Output 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

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 1 3 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 5 Output 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

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 1 2 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 5 Output 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

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 1 2 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 2 5 Output 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

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 1 2 4 5 6 8 8 8 9 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 2 5 Output 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

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 1 2 4 5 6 8 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 2 5 Output 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

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 1 2 4 5 6 8 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 2 5 9 Output 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

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 1 2 4 5 6 8 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 2 5 9 Output 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

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 1 2 4 4 6 8 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 2 4 5 9 Output 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

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 1 2 4 4 6 8 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 2 2 4 5 9 Output 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

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 2 4 4 6 8 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 1 2 2 4 5 9 Output 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

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 2 4 4 6 8 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 1 2 2 4 5 9 Output 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

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 2 4 4 6 7 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 1 2 2 4 5 6 9 Output 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

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 2 4 4 6 7 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 1 2 2 4 5 6 9 Output 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

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 2 4 4 5 7 8 8 8 Count Array

Index 0 1 2 3 4 5 6 7 8

Values 1 1 2 2 4 5 5 6 9 Output Array


 Copy back the Elements from the Output Array to the original Input Array.
 Copy back the Elements from the Output Array to the original Input Array.

Index 0 1 2 3 4 5 6 7 8

Values 1 1 2 2 4 5 5 6 9 Output Array

Index 0 1 2 3 4 5 6 7 8

Values 5 2 1 2 9 4 1 6 5 Input Array


 Copy back the Elements from the Output Array to the original Input Array.

Index 0 1 2 3 4 5 6 7 8

Values 1 1 2 2 4 5 5 6 9 Output Array

Index 0 1 2 3 4 5 6 7 8

Values 1 1 2 2 4 5 5 6 9 Input Array


Code for Count Sort

 Here is the Code for Count Sort, implementing the same example as
discussed in the previous slides:
#include<iostream>
using namespace std;

void Count_Sort(int arr[] , int size)


{
int max = arr[0] ; // initialize a 'max' variable with the first value of the array

for(int i=1; i<size ; i++) // To find the maximum value


{
if(arr[i] > max)
max = arr[i] ;
}
int count[max+1] = {0} ; // creating and initializing the count array with 0

for(int i=0; i<size ; i++)


{
count[ arr[i] ]++ ; // counting each number in count array, from the original array
}

for(int i=1; i<=max ; i++)


{
count[i] = count[i] + count[i-1] ; // Cumulative Adding
}

int Output[size] ; // Creating an Output Array of length “size”

for(int i=0 ; i<size ; i++)


{
Output[--count[ arr[i] ]] = arr[i] ; // finding the position of a number and
placing that number in the Output array
}
for(int i=0 ; i<size ; i++)
{
arr[i] = Output[i] ; // Copying the elements from the Output Array to the
original array
}
}

int main()
{
int Input_Array[9] = {5,2,1,2,9,4,1,6,5} ; // creating and initializing an Input Array

Count_Sort(Input_Array , 9) ; // Calling the function

for(int i=0; i<9; i++)


{
cout << Input_Array[i] << " " ; // Printing The Array ;
}
}
Complexity of Count Sort

 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

 Best Case: When all elements are Equal.


{2, 2, 2, 2, 2}
 Worst Case: When the Range of elements is Massive
{1, 4, 2, 15000, 3}
 Average Case: O(N+K) , When Range & No. of elements are equally
dominant.
Range = 5
{ 3, 2, 7, 2, 5} No. of elements = 5
ANY QUESTIONS?
(But don’t ask )
Hamza Sajid
- The Slides Creator
Yours Truly, the Best in the World.
JAZAKALLA
H

You might also like