SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
Sunawar Khan
Islamia University of Bahawalpur
Bahawalnagar Campus
Analysis o f Algorithm
‫خان‬ ‫سنور‬ Algorithm Analysis
Counting Sort
Linear Time Sorting
Bucket Sort
Radix Sort
Contents
‫خان‬ ‫سنور‬ Algorithm Analysis
Linear Time Sorting
• There are sorting algorithms that run faster than O(n lg n) time but
they require special assumptions about the input sequence to be sort.
• Examples of sorting algorithms that run in linear time are counting
sort, radix sort and bucket sort.
• Counting sort and radix sort assume that the input consists of integers
in a small range. Whereas, bucket sort assumes that the input is
generated by a random process that distributes elements uniformly
over the interval.
• Since we already know that the best comparison-based sorting can to
is Ω(n lg n).
• It is not difficult to figure out that linear-time sorting algorithms use
operations other than comparisons to determine the sorted order.
‫خان‬ ‫سنور‬ Algorithm Analysis
Linear Time Sorting
• Despite of linear time usually these algorithms are not very desirable
from practical point of view. Firstly, the efficiency of linear-time
algorithms depend on the keys randomly ordered.
• If this condition is not satisfied, the result is the degrading in
performance. Secondly, these algorithms require extra space
proportional to the size of the array being sorted, so if we are dealing
with large file, extra array becomes a real liability. Thirdly, the
"inner-loop" of these algorithms contain quite a few instructions, so
even though they are linear, they would not be as faster than quick
sort (say).
‫خان‬ ‫سنور‬ Algorithm Analysis
Counting Sort
Runs in (n) time. . .
‫خان‬ ‫سنور‬ Algorithm Analysis
Counting Sort
• Counting sort assumes that each of the elements is an integer in the
range 1 to k, for some integer k. When k = O(n), the Counting-sort
runs in O(n) time.
• The basic idea of Counting sort is to determine, for each input
elements x, the number of elements less than x. This information can
be used to place directly into its correct position.
• For example, if there 17 elements less than x, than x belongs in
output position 18.
• In the code for Counting sort, we are given array A[1 . . n] of
length n. We required two more arrays, the array B[1 . . n] holds the
sorted output and the array c[1 . . k] provides temporary working
storage.
‫خان‬ ‫سنور‬ Algorithm Analysis
Implementation
1. for i ← 0 to k do
2. c[i] ← 0
3. for j ← 0 to n do
4. c[A[j]] ← c[A[j]] + 1
5. //c[i] now contains the number of elements equal to i
6. for i ← 1 to k do
7. c[i] ← c[i] + c[i-1]
8. // c[i] now contains the number of elements ≤ i
9. for j ← n-1 downto 0 do
10. B[c[A[i]]] ← A[j]
11. c[A[i]] ← c[A[j]] - 1
‫خان‬ ‫سنور‬ Algorithm Analysis
Running Example
1. for i ← 1 to k do
2. c[i] ← 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5 9 4 5 3 6 8 5 4 8 7 4 6 9 7
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
A
C
‫خان‬ ‫سنور‬ Algorithm Analysis
Running Example
3. for j ← 0 to n do
4. c[A[j]] ← c[A[j]] + 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5 9 4 5 3 6 8 5 4 8 7 4 6 9 7
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 1 0 0 0 0
A
C
J 0
A{j} 5
C{A{j}} 1
‫خان‬ ‫سنور‬ Algorithm Analysis
Running Example
3. for j ← 0 to n do
4. c[A[j]] ← c[A[j]] + 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5 9 4 5 3 6 8 5 4 8 7 4 6 9 7
0 1 2 3 4 5 6 7 8 9
0 0 0 0 1 1 0 0 0 1
A
C
J 0 1 2
A{j} 5 9 4
C{A{j}} 1 1 1
Continue till end. . .
‫خان‬ ‫سنور‬ Algorithm Analysis
Running Example
3. for j ← 0 to n do
4. c[A[j]] ← c[A[j]] + 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5 9 4 5 3 6 8 5 4 8 7 4 6 9 7
0 1 2 3 4 5 6 7 8 9
0 0 0 1 3 3 2 2 2 2
A
C
‫خان‬ ‫سنور‬ Algorithm Analysis
Running Example
6. for i ← 1 to k do
7. c[i] ← c[i] + c[i-1]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5 9 4 5 3 6 8 5 4 8 7 4 6 9 7
0 1 2 3 4 5 6 7 8 9
0 0 0 1 3 3 2 2 2 2
A
C
i 1 2 3 4 5 6 7 8 9 10
c[i] 0
c[i-1] 0
C[i]+c[i-1] 0
‫خان‬ ‫سنور‬ Algorithm Analysis
Running Example
6. for i ← 1 to k do
7. c[i] ← c[i] + c[i-1]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5 9 4 5 3 6 8 5 4 8 7 4 6 9 7
0 1 2 3 4 5 6 7 8 9
0 0 0 1 3 3 2 2 2 2
A
C
i 1 2 3 4 5 6 7 8 9 10
c[i] 0 0
c[i-1] 0 0
C[i]+c[i-1] 0 0
‫خان‬ ‫سنور‬ Algorithm Analysis
Running Example
6. for i ← 1 to k do
7. c[i] ← c[i] + c[i-1]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5 9 4 5 3 6 8 5 4 8 7 4 6 9 7
0 1 2 3 4 5 6 7 8 9
0 0 0 1 3 3 2 2 2 2
A
C
i 1 2 3 4 5 6 7 8 9 10
c[i] 0 0 1
c[i-1] 0 0 0
C[i]+c[i-1] 0 0 1

Contenu connexe

Tendances

Tendances (19)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Quick sort
Quick sortQuick sort
Quick sort
 
1 lesson 6 introduction to radical functions
1 lesson 6 introduction to radical functions1 lesson 6 introduction to radical functions
1 lesson 6 introduction to radical functions
 
Sorting
SortingSorting
Sorting
 
Quick sort
Quick sortQuick sort
Quick sort
 
Q#8
Q#8Q#8
Q#8
 
Counting sort
Counting sortCounting sort
Counting sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Introduction to FAST-LAMP
Introduction to FAST-LAMPIntroduction to FAST-LAMP
Introduction to FAST-LAMP
 
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
Unit 5 dsa QUEUE
 
Overriding methods
Overriding methodsOverriding methods
Overriding methods
 
Quick sort
Quick sortQuick sort
Quick sort
 
นางสาว อริยาภรณ์ ภูมิตินทรีย์ ชั้น ม
นางสาว อริยาภรณ์ ภูมิตินทรีย์ ชั้น มนางสาว อริยาภรณ์ ภูมิตินทรีย์ ชั้น ม
นางสาว อริยาภรณ์ ภูมิตินทรีย์ ชั้น ม
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
[01] Quantum Error Correction for Beginners
[01] Quantum Error Correction for Beginners [01] Quantum Error Correction for Beginners
[01] Quantum Error Correction for Beginners
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 

Similaire à Linear Time Sorting Algorithms

Similaire à Linear Time Sorting Algorithms (20)

DAA Notes.pdf
DAA Notes.pdfDAA Notes.pdf
DAA Notes.pdf
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
AI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxAI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptx
 
Lec2-Array.pptx
Lec2-Array.pptxLec2-Array.pptx
Lec2-Array.pptx
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Merge sort
Merge sortMerge sort
Merge sort
 
Fine Grained Complexity
Fine Grained ComplexityFine Grained Complexity
Fine Grained Complexity
 
Clustering.pptx
Clustering.pptxClustering.pptx
Clustering.pptx
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 

Plus de International Islamic University (20)

Hash tables
Hash tablesHash tables
Hash tables
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Graph 1
Graph 1Graph 1
Graph 1
 
Graph 2
Graph 2Graph 2
Graph 2
 
Graph 3
Graph 3Graph 3
Graph 3
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Facial Expression Recognitino
Facial Expression RecognitinoFacial Expression Recognitino
Facial Expression Recognitino
 
Lecture#4
Lecture#4Lecture#4
Lecture#4
 
Lecture#3
Lecture#3 Lecture#3
Lecture#3
 
Lecture#2
Lecture#2 Lecture#2
Lecture#2
 
Case study
Case studyCase study
Case study
 
Arrays
ArraysArrays
Arrays
 
Pcb
PcbPcb
Pcb
 
Data transmission
Data transmissionData transmission
Data transmission
 
Basic organization of computer
Basic organization of computerBasic organization of computer
Basic organization of computer
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Fundamentals of-algorithm
Fundamentals of-algorithmFundamentals of-algorithm
Fundamentals of-algorithm
 
Fundamentals of-algorithm
Fundamentals of-algorithmFundamentals of-algorithm
Fundamentals of-algorithm
 
What is computer
What is computerWhat is computer
What is computer
 

Dernier

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 

Dernier (20)

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 

Linear Time Sorting Algorithms

  • 1. Sunawar Khan Islamia University of Bahawalpur Bahawalnagar Campus Analysis o f Algorithm
  • 2. ‫خان‬ ‫سنور‬ Algorithm Analysis Counting Sort Linear Time Sorting Bucket Sort Radix Sort Contents
  • 3. ‫خان‬ ‫سنور‬ Algorithm Analysis Linear Time Sorting • There are sorting algorithms that run faster than O(n lg n) time but they require special assumptions about the input sequence to be sort. • Examples of sorting algorithms that run in linear time are counting sort, radix sort and bucket sort. • Counting sort and radix sort assume that the input consists of integers in a small range. Whereas, bucket sort assumes that the input is generated by a random process that distributes elements uniformly over the interval. • Since we already know that the best comparison-based sorting can to is Ω(n lg n). • It is not difficult to figure out that linear-time sorting algorithms use operations other than comparisons to determine the sorted order.
  • 4. ‫خان‬ ‫سنور‬ Algorithm Analysis Linear Time Sorting • Despite of linear time usually these algorithms are not very desirable from practical point of view. Firstly, the efficiency of linear-time algorithms depend on the keys randomly ordered. • If this condition is not satisfied, the result is the degrading in performance. Secondly, these algorithms require extra space proportional to the size of the array being sorted, so if we are dealing with large file, extra array becomes a real liability. Thirdly, the "inner-loop" of these algorithms contain quite a few instructions, so even though they are linear, they would not be as faster than quick sort (say).
  • 5. ‫خان‬ ‫سنور‬ Algorithm Analysis Counting Sort Runs in (n) time. . .
  • 6. ‫خان‬ ‫سنور‬ Algorithm Analysis Counting Sort • Counting sort assumes that each of the elements is an integer in the range 1 to k, for some integer k. When k = O(n), the Counting-sort runs in O(n) time. • The basic idea of Counting sort is to determine, for each input elements x, the number of elements less than x. This information can be used to place directly into its correct position. • For example, if there 17 elements less than x, than x belongs in output position 18. • In the code for Counting sort, we are given array A[1 . . n] of length n. We required two more arrays, the array B[1 . . n] holds the sorted output and the array c[1 . . k] provides temporary working storage.
  • 7. ‫خان‬ ‫سنور‬ Algorithm Analysis Implementation 1. for i ← 0 to k do 2. c[i] ← 0 3. for j ← 0 to n do 4. c[A[j]] ← c[A[j]] + 1 5. //c[i] now contains the number of elements equal to i 6. for i ← 1 to k do 7. c[i] ← c[i] + c[i-1] 8. // c[i] now contains the number of elements ≤ i 9. for j ← n-1 downto 0 do 10. B[c[A[i]]] ← A[j] 11. c[A[i]] ← c[A[j]] - 1
  • 8. ‫خان‬ ‫سنور‬ Algorithm Analysis Running Example 1. for i ← 1 to k do 2. c[i] ← 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 9 4 5 3 6 8 5 4 8 7 4 6 9 7 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 A C
  • 9. ‫خان‬ ‫سنور‬ Algorithm Analysis Running Example 3. for j ← 0 to n do 4. c[A[j]] ← c[A[j]] + 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 9 4 5 3 6 8 5 4 8 7 4 6 9 7 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 1 0 0 0 0 A C J 0 A{j} 5 C{A{j}} 1
  • 10. ‫خان‬ ‫سنور‬ Algorithm Analysis Running Example 3. for j ← 0 to n do 4. c[A[j]] ← c[A[j]] + 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 9 4 5 3 6 8 5 4 8 7 4 6 9 7 0 1 2 3 4 5 6 7 8 9 0 0 0 0 1 1 0 0 0 1 A C J 0 1 2 A{j} 5 9 4 C{A{j}} 1 1 1 Continue till end. . .
  • 11. ‫خان‬ ‫سنور‬ Algorithm Analysis Running Example 3. for j ← 0 to n do 4. c[A[j]] ← c[A[j]] + 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 9 4 5 3 6 8 5 4 8 7 4 6 9 7 0 1 2 3 4 5 6 7 8 9 0 0 0 1 3 3 2 2 2 2 A C
  • 12. ‫خان‬ ‫سنور‬ Algorithm Analysis Running Example 6. for i ← 1 to k do 7. c[i] ← c[i] + c[i-1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 9 4 5 3 6 8 5 4 8 7 4 6 9 7 0 1 2 3 4 5 6 7 8 9 0 0 0 1 3 3 2 2 2 2 A C i 1 2 3 4 5 6 7 8 9 10 c[i] 0 c[i-1] 0 C[i]+c[i-1] 0
  • 13. ‫خان‬ ‫سنور‬ Algorithm Analysis Running Example 6. for i ← 1 to k do 7. c[i] ← c[i] + c[i-1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 9 4 5 3 6 8 5 4 8 7 4 6 9 7 0 1 2 3 4 5 6 7 8 9 0 0 0 1 3 3 2 2 2 2 A C i 1 2 3 4 5 6 7 8 9 10 c[i] 0 0 c[i-1] 0 0 C[i]+c[i-1] 0 0
  • 14. ‫خان‬ ‫سنور‬ Algorithm Analysis Running Example 6. for i ← 1 to k do 7. c[i] ← c[i] + c[i-1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 9 4 5 3 6 8 5 4 8 7 4 6 9 7 0 1 2 3 4 5 6 7 8 9 0 0 0 1 3 3 2 2 2 2 A C i 1 2 3 4 5 6 7 8 9 10 c[i] 0 0 1 c[i-1] 0 0 0 C[i]+c[i-1] 0 0 1