SlideShare a Scribd company logo
Sorting Algorithms
Counting sort
 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
 Counting sort determines for each input element x, the number of
elements less than x. And it uses this information to place
element x directly into its position in the output array. For
example if there exits 17 elements less that x then x is placed into
the 18th
position into the output array.
 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[1..k] store data temporarily
Counting Sort
1. Counting-Sort(A, B, k)
2. Let C[0…..k] be a new array
3. for i=0 to k
4. C[i]= 0;
5. for j=1 to A.length or n
6. C[ A[j] ] = C[ A[j] ] + 1;
7. for i=1 to k
8. C[i] = C[i] + C[i-1];
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;
Counting Sort
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;
Counting-sort example
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00 33
0 5
1 2 3 4 5 6 7 8
Executing Loop 1
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00 33
00 00 00 00 00
0
00
5
1 2 3 4 5 6 7 8
Executing Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00 33
00 00 1 00 00
0
00
5
1 2 3 4 5 6 7 8
Executing Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
5 33 00 22 33 00 33
00 00 11 00 00
0
1
5
1 2 3 4 5 6 7 8
Executing Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 3 00 22 33 00 33
00 00 11 1 00
0
11
5
1 2 3 4 5 6 7 8
Executing Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 0 22 33 00 33
1 00 11 11 00
0
11
5
1 2 3 4 5 6 7 8
Executing Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 2 33 00 33
11 00 2 11 00
0
11
5
1 2 3 4 5 6 7 8
Executing Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 3 00 33
11 00 22 2 00
0
11
5
1 2 3 4 5 6 7 8
Executing Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 0 33
2 00 22 22 00
0
11
5
1 2 3 4 5 6 7 8
Executing Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00 3
22 00 22 3 00
0
11
5
1 2 3 4 5 6 7 8
End of Loop 2
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00
22 00 22 00
0
11
5
1 2 3 4 5 6 7 8
33
33
Executing Loop 3
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00
2 0 22 00
0
11
5
1 2 3 4 5 6 7 8
33
33
C: 22 00 1133
1 2 3 40 5
222
Executing Loop 3
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00
2222 00
0
11
5
1 2 3 4 5 6 7 8
33
33
C: 22 4 00 1133
1 2 3 40 5
22
Executing Loop 3
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00
422 00
0
11
5
1 2 3 4 5 6 7 8
3
33
C: 22 44 00 117
1 2 3 40 5
22
22
Executing Loop 3
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00
22 0
0
11
5
1 2 3 4 5 6 7 8
7
33
C: 22 44 7 11
1 2 3 40 5
22
22
44
77
Executing Loop 3
A:
B:
1 2 3 4 5
C:
1 2 3 4
6 7 8
55 33 00 22 33 00
22
0
1
5
1 2 3 4 5 6 7 8
33
C: 22 44 8
1 2 3 40 5
22
22
44
77
77 7
77
End of Loop 3
A:
B:
1 2 3 4 5 6 7 8
55 33 00 22 33 00
1 2 3 4 5 6 7 8
33
C: 22 44
1 2 3 40 5
22 77 77 88
Executing Loop 4
A:
B:
1 2 3 4 5 6 7 8
55 33 00 22 33 00
1 2 3 4 5 6 7 8
3
C: 22 44
1 2 3 40 5
22 7 77 88
Executing Loop 4
A:
B:
1 2 3 4 5 6 7 8
55 33 00 22 33 00
1 2 3 4 5 6 7 8
C: 22 44
1 2 3 40 5
22 7 77 88
3
J=8, then A[ j ]=A[8]=3
And B[ C[ A[j] ] ]
=B[ C[ 3 ] ]
=B[ 7]
So B[ C[ A[j] ] ] ←A[ j ]
=B[7]←3
A:
B:
1 2 3 4 5 6 7 8
55 33 00 22 33
1 2 3 4 5 6 7 8
C: 2 44
1 2 3 40 5
22 77 88
330
66
33
Executing Loop 4
J=8, then A[ j ]=A[8]=3
Then C[ A[j] ]
= C[ 3 ]
=7
So C[ A[j] ] = C[ A[j] ] -1
=7-1=6
Executing Loop 4
A:
B:
1 2 3 4 5 6 7 8
55 33 00 22
1 2 3 4 5 6 7 8
C: 44
1 2 3 40 5
22 77 88
33
6
33
00
11
00
3
Executing Loop 4
A:
B:
1 2 3 4 5 6 7 8
55 33 00
1 2 3 4 5 6 7 8
C: 4
1 2 3 40 5
22 77 88
33
55
33
00
00
2
11
33
33
Executing Loop 4
A:
B:
1 2 3 4 5 6 7 8
55 33
1 2 3 4 5 6 7 8
C: 33
1 2 3 40 5
22 77 88
33
33
00
00
0
1
33
33
55
22
Executing Loop 4
A:
B:
1 2 3 4 5 6 7 8
55
1 2 3 4 5 6 7 8
C:
1 2 3 40 5
22 77 88
33
33
00
00
0
00
33
33
533
00 22
3
Executing Loop 4
A:
B:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
C:
1 2 3 40 5
22 77 8
33
33
00
00
0 33
33
33
00 22
335
44
33
00
Executing Loop 4
A:
B:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
C:
1 2 3 40 5
22 77 77
33
33
00
00
0 33
33
3
00 22
332
33
00
55
44
55
End of Loop 4
A:
B:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
C:
1 2 3 40 5
22 77
33
33
00
00
0 33
3300 22
33
33
00
55
44
55
22 77
22
Sorted data in Array B
Time Complexity Analysis
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;
Loop 2 and 4
takes O(n) time
Loop 1 and 3
takes O(k) time
Time Complexity Analysis
• So the counting sort takes a total time of: O(n + k)
• Counting sort is called stable sort.
– A sorting algorithm is stable when numbers with
the same values appear in the output array in the
same order as they do in the input array.
Counting Sort Review
• Assumption: input taken from small set of numbers of size k
• Basic idea:
– Count number of elements less than you for each element.
– This gives the position of that number – similar to selection
sort.
• Pro’s:
– Fast
– Asymptotically fast - O(n+k)
– Simple to code
• Con’s:
– Doesn’t sort in place.
– Requires O(n+k) extra storage.
Radix Sort
• Radix sort is non comparative sorting
method
• Two classifications of radix sorts are least
significant digit (LSD) radix sorts and most
significant digit (MSD) radix sorts.
• LSD radix sorts process the integer
representations starting from the least digit
and move towards the most significant digit.
MSD radix sorts work the other way around.
35
Radix Sort
;digitonarraysortsort tostableause"do
to1for
),(Sort-Radix
digit.ofnumberaiselementeach,arrayinputIn
iA
di
dA
dA
←
355
720
436
839
657
457
329
839
329
657
457
436
355
720
657
457
355
839
436
329
720
839
720
657
457
436
355
329
The Algorithm
void radixsort(int a[1000],int n,int digits)
{
for(int i =1;i<=digits;i++)
countsort(a,n,i);
}
The Algorithm
void countsort(int a[1000],int n,int x)
{
int d[1000],t;
for(int s=1;s<=n;s++) // extracting the concerned digit from
{ t = a[s]; the number
t = t / (pow(10,x-1));
d[s] = t%10;
}
int c[10],b[1000],i,j;
for(i=0;i<=9;i++)
c[i] = 0;
The Algorithm
for(j = 1;j<=n;++j)
c[d[j]] = c[d[j]] + 1; //c[i] contains no of elements
for(i =0;i<9;i++) equal to i
c[i+1] = c[i+1] + c[i];
for(j=n;j>0;j--)
{ b[c[d[j]]] = a[j]; //shift the array’s numbers
c[d[j]] = c[d[j]] -1;
}
for(i=1;i<=n;i++)
a[i] = b[i];
}
Time Complexity Analysis
Given n d-digit number in which each digit can
take up to k possible values, RADIX-SORT
correctly sorts these numbers in Ө(d(n+k))
time if the stable sort it uses takes Ө(n+k)
time.
Time Complexity Analysis
Given n b-bit numbers and any positive integer
r<=b, RADIX-SORT correctly sorts theses
numbers in Ө((b/r)(n + 2r
)) time if the stable
sort it uses takes Ө(n+k) time for inputs in the
range 0 to k.
For example – A 32 bit word can be viewed as
four 8 bit digits, so b = 32, r = 8, k = 2r
– 1 =
255, d = 4. Each pass of counting sort takes
time Ө(n+k) = Ө(n+2r
) and there are d passes,
so total running time Ө(d(n+2r
)) = Ө(b/r(n+2r
))
Radix Sort Review
• Assumption: input taken from large set of numbers
• Basic idea:
– Sort the input on the basis of digits starting from unit’s
place.
– This gives the position of that number – similar to selection
sort.
• Pro’s:
– Fast
– Asymptotically fast - O(d(n+k))
– Simple to code
• Con’s:
– Doesn’t sort in place.
Counting Sort and Radix Sort Algorithms

More Related Content

What's hot (20)

PDF
Recurrence relation solutions
subhashchandra197
 
PPTX
Complexity analysis in Algorithms
Daffodil International University
 
PPT
Infix prefix postfix
Self-Employed
 
PPT
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
PPT
Chapter 12 ds
Hanif Durad
 
PPT
Heaps
Hafiz Atif Amin
 
PPTX
Huffman Coding Algorithm Presentation
Akm Monir
 
PPTX
Greedy algorithms
sandeep54552
 
PPTX
Merge Sort
Nikhil Sonkamble
 
PPTX
Counting Sort
Faiza Saleem
 
PPTX
Binary search
AparnaKumari31
 
PPT
Breadth first search
Sazzad Hossain
 
PPTX
01 Knapsack using Dynamic Programming
Fenil Shah
 
PPT
Bucket sort
Hossain Md Shakhawat
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PPTX
Stressen's matrix multiplication
Kumar
 
PPTX
Strassen's matrix multiplication
Megha V
 
PPTX
Bucket sort- A Noncomparision Algorithm
Krupali Mistry
 
PPTX
Queue Implementation Using Array & Linked List
PTCL
 
PPTX
heap Sort Algorithm
Lemia Algmri
 
Recurrence relation solutions
subhashchandra197
 
Complexity analysis in Algorithms
Daffodil International University
 
Infix prefix postfix
Self-Employed
 
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Chapter 12 ds
Hanif Durad
 
Huffman Coding Algorithm Presentation
Akm Monir
 
Greedy algorithms
sandeep54552
 
Merge Sort
Nikhil Sonkamble
 
Counting Sort
Faiza Saleem
 
Binary search
AparnaKumari31
 
Breadth first search
Sazzad Hossain
 
01 Knapsack using Dynamic Programming
Fenil Shah
 
Divide and conquer
Dr Shashikant Athawale
 
Stressen's matrix multiplication
Kumar
 
Strassen's matrix multiplication
Megha V
 
Bucket sort- A Noncomparision Algorithm
Krupali Mistry
 
Queue Implementation Using Array & Linked List
PTCL
 
heap Sort Algorithm
Lemia Algmri
 

Viewers also liked (20)

PPT
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
PPTX
Radix sorting
Madhawa Gunasekara
 
PPTX
Metodos de ordenacion radix sort
tephyfree
 
PPTX
Radix sort presentation
Ratul Hasan
 
PDF
Algoritmo Counting sort
Fernando Simeone
 
PPTX
Bucket sort
eislenp
 
PPT
Algorithm: Quick-Sort
Tareq Hasan
 
PPTX
SORTTING IN LINEAR TIME - Radix Sort
Devanshu Taneja
 
PPTX
Presentation
Sayed Hoque
 
PPT
Bucket sort
Toto MZiri
 
PPT
3.6 radix sort
Krish_ver2
 
PPTX
Insertion and Redix Sort
Sukhvinder Singh
 
PPT
Heap sort
Mohd Arif
 
PPT
Graphs In Data Structure
Anuj Modi
 
PPT
Merge sort
Vidushi Pathak
 
PPT
Sorting algorithms v01
Dusan Vuckovic
 
PPTX
Sorting algorithms
Trupti Agrawal
 
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Radix sorting
Madhawa Gunasekara
 
Metodos de ordenacion radix sort
tephyfree
 
Radix sort presentation
Ratul Hasan
 
Algoritmo Counting sort
Fernando Simeone
 
Bucket sort
eislenp
 
Algorithm: Quick-Sort
Tareq Hasan
 
SORTTING IN LINEAR TIME - Radix Sort
Devanshu Taneja
 
Presentation
Sayed Hoque
 
Bucket sort
Toto MZiri
 
3.6 radix sort
Krish_ver2
 
Insertion and Redix Sort
Sukhvinder Singh
 
Heap sort
Mohd Arif
 
Graphs In Data Structure
Anuj Modi
 
Merge sort
Vidushi Pathak
 
Sorting algorithms v01
Dusan Vuckovic
 
Sorting algorithms
Trupti Agrawal
 
Ad

Similar to Counting Sort and Radix Sort Algorithms (20)

PPTX
Counting sort
Imdad Ul Haq
 
PPTX
Linear Sorting
Bhavik Vashi
 
PPT
Chapter 6 Matrices in MATLAB
Pranoti Doke
 
PPTX
unit3_Dynamic_Progrghaiajawzjabagamming1.pptx
suhailakrami001
 
PDF
Form 5 Additional Maths Note
Chek Wei Tan
 
PPTX
Trigonometry Cheat Sheet
Jayson Albrey Sastre
 
PDF
Negative numbers
Ahmed Shaaban
 
PPTX
Data Structure and algorithms for software
ManishShukla712917
 
PDF
Numerical Methods: Solution of system of equations
Nikolai Priezjev
 
PPT
Complete Factoring Rules for Grade 8.ppt
KirbyRaeDiaz2
 
PDF
Sesión de aprendizaje de Radicación Algebra pre u ccesa007
Demetrio Ccesa Rayme
 
PPT
Complete Factoring Rules.ppt
Jasmin679773
 
PPT
Complete Factoring Rules in Grade 8 Math.ppt
ElmabethDelaCruz2
 
PPTX
Cacaomplete Factoring Rules for grade 10 .pptx
AlberMartins1
 
PDF
Teoria y problemas de funciones cuadraticas fc324 ccesa007
Demetrio Ccesa Rayme
 
PDF
determinants-160504230830.pdf
Praveen Kumar Verma PMP
 
PDF
determinants-160504230830_repaired.pdf
TGBSmile
 
PDF
Solution of matlab chapter 3
AhsanIrshad8
 
PDF
Determinants
Joey Fontanilla Valdriz
 
PDF
Sesion de aprendizaje de logaritmos algebra pre u ccesa007
Demetrio Ccesa Rayme
 
Counting sort
Imdad Ul Haq
 
Linear Sorting
Bhavik Vashi
 
Chapter 6 Matrices in MATLAB
Pranoti Doke
 
unit3_Dynamic_Progrghaiajawzjabagamming1.pptx
suhailakrami001
 
Form 5 Additional Maths Note
Chek Wei Tan
 
Trigonometry Cheat Sheet
Jayson Albrey Sastre
 
Negative numbers
Ahmed Shaaban
 
Data Structure and algorithms for software
ManishShukla712917
 
Numerical Methods: Solution of system of equations
Nikolai Priezjev
 
Complete Factoring Rules for Grade 8.ppt
KirbyRaeDiaz2
 
Sesión de aprendizaje de Radicación Algebra pre u ccesa007
Demetrio Ccesa Rayme
 
Complete Factoring Rules.ppt
Jasmin679773
 
Complete Factoring Rules in Grade 8 Math.ppt
ElmabethDelaCruz2
 
Cacaomplete Factoring Rules for grade 10 .pptx
AlberMartins1
 
Teoria y problemas de funciones cuadraticas fc324 ccesa007
Demetrio Ccesa Rayme
 
determinants-160504230830.pdf
Praveen Kumar Verma PMP
 
determinants-160504230830_repaired.pdf
TGBSmile
 
Solution of matlab chapter 3
AhsanIrshad8
 
Sesion de aprendizaje de logaritmos algebra pre u ccesa007
Demetrio Ccesa Rayme
 
Ad

Recently uploaded (20)

PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 

Counting Sort and Radix Sort Algorithms

  • 2. Counting sort  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  Counting sort determines for each input element x, the number of elements less than x. And it uses this information to place element x directly into its position in the output array. For example if there exits 17 elements less that x then x is placed into the 18th position into the output array.  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[1..k] store data temporarily
  • 3. Counting Sort 1. Counting-Sort(A, B, k) 2. Let C[0…..k] be a new array 3. for i=0 to k 4. C[i]= 0; 5. for j=1 to A.length or n 6. C[ A[j] ] = C[ A[j] ] + 1; 7. for i=1 to k 8. C[i] = C[i] + C[i-1]; 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;
  • 4. Counting Sort 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;
  • 5. Counting-sort example A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 33 0 5 1 2 3 4 5 6 7 8
  • 6. Executing Loop 1 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 33 00 00 00 00 00 0 00 5 1 2 3 4 5 6 7 8
  • 7. Executing Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 33 00 00 1 00 00 0 00 5 1 2 3 4 5 6 7 8
  • 8. Executing Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 5 33 00 22 33 00 33 00 00 11 00 00 0 1 5 1 2 3 4 5 6 7 8
  • 9. Executing Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 3 00 22 33 00 33 00 00 11 1 00 0 11 5 1 2 3 4 5 6 7 8
  • 10. Executing Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 0 22 33 00 33 1 00 11 11 00 0 11 5 1 2 3 4 5 6 7 8
  • 11. Executing Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 2 33 00 33 11 00 2 11 00 0 11 5 1 2 3 4 5 6 7 8
  • 12. Executing Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 3 00 33 11 00 22 2 00 0 11 5 1 2 3 4 5 6 7 8
  • 13. Executing Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 0 33 2 00 22 22 00 0 11 5 1 2 3 4 5 6 7 8
  • 14. Executing Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 3 22 00 22 3 00 0 11 5 1 2 3 4 5 6 7 8
  • 15. End of Loop 2 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 22 00 22 00 0 11 5 1 2 3 4 5 6 7 8 33 33
  • 16. Executing Loop 3 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 2 0 22 00 0 11 5 1 2 3 4 5 6 7 8 33 33 C: 22 00 1133 1 2 3 40 5 222
  • 17. Executing Loop 3 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 2222 00 0 11 5 1 2 3 4 5 6 7 8 33 33 C: 22 4 00 1133 1 2 3 40 5 22
  • 18. Executing Loop 3 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 422 00 0 11 5 1 2 3 4 5 6 7 8 3 33 C: 22 44 00 117 1 2 3 40 5 22 22
  • 19. Executing Loop 3 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 22 0 0 11 5 1 2 3 4 5 6 7 8 7 33 C: 22 44 7 11 1 2 3 40 5 22 22 44 77
  • 20. Executing Loop 3 A: B: 1 2 3 4 5 C: 1 2 3 4 6 7 8 55 33 00 22 33 00 22 0 1 5 1 2 3 4 5 6 7 8 33 C: 22 44 8 1 2 3 40 5 22 22 44 77 77 7 77
  • 21. End of Loop 3 A: B: 1 2 3 4 5 6 7 8 55 33 00 22 33 00 1 2 3 4 5 6 7 8 33 C: 22 44 1 2 3 40 5 22 77 77 88
  • 22. Executing Loop 4 A: B: 1 2 3 4 5 6 7 8 55 33 00 22 33 00 1 2 3 4 5 6 7 8 3 C: 22 44 1 2 3 40 5 22 7 77 88
  • 23. Executing Loop 4 A: B: 1 2 3 4 5 6 7 8 55 33 00 22 33 00 1 2 3 4 5 6 7 8 C: 22 44 1 2 3 40 5 22 7 77 88 3 J=8, then A[ j ]=A[8]=3 And B[ C[ A[j] ] ] =B[ C[ 3 ] ] =B[ 7] So B[ C[ A[j] ] ] ←A[ j ] =B[7]←3
  • 24. A: B: 1 2 3 4 5 6 7 8 55 33 00 22 33 1 2 3 4 5 6 7 8 C: 2 44 1 2 3 40 5 22 77 88 330 66 33 Executing Loop 4 J=8, then A[ j ]=A[8]=3 Then C[ A[j] ] = C[ 3 ] =7 So C[ A[j] ] = C[ A[j] ] -1 =7-1=6
  • 25. Executing Loop 4 A: B: 1 2 3 4 5 6 7 8 55 33 00 22 1 2 3 4 5 6 7 8 C: 44 1 2 3 40 5 22 77 88 33 6 33 00 11 00 3
  • 26. Executing Loop 4 A: B: 1 2 3 4 5 6 7 8 55 33 00 1 2 3 4 5 6 7 8 C: 4 1 2 3 40 5 22 77 88 33 55 33 00 00 2 11 33 33
  • 27. Executing Loop 4 A: B: 1 2 3 4 5 6 7 8 55 33 1 2 3 4 5 6 7 8 C: 33 1 2 3 40 5 22 77 88 33 33 00 00 0 1 33 33 55 22
  • 28. Executing Loop 4 A: B: 1 2 3 4 5 6 7 8 55 1 2 3 4 5 6 7 8 C: 1 2 3 40 5 22 77 88 33 33 00 00 0 00 33 33 533 00 22 3
  • 29. Executing Loop 4 A: B: 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 C: 1 2 3 40 5 22 77 8 33 33 00 00 0 33 33 33 00 22 335 44 33 00
  • 30. Executing Loop 4 A: B: 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 C: 1 2 3 40 5 22 77 77 33 33 00 00 0 33 33 3 00 22 332 33 00 55 44 55
  • 31. End of Loop 4 A: B: 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 C: 1 2 3 40 5 22 77 33 33 00 00 0 33 3300 22 33 33 00 55 44 55 22 77 22 Sorted data in Array B
  • 32. Time Complexity Analysis 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; Loop 2 and 4 takes O(n) time Loop 1 and 3 takes O(k) time
  • 33. Time Complexity Analysis • So the counting sort takes a total time of: O(n + k) • Counting sort is called stable sort. – A sorting algorithm is stable when numbers with the same values appear in the output array in the same order as they do in the input array.
  • 34. Counting Sort Review • Assumption: input taken from small set of numbers of size k • Basic idea: – Count number of elements less than you for each element. – This gives the position of that number – similar to selection sort. • Pro’s: – Fast – Asymptotically fast - O(n+k) – Simple to code • Con’s: – Doesn’t sort in place. – Requires O(n+k) extra storage.
  • 35. Radix Sort • Radix sort is non comparative sorting method • Two classifications of radix sorts are least significant digit (LSD) radix sorts and most significant digit (MSD) radix sorts. • LSD radix sorts process the integer representations starting from the least digit and move towards the most significant digit. MSD radix sorts work the other way around. 35
  • 37. The Algorithm void radixsort(int a[1000],int n,int digits) { for(int i =1;i<=digits;i++) countsort(a,n,i); }
  • 38. The Algorithm void countsort(int a[1000],int n,int x) { int d[1000],t; for(int s=1;s<=n;s++) // extracting the concerned digit from { t = a[s]; the number t = t / (pow(10,x-1)); d[s] = t%10; } int c[10],b[1000],i,j; for(i=0;i<=9;i++) c[i] = 0;
  • 39. The Algorithm for(j = 1;j<=n;++j) c[d[j]] = c[d[j]] + 1; //c[i] contains no of elements for(i =0;i<9;i++) equal to i c[i+1] = c[i+1] + c[i]; for(j=n;j>0;j--) { b[c[d[j]]] = a[j]; //shift the array’s numbers c[d[j]] = c[d[j]] -1; } for(i=1;i<=n;i++) a[i] = b[i]; }
  • 40. Time Complexity Analysis Given n d-digit number in which each digit can take up to k possible values, RADIX-SORT correctly sorts these numbers in Ө(d(n+k)) time if the stable sort it uses takes Ө(n+k) time.
  • 41. Time Complexity Analysis Given n b-bit numbers and any positive integer r<=b, RADIX-SORT correctly sorts theses numbers in Ө((b/r)(n + 2r )) time if the stable sort it uses takes Ө(n+k) time for inputs in the range 0 to k. For example – A 32 bit word can be viewed as four 8 bit digits, so b = 32, r = 8, k = 2r – 1 = 255, d = 4. Each pass of counting sort takes time Ө(n+k) = Ө(n+2r ) and there are d passes, so total running time Ө(d(n+2r )) = Ө(b/r(n+2r ))
  • 42. Radix Sort Review • Assumption: input taken from large set of numbers • Basic idea: – Sort the input on the basis of digits starting from unit’s place. – This gives the position of that number – similar to selection sort. • Pro’s: – Fast – Asymptotically fast - O(d(n+k)) – Simple to code • Con’s: – Doesn’t sort in place.