












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture is part of lecture series for Design and Analysis of Algorithms course. This course was taught by Dr. Bhaskar Sanyal at Maulana Azad National Institute of Technology. It includes: Design, Analysis, Algorithms, Counting, Sort, Linear, Time, Auxiliary, Storage, Loop, Running
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Design & Analysis of Algorithms
Counting sort: No comparisons between elements.
1 2 3 4 5 C : 1 2 3 4
1 2 3 4 5 C : 0 0 0 0 1 2 3 4 for i 1 to k do C [ i ] 0
1 2 3 4 5 C : 1 0 0 1 1 2 3 4 for j 1 to n do C [ A [ j ]] C [ A [ j ]] + 1 ⊳ C [ i ] = |{key = i }|
1 2 3 4 5 C : 1 0 1 1 1 2 3 4 for j 1 to n do C [ A [ j ]] C [ A [ j ]] + 1 ⊳ C [ i ] = |{key = i }|
1 2 3 4 5 C : 1 0 2 2 1 2 3 4 for j 1 to n do C [ A [ j ]] C [ A [ j ]] + 1 ⊳ C [ i ] = |{key = i }|
1 2 3 4 5 C : 1 0 2 2 1 2 3 4 C' : 1 1 2 2 for i 2 to k do C [ i ] C [ i ] + C [ i – 1] ⊳ C [ i ] = |{key i }|
1 2 3 4 5 C : 1 0 2 2 1 2 3 4 C' : 1 1 3 5 for i 2 to k do C [ i ] C [ i ] + C [ i – 1] ⊳ C [ i ] = |{key i }|
1 2 3 4 5 C : 1 1 3 5 1 2 3 4 C' : 1 1 2 5 for j n downto 1 do B [ C [ A [ j ]]] A[ j ] C [ A [ j ]] C [ A [ j ]] – 1
1 2 3 4 5 C : 1 1 2 4 1 2 3 4 C' : 1 1 1 4 for j n downto 1 do B [ C [ A [ j ]]] A[ j ] C [ A [ j ]] C [ A [ j ]] – 1
1 2 3 4 5 C : 1 1 1 4 1 2 3 4 C' : 0 1 1 4 for j n downto 1 do B [ C [ A [ j ]]] A[ j ] C [ A [ j ]] C [ A [ j ]] – 1
for i 1 to k do C [ i ] 0 ( n ) ( k ) ( n ) ( k ) for j 1 to n do C [ A [ j ]] C [ A [ j ]] + 1 for i 2 to k do C [ i ] C [ i ] + C [ i – 1] for j n downto 1 do B [ C [ A [ j ]]] A[ j ] C [ A [ j ]] C [ A [ j ]] – 1 ( n + k )
If k = O ( n ), then counting sort takes ( n ) time.
Answer:
elements occurs!