SlideShare a Scribd company logo
1 of 15
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

What's hot

Selection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexitySelection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexityComputer_ at_home
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common SubsequenceSwati Swati
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsMohamed Loey
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIMohamed Loey
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Floyd warshall algorithm
Floyd warshall algorithmFloyd warshall algorithm
Floyd warshall algorithmA. S. M. Shafi
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 

What's hot (20)

Algoritmo Counting sort
Algoritmo Counting sortAlgoritmo Counting sort
Algoritmo Counting sort
 
Selection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexitySelection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexity
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Subtraction of Integers
Subtraction of IntegersSubtraction of Integers
Subtraction of Integers
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
probability ppt
probability pptprobability ppt
probability ppt
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Floyd warshall algorithm
Floyd warshall algorithmFloyd warshall algorithm
Floyd warshall algorithm
 
Counting sort
Counting sortCounting sort
Counting sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Red black tree
Red black treeRed black tree
Red black tree
 

Similar to Counting sort

Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsSarvesh Rawat
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
06 Analysis of Algorithms: Sorting in Linear Time
06 Analysis of Algorithms:  Sorting in Linear Time06 Analysis of Algorithms:  Sorting in Linear Time
06 Analysis of Algorithms: Sorting in Linear TimeAndres Mendez-Vazquez
 
4.9 cpm network calculations
4.9 cpm network calculations4.9 cpm network calculations
4.9 cpm network calculationsVishal Tidake
 
Application of parallel hierarchical matrices and low-rank tensors in spatial...
Application of parallel hierarchical matrices and low-rank tensors in spatial...Application of parallel hierarchical matrices and low-rank tensors in spatial...
Application of parallel hierarchical matrices and low-rank tensors in spatial...Alexander Litvinenko
 
Butterfly Counting in Bipartite Networks
Butterfly Counting in Bipartite NetworksButterfly Counting in Bipartite Networks
Butterfly Counting in Bipartite NetworksSeyed-Vahid Sanei-Mehri
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modifiedInbok Lee
 
Obj. 7 Midpoint and Distance Formulas
Obj. 7 Midpoint and Distance FormulasObj. 7 Midpoint and Distance Formulas
Obj. 7 Midpoint and Distance Formulassmiller5
 
Form 5 Additional Maths Note
Form 5 Additional Maths NoteForm 5 Additional Maths Note
Form 5 Additional Maths NoteChek Wei Tan
 
parallel programming.ppt
parallel programming.pptparallel programming.ppt
parallel programming.pptnazimsattar
 
Wu Mamber (String Algorithms 2007)
Wu  Mamber (String Algorithms 2007)Wu  Mamber (String Algorithms 2007)
Wu Mamber (String Algorithms 2007)mailund
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordLakshmi Sarvani Videla
 

Similar to Counting sort (20)

Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
 
Linear Sorting
Linear SortingLinear Sorting
Linear Sorting
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
06 Analysis of Algorithms: Sorting in Linear Time
06 Analysis of Algorithms:  Sorting in Linear Time06 Analysis of Algorithms:  Sorting in Linear Time
06 Analysis of Algorithms: Sorting in Linear Time
 
4.9 cpm network calculations
4.9 cpm network calculations4.9 cpm network calculations
4.9 cpm network calculations
 
Application of parallel hierarchical matrices and low-rank tensors in spatial...
Application of parallel hierarchical matrices and low-rank tensors in spatial...Application of parallel hierarchical matrices and low-rank tensors in spatial...
Application of parallel hierarchical matrices and low-rank tensors in spatial...
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Butterfly Counting in Bipartite Networks
Butterfly Counting in Bipartite NetworksButterfly Counting in Bipartite Networks
Butterfly Counting in Bipartite Networks
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modified
 
3rd Semester Computer Science and Engineering (ACU) Question papers
3rd Semester Computer Science and Engineering  (ACU) Question papers3rd Semester Computer Science and Engineering  (ACU) Question papers
3rd Semester Computer Science and Engineering (ACU) Question papers
 
MATLAB ARRAYS
MATLAB ARRAYSMATLAB ARRAYS
MATLAB ARRAYS
 
7th Semester (June; July-2014) Mechanical Engineering Question Paper
7th Semester (June; July-2014) Mechanical Engineering Question Paper7th Semester (June; July-2014) Mechanical Engineering Question Paper
7th Semester (June; July-2014) Mechanical Engineering Question Paper
 
Obj. 7 Midpoint and Distance Formulas
Obj. 7 Midpoint and Distance FormulasObj. 7 Midpoint and Distance Formulas
Obj. 7 Midpoint and Distance Formulas
 
Form 5 Additional Maths Note
Form 5 Additional Maths NoteForm 5 Additional Maths Note
Form 5 Additional Maths Note
 
3rd Semester Computer Science and Engineering (ACU) Question papers
3rd Semester Computer Science and Engineering  (ACU) Question papers3rd Semester Computer Science and Engineering  (ACU) Question papers
3rd Semester Computer Science and Engineering (ACU) Question papers
 
parallel programming.ppt
parallel programming.pptparallel programming.ppt
parallel programming.ppt
 
3rd Semester Computer Science and Engineering (ACU - 2022) Question papers
3rd Semester Computer Science and Engineering  (ACU - 2022) Question papers3rd Semester Computer Science and Engineering  (ACU - 2022) Question papers
3rd Semester Computer Science and Engineering (ACU - 2022) Question papers
 
Wu Mamber (String Algorithms 2007)
Wu  Mamber (String Algorithms 2007)Wu  Mamber (String Algorithms 2007)
Wu Mamber (String Algorithms 2007)
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab Record
 

More from Imdad Ul Haq

How amazon web services uses formal methods
How amazon web services uses formal methodsHow amazon web services uses formal methods
How amazon web services uses formal methodsImdad Ul Haq
 
Software architacture recovery
Software architacture recoverySoftware architacture recovery
Software architacture recoveryImdad Ul Haq
 
Introduction to Function and there types
Introduction to Function and there typesIntroduction to Function and there types
Introduction to Function and there typesImdad Ul Haq
 
Enhance ERD(Entity Relationship Diagram)
Enhance ERD(Entity Relationship Diagram)Enhance ERD(Entity Relationship Diagram)
Enhance ERD(Entity Relationship Diagram)Imdad Ul Haq
 
V_Sound(Visualize Softwar and Understand the Document)
V_Sound(Visualize Softwar  and Understand the Document)V_Sound(Visualize Softwar  and Understand the Document)
V_Sound(Visualize Softwar and Understand the Document)Imdad Ul Haq
 
Ufone marketing oriantation
Ufone marketing oriantationUfone marketing oriantation
Ufone marketing oriantationImdad Ul Haq
 
Library management system
Library management systemLibrary management system
Library management systemImdad Ul Haq
 

More from Imdad Ul Haq (7)

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

Recently uploaded

chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Recently uploaded (20)

chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

Counting sort

  • 1.
  • 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.