3
Most read
13
Most read
14
Most read
Counting sort
TODAY’S
TOPIC ABOUT
COUNTING SORT
BY:SYED SALAHUDDIN AHMED
AND
IMDAD-UL-HAQ
COUNTING SORT
Counting sort is a sorting technique based on keys between
a specific range. It works by counting the number of objects
having distinct key values (kind of hashing). Then doing
some arithmetic to calculate the position of each object in
the output sequence.
In this sorting we didn’t compare elements while sorting.
Counting sort assumes that each of the n input elements is an integer
in the range 0 to k. that is n is the number of elements and k is the
highest value element.
Consider the input set : 4, 1, 3, 4, 3. Then n=5 and k=4.
The algorithm uses three array:
Input Array: A[1..n] store input data where A[j] ∈ {1, 2, 3, …, k}
Output Array: B[1..n] finally store the sorted data
Temporary Array: C[0..k] store data temporarily
1. Counting-Sort(A, B, k)
2. Let C[0…..k] be a new array
3. for i=0 to k [Loop 1]
4. C[i]= 0;
5. for j=1 to A.length( or n) [Loop 2]
6. C[ A[j] ] = C[ A[j] ] + 1;
7. for i=1 to k [Loop 3]
8. C[i] = C[i] + C[i-1];
9. for j=n or A.length down to 1 [Loop 4]
10. B[ C[ A[j] ] ] = A[j];
11. C[ A[j] ] = C[ A[j] ] - 1;
[LOOP1]
For i=0 to k
c[i]=0;
2 5 3 0 2 3 0 3
0 0 0 0 0 0
B:
C:
A:
1 2 3 4 5 6 7 8
0 1 2 3 4 5
1 2 3 4 5 6 7 8
[Loop 2]
5. for j=1 to A.length( or n)
6. C[ A[j] ] = C[ A[j] ] + 1;
2 5 3 0 2 3 0 3
2 0 2 3 0 1
B:
C:
A:
1 2 3 4 5 6 7 8
0 1 2 3 4 5
1 2 3 4 5 6 7 8
[Loop 3]
7. for i=1 to K
8. C[i] = C[i] + C[i-1];
2 5 3 0 2 3 0 3
2 0 2 3 0 1
C:
C:
A:
0 1 2 3 4 5
1 2 3 4 5 6 7 8
2 2 2 3 0 1
0 1 2 3 4 5
[Loop 3]
7. for i=1 to K
8. C[i] = C[i] + C[i-1];
2 5 3 0 2 3 0 3
2 0 2 3 0 1
C:
C:
A:
0 1 2 3 4 5
1 2 3 4 5 6 7 8
2 2 4 7 7 8
0 1 2 3 4 5
9. for j=n or A.length down to 1
10. B[ C[ A[j] ] ] = A[j];
11. C[ A[j] ] = C[ A[j] ] - 1;
2 5 3 0 2 3 0 3
B:
C:
A:
0 1 2 3 4 5
1 2 3 4 5 6 7 8
2 2 4 7 7 8
[Loop 4]
1 2 3 4 5 6 7 8
3
3
7
7
3
9. for j=n or A.length down to 1
10. B[ C[ A[j] ] ] = A[j];
11. C[ A[j] ] = C[ A[j] ] - 1;
2 5 3 0 2 3 0 3
B:
C:
A:
0 1 2 3 4 5
1 2 3 4 5 6 7 8
2 2 4 6 7 8
[Loop 4]
3
1 2 3 4 5 6 7 8
0
0
2
2
0
END OF LOOP [Loop 4]
2 5 3 0 2 3 0 3
0 2 2 4 7 7
B:
C:
A:
0 1 2 3 4 5
1 2 3 4 5 6 7 8
0 0 2 2 3 3 3 5
1 2 3 4 5 6 7 8
TIME COMPLEXITY
• So the counting sort takes a total time of: O(n
+ k)
•Pro’s:
• –Fast
• –Asymptotically fast - O(n+k)
• –Simple to code
• Con’s:
•–Doesn’t sort in place.
• –Requires O(n+k) extra storage.
TIME COMPLEXITY OF SORTING
Thank You

More Related Content

PPTX
Sorting Algorithms
PPTX
Counting Sort
PPTX
Counting sort
PPTX
Counting sort
PPTX
Registers
PPTX
Nand gate
PPTX
Binary Tree in Data Structure
PPTX
Harvard architecture
Sorting Algorithms
Counting Sort
Counting sort
Counting sort
Registers
Nand gate
Binary Tree in Data Structure
Harvard architecture

What's hot (20)

PPTX
Merge Sort
PPTX
heap Sort Algorithm
PDF
Binary tree
PPT
Addressing modes
PPTX
Merge sort algorithm power point presentation
PPTX
The bus interface unit (biu)
PPTX
Arithmetic micro operations
PPTX
Octal to binary encoder
PPT
Greedy algorithms
PPT
Addressing modes
PPT
DATA REPRESENTATION
PDF
Double ended queue
PPTX
Circular Queue data structure
PPTX
linked list in data structure
PPT
Merge sort
PPT
SEARCHING AND SORTING ALGORITHMS
PPT
1.4 expression tree
PPTX
Stacks IN DATA STRUCTURES
PPTX
Priority Queue in Data Structure
Merge Sort
heap Sort Algorithm
Binary tree
Addressing modes
Merge sort algorithm power point presentation
The bus interface unit (biu)
Arithmetic micro operations
Octal to binary encoder
Greedy algorithms
Addressing modes
DATA REPRESENTATION
Double ended queue
Circular Queue data structure
linked list in data structure
Merge sort
SEARCHING AND SORTING ALGORITHMS
1.4 expression tree
Stacks IN DATA STRUCTURES
Priority Queue in Data Structure
Ad

Similar to Counting sort (20)

PPT
Counting Sort and Radix Sort Algorithms
PPT
Counting sort(Non Comparison Sort)
PDF
06 Analysis of Algorithms: Sorting in Linear Time
PPTX
Linear Sorting
PPT
chapter-8.ppt
PPTX
Sorting ppt
PDF
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
PPT
Top school in delhi ncr
PDF
Sienna 8 countingsorts
PPTX
Algorithms - Rocksolid Tour 2013
PPTX
Advance Algorithm_unit_2_czcbcnhgjy.pptx
PPT
Introduction
PPT
Algorithm Design and Analysis
PPT
Algorithm.ppt
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
PPT
MergesortQuickSort.ppt
PPT
5.2 divide and conquer
PPT
03 dc
PPT
Divide and conquer
PPT
Mergesort
Counting Sort and Radix Sort Algorithms
Counting sort(Non Comparison Sort)
06 Analysis of Algorithms: Sorting in Linear Time
Linear Sorting
chapter-8.ppt
Sorting ppt
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Top school in delhi ncr
Sienna 8 countingsorts
Algorithms - Rocksolid Tour 2013
Advance Algorithm_unit_2_czcbcnhgjy.pptx
Introduction
Algorithm Design and Analysis
Algorithm.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
MergesortQuickSort.ppt
5.2 divide and conquer
03 dc
Divide and conquer
Mergesort
Ad

More from Imdad Ul Haq (7)

PPTX
How amazon web services uses formal methods
PPTX
Software architacture recovery
PPTX
Introduction to Function and there types
PPTX
Enhance ERD(Entity Relationship Diagram)
DOCX
V_Sound(Visualize Softwar and Understand the Document)
DOCX
Ufone marketing oriantation
PPTX
Library management system
How amazon web services uses formal methods
Software architacture recovery
Introduction to Function and there types
Enhance ERD(Entity Relationship Diagram)
V_Sound(Visualize Softwar and Understand the Document)
Ufone marketing oriantation
Library management system

Recently uploaded (20)

PDF
AI Guide for Business Growth - Arna Softech
PDF
Website Design Services for Small Businesses.pdf
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PPTX
most interesting chapter in the world ppt
PPTX
Matchmaking for JVMs: How to Pick the Perfect GC Partner
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
PDF
Salesforce Agentforce AI Implementation.pdf
PPTX
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
PDF
CCleaner 6.39.11548 Crack 2025 License Key
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
PPTX
Introduction to Windows Operating System
DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PPTX
Cybersecurity: Protecting the Digital World
PDF
Wondershare Recoverit Full Crack New Version (Latest 2025)
PPTX
Trending Python Topics for Data Visualization in 2025
PPTX
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
PDF
Practical Indispensable Project Management Tips for Delivering Successful Exp...
AI Guide for Business Growth - Arna Softech
Website Design Services for Small Businesses.pdf
iTop VPN Crack Latest Version Full Key 2025
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
most interesting chapter in the world ppt
Matchmaking for JVMs: How to Pick the Perfect GC Partner
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
Advanced SystemCare Ultimate Crack + Portable (2025)
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
Salesforce Agentforce AI Implementation.pdf
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
CCleaner 6.39.11548 Crack 2025 License Key
How to Use SharePoint as an ISO-Compliant Document Management System
Introduction to Windows Operating System
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
Cybersecurity: Protecting the Digital World
Wondershare Recoverit Full Crack New Version (Latest 2025)
Trending Python Topics for Data Visualization in 2025
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
Practical Indispensable Project Management Tips for Delivering Successful Exp...

Counting sort

  • 2. TODAY’S TOPIC ABOUT COUNTING SORT BY:SYED SALAHUDDIN AHMED AND IMDAD-UL-HAQ
  • 3. COUNTING SORT Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. In this sorting we didn’t compare elements while sorting.
  • 4. Counting sort assumes that each of the n input elements is an integer in the range 0 to k. that is n is the number of elements and k is the highest value element. Consider the input set : 4, 1, 3, 4, 3. Then n=5 and k=4. The algorithm uses three array: Input Array: A[1..n] store input data where A[j] ∈ {1, 2, 3, …, k} Output Array: B[1..n] finally store the sorted data Temporary Array: C[0..k] store data temporarily
  • 5. 1. Counting-Sort(A, B, k) 2. Let C[0…..k] be a new array 3. for i=0 to k [Loop 1] 4. C[i]= 0; 5. for j=1 to A.length( or n) [Loop 2] 6. C[ A[j] ] = C[ A[j] ] + 1; 7. for i=1 to k [Loop 3] 8. C[i] = C[i] + C[i-1]; 9. for j=n or A.length down to 1 [Loop 4] 10. B[ C[ A[j] ] ] = A[j]; 11. C[ A[j] ] = C[ A[j] ] - 1;
  • 6. [LOOP1] For i=0 to k c[i]=0; 2 5 3 0 2 3 0 3 0 0 0 0 0 0 B: C: A: 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 4 5 6 7 8
  • 7. [Loop 2] 5. for j=1 to A.length( or n) 6. C[ A[j] ] = C[ A[j] ] + 1; 2 5 3 0 2 3 0 3 2 0 2 3 0 1 B: C: A: 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 4 5 6 7 8
  • 8. [Loop 3] 7. for i=1 to K 8. C[i] = C[i] + C[i-1]; 2 5 3 0 2 3 0 3 2 0 2 3 0 1 C: C: A: 0 1 2 3 4 5 1 2 3 4 5 6 7 8 2 2 2 3 0 1 0 1 2 3 4 5
  • 9. [Loop 3] 7. for i=1 to K 8. C[i] = C[i] + C[i-1]; 2 5 3 0 2 3 0 3 2 0 2 3 0 1 C: C: A: 0 1 2 3 4 5 1 2 3 4 5 6 7 8 2 2 4 7 7 8 0 1 2 3 4 5
  • 10. 9. for j=n or A.length down to 1 10. B[ C[ A[j] ] ] = A[j]; 11. C[ A[j] ] = C[ A[j] ] - 1; 2 5 3 0 2 3 0 3 B: C: A: 0 1 2 3 4 5 1 2 3 4 5 6 7 8 2 2 4 7 7 8 [Loop 4] 1 2 3 4 5 6 7 8 3 3 7 7 3
  • 11. 9. for j=n or A.length down to 1 10. B[ C[ A[j] ] ] = A[j]; 11. C[ A[j] ] = C[ A[j] ] - 1; 2 5 3 0 2 3 0 3 B: C: A: 0 1 2 3 4 5 1 2 3 4 5 6 7 8 2 2 4 6 7 8 [Loop 4] 3 1 2 3 4 5 6 7 8 0 0 2 2 0
  • 12. END OF LOOP [Loop 4] 2 5 3 0 2 3 0 3 0 2 2 4 7 7 B: C: A: 0 1 2 3 4 5 1 2 3 4 5 6 7 8 0 0 2 2 3 3 3 5 1 2 3 4 5 6 7 8
  • 13. TIME COMPLEXITY • So the counting sort takes a total time of: O(n + k) •Pro’s: • –Fast • –Asymptotically fast - O(n+k) • –Simple to code • Con’s: •–Doesn’t sort in place. • –Requires O(n+k) extra storage.