944,110 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Mar 30th, 2007
0

Question: Linear Time Sorting Problem

Expand Post »
Hello, I have been asked to provide an algorithm for the following:

You are given an array of length N, which contains values in the range from 0 to (N^2) - 1. You are to sort it in O(N) time.

I have been unable to find any way to do it, due to the lack of a "hard" upper bound on the input range (Which varies with the size of input).

I'll be happy to hear ideas for anyone who can offer help. Thanks !
Reputation Points: 20
Solved Threads: 1
Junior Poster
Direwolf007 is offline Offline
161 posts
since Mar 2007
Mar 31st, 2007
0

Re: Question: Linear Time Sorting Problem

My first thought was a radix sort, but since that does depend on the value of the largest number, I'm don't think that it's strictly O(N) in this case.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Mar 31st, 2007
0

Re: Question: Linear Time Sorting Problem

I have tried Radix Sort, but since the number of digits is not fixed, it scales with size of input, due to that fact, in this case, the complexity would be O(nlogn), since log(base10)n is the amount of digits.
Reputation Points: 20
Solved Threads: 1
Junior Poster
Direwolf007 is offline Offline
161 posts
since Mar 2007
Mar 31st, 2007
0

Re: Question: Linear Time Sorting Problem

Yeah, that's what I was thinking too. Even counting sort depends on the range of values, so it wouldn't fit either. I'm out of ideas, sorry
Last edited by Infarction; Mar 31st, 2007 at 6:53 pm.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 1st, 2007
0

Re: Question: Linear Time Sorting Problem

Thanks for the reply, nonetheless.

If anyone else could chime in, I would be rather thankful.
Reputation Points: 20
Solved Threads: 1
Junior Poster
Direwolf007 is offline Offline
161 posts
since Mar 2007
Apr 1st, 2007
-1

Re: Question: Linear Time Sorting Problem

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 1st, 2007
0

Re: Question: Linear Time Sorting Problem

While I thank you for the reply - I already read the wikipedia entry for that, twice. I also spent a few hours on google, and reading "Introduction To Algorithms" (One of the authors is Cormen, another is Rivest, another is Stein, I don't recall the last one).

Any other ideas folks ?
Last edited by Direwolf007; Apr 1st, 2007 at 9:57 am.
Reputation Points: 20
Solved Threads: 1
Junior Poster
Direwolf007 is offline Offline
161 posts
since Mar 2007
Apr 3rd, 2007
0

Re: Question: Linear Time Sorting Problem

>> You are given an array of length N, which contains values in the range from 0 to (N^2) - 1. You are to sort it in O(N) time.

2 questions to understand the question:

1. Why does the range of value matter? (size/range of value would have no impact on the order of algorithm from what I know)
2. When you say "in O(N) time" do you mean it's an algorithm of order N or you really mean some number of mili/micro/seconds.. ?
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Apr 3rd, 2007
0

Re: Question: Linear Time Sorting Problem

1. It does affect algorithm runtime in several cases, especially when dealing with non-comparison based sorts (since it's proven that comparision based sorts cannot be guaranteed to run in better than O(n log n) time, they don't make the cut for this problem). Look at some of the sorts already mentioned in this thread.

2. Linear time with respect to the length of the input.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 3rd, 2007
0

Re: Question: Linear Time Sorting Problem

Have you tried out SmoothSort ?

Its best case is O(n) and worst case is O(n log(n)) with the obvious downfall of difficult to implement.
Last edited by ~s.o.s~; Apr 3rd, 2007 at 4:11 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Computer Science Forum Timeline: Using flex and bison
Next Thread in Computer Science Forum Timeline: Instructor's manual





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC