Saturday, January 24, 2009

Problem solving

I have already told in the introduction about myself, my blogs can be random...well, I mean, with a probability (P) of any topic being covered defined as
P = a rational number x where 0 <= x <= 1 ;)

So, let's solve a algorithmic problem, which I found on someone else' blog (http://my-experiments-with-it.blogspot.com) and liked it.

Here goes it:

You have an array of size 'n' with numbers from 1 to 'n'. You need to sort this array in the most optimal way.

HINT(Don't read the hint if you think you can do without it): The solution has a complexity of O(n). i.e. even less than the O(n.log(n)) which is given by the best sorting algorithms.
Find the answer in the comments section.

3 comments:

Anish Goyal said...

Answer: There is no need to sort the array at all. Just enumerate the array in a loop of 'n'. Remember that array had numbers from 1 to 'n'.

Anonymous said...

man, this is such an easy problem.

Anish Goyal said...

yes, I know! Added it here just for fun :-)
may I know your name?
unable to guess...!