The Gateway to Computer Science Excellence
+1 vote
Show how to sort $n$ integers in the range $0$ to $n^3-1$ in $O(n)$ time.
in Algorithms by Boss (42.5k points) | 61 views

1 Answer

+2 votes

Assume $n$ keys sorts integers $\epsilon \left \{ 0,1,....(k-1) \right \}$

Then according to counting sort , for $k$ integers can sort $O(n)$ time.

Now, here is the  proof by an algorithm:

Say, $L=$ Array of $k$ empty lists for $j$ in $range\left ( n \right )$. 

$L\left [ key\left ( A\left [ j \right ] \right ) \right ],append\left ( A\left [ j \right ] \right )$

for $i$ in $range\left [ k \right ]:$

$Output.extend\left ( L\left [ i \right ] \right )$

This algorithm has time complexity $O\left ( n+k \right )$.........$(i)$


Now, using counting sort imagine each integer has base $b.$

Then, $digit=d=\log _{b}k+1$

Now, according to counting sort expression $(i)$ will be $O\left ( n+b \right )$

Then total time$=O\left ( \left ( n+b \right ).d \right )$

                         $=O\left ( \left ( n+b \right ).\log _{b}k \right )$

                         $=O\left ( \left ( n \right ).\log _{n}k \right )$ when $b\simeq n$

                        $=O\left ( 3n \right )=O\left ( n \right )$ [when $k=n^{3}$ a polynomial function]

Source:MIT Lecture

by Veteran (119k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,378 answers
105,314 users