SlideShare a Scribd company logo
2
Most read
5
Most read
8
Most read
COUNTING SORT
INTRODUCTION
• It is an integer sorting algorithm. It operates by counting the
number of objects that have each distinct key value, and using
arithmetic on those counts to determine the positions of each key
value in the output sequence
• Counting sort uses key values as indexes into an array, it is not a
comparison sort.
INPUT AND OUTPUT ASSUMPTIONS
• The input to counting sort consists of a collection of n items, each of which
has a non-negative integer key whose maximum value is at most k
• The input to be sorted is assumed to be more simply a sequence of integers
itself,but this simplification does not accommodate many applications of
counting sort. For instance, when used as a subroutine in radix sort, the keys
for each call to counting sort are individual digits of larger item keys; it would
not suffice to return only a sorted list of the key digits, separated from the
items.
• The output is an array of the items, in order by their keys. Because
of the application to radix sorting, it is important for counting sort
to be a stable sort: if two items have the same key as each other,
they should have the same relative position in the output as they
did in the input
Counting sort
Counting sort
TIME COMPLEXITY
• The algorithm uses only simple for loops, without recursion or
subroutine calls, it is straightforward to analyze. The initialization
of the count array, and the second for loop which performs a prefix
sum on the count array, each iterate at most k + 1 times and
therefore take O(k) time. The other two for loops, and the
initialization of the output array, each take O(n) time. Therefore,
the time for the whole algorithm is the sum of the times for these
steps, O(n + k).
SPACE COMPLEXITY
• It uses arrays of length k + 1 and n, the total space usage of the
algorithm is also O(n + k). For problem instances in which the
maximum key value is significantly smaller than the number of
items, counting sort can be highly space-efficient, as the only
storage it uses other than its input and output arrays is the Count
array which uses space O(k).
THANK YOU !!!

More Related Content

PPTX
Binary search
PPTX
Counting sort
PPTX
Counting Sort
PPTX
Bucket sort
PPT
PPTX
Bucket sort
PPT
Counting sort(Non Comparison Sort)
PDF
Sorting Algorithms
Binary search
Counting sort
Counting Sort
Bucket sort
Bucket sort
Counting sort(Non Comparison Sort)
Sorting Algorithms

What's hot (20)

PPT
Selection sort
PPTX
Quick sort
PDF
Quick Sort , Merge Sort , Heap Sort
PPTX
Linear search-and-binary-search
PPTX
Ppt on Linked list,stack,queue
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPT
Searching algorithm
PPT
3.2 insertion sort
PDF
Rabin karp string matcher
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Counting sort
PPTX
SORTING techniques.pptx
PPT
Prim's Algorithm on minimum spanning tree
PDF
linear search and binary search
PPTX
Quadratic probing
PPSX
Data structure stack&queue basics
PPTX
Ppt bubble sort
PPTX
Insertion sort
PDF
Lecture 07 Data Structures - Basic Sorting
PPTX
Radix sort presentation
Selection sort
Quick sort
Quick Sort , Merge Sort , Heap Sort
Linear search-and-binary-search
Ppt on Linked list,stack,queue
Data Structures - Lecture 8 [Sorting Algorithms]
Searching algorithm
3.2 insertion sort
Rabin karp string matcher
BINARY TREE REPRESENTATION.ppt
Counting sort
SORTING techniques.pptx
Prim's Algorithm on minimum spanning tree
linear search and binary search
Quadratic probing
Data structure stack&queue basics
Ppt bubble sort
Insertion sort
Lecture 07 Data Structures - Basic Sorting
Radix sort presentation
Ad

Similar to Counting sort (20)

PPTX
Advance Algorithm_unit_2_czcbcnhgjy.pptx
PDF
06 Analysis of Algorithms: Sorting in Linear Time
PPT
Sorting Seminar Presentation by Ashin Guha Majumder
PPTX
Sorting ppt
PDF
Sienna 8 countingsorts
PDF
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
PPTX
SORTTING IN LINEAR TIME - Radix Sort
PPT
Introduction
PPTX
ppt2- sorting data in design and anlysis.pptx
ODP
Intro to Sorting + Insertion Sort
PPT
Cis435 week06
PDF
INDEX SORT
PDF
Binary Sort
PPT
lecture 9
PPSX
Lecture 2 data structures & algorithms - sorting techniques
PDF
Elementary algorithms
PPTX
Linear Sorting
PPT
Algorithm Design and Analysis
Advance Algorithm_unit_2_czcbcnhgjy.pptx
06 Analysis of Algorithms: Sorting in Linear Time
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting ppt
Sienna 8 countingsorts
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
SORTTING IN LINEAR TIME - Radix Sort
Introduction
ppt2- sorting data in design and anlysis.pptx
Intro to Sorting + Insertion Sort
Cis435 week06
INDEX SORT
Binary Sort
lecture 9
Lecture 2 data structures & algorithms - sorting techniques
Elementary algorithms
Linear Sorting
Algorithm Design and Analysis
Ad

Recently uploaded (20)

PDF
oil_refinery_presentation_v1 sllfmfls.pdf
PPT
First Aid Training Presentation Slides.ppt
PPTX
The spiral of silence is a theory in communication and political science that...
PDF
Swiggy’s Playbook: UX, Logistics & Monetization
PDF
Tunisia's Founding Father(s) Pitch-Deck 2022.pdf
DOCX
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
PPTX
water for all cao bang - a charity project
PPTX
Human Mind & its character Characteristics
PPTX
worship songs, in any order, compilation
PPTX
2025-08-10 Joseph 02 (shared slides).pptx
PPTX
Intro to ISO 9001 2015.pptx wareness raising
DOCX
ENGLISH PROJECT FOR BINOD BIHARI MAHTO KOYLANCHAL UNIVERSITY
PPT
The Effect of Human Resource Management Practice on Organizational Performanc...
PPTX
_ISO_Presentation_ISO 9001 and 45001.pptx
PPTX
PHIL.-ASTRONOMY-AND-NAVIGATION of ..pptx
DOC
学位双硕士UTAS毕业证,墨尔本理工学院毕业证留学硕士毕业证
PDF
Presentation1 [Autosaved].pdf diagnosiss
PDF
Parts of Speech Prepositions Presentation in Colorful Cute Style_20250724_230...
PPTX
English-9-Q1-3-.pptxjkshbxnnxgchchxgxhxhx
PPTX
Tablets And Capsule Preformulation Of Paracetamol
oil_refinery_presentation_v1 sllfmfls.pdf
First Aid Training Presentation Slides.ppt
The spiral of silence is a theory in communication and political science that...
Swiggy’s Playbook: UX, Logistics & Monetization
Tunisia's Founding Father(s) Pitch-Deck 2022.pdf
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
water for all cao bang - a charity project
Human Mind & its character Characteristics
worship songs, in any order, compilation
2025-08-10 Joseph 02 (shared slides).pptx
Intro to ISO 9001 2015.pptx wareness raising
ENGLISH PROJECT FOR BINOD BIHARI MAHTO KOYLANCHAL UNIVERSITY
The Effect of Human Resource Management Practice on Organizational Performanc...
_ISO_Presentation_ISO 9001 and 45001.pptx
PHIL.-ASTRONOMY-AND-NAVIGATION of ..pptx
学位双硕士UTAS毕业证,墨尔本理工学院毕业证留学硕士毕业证
Presentation1 [Autosaved].pdf diagnosiss
Parts of Speech Prepositions Presentation in Colorful Cute Style_20250724_230...
English-9-Q1-3-.pptxjkshbxnnxgchchxgxhxhx
Tablets And Capsule Preformulation Of Paracetamol

Counting sort

  • 2. INTRODUCTION • It is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence • Counting sort uses key values as indexes into an array, it is not a comparison sort.
  • 3. INPUT AND OUTPUT ASSUMPTIONS • The input to counting sort consists of a collection of n items, each of which has a non-negative integer key whose maximum value is at most k • The input to be sorted is assumed to be more simply a sequence of integers itself,but this simplification does not accommodate many applications of counting sort. For instance, when used as a subroutine in radix sort, the keys for each call to counting sort are individual digits of larger item keys; it would not suffice to return only a sorted list of the key digits, separated from the items.
  • 4. • The output is an array of the items, in order by their keys. Because of the application to radix sorting, it is important for counting sort to be a stable sort: if two items have the same key as each other, they should have the same relative position in the output as they did in the input
  • 7. TIME COMPLEXITY • The algorithm uses only simple for loops, without recursion or subroutine calls, it is straightforward to analyze. The initialization of the count array, and the second for loop which performs a prefix sum on the count array, each iterate at most k + 1 times and therefore take O(k) time. The other two for loops, and the initialization of the output array, each take O(n) time. Therefore, the time for the whole algorithm is the sum of the times for these steps, O(n + k).
  • 8. SPACE COMPLEXITY • It uses arrays of length k + 1 and n, the total space usage of the algorithm is also O(n + k). For problem instances in which the maximum key value is significantly smaller than the number of items, counting sort can be highly space-efficient, as the only storage it uses other than its input and output arrays is the Count array which uses space O(k).