5

If I have a list of size n and I know that the numbers in the list will be between 1 and 2n how would I go about solving it where the worst case would be O(n)?

I was thinking that if it was between 1 and n I could just take the number and swap it with the value of the array at that number - 1 but then it wouldn't sort if there were any duplicate.

I was thinking of a similar approach for the list having number between 1 and 2n but I can't seem to figure it out. Any help please?

3
  • How big can n be? As long as n is not much bigger than the length of the list, counting sort is by far the fastest solution.
    – Tesseract
    Commented Dec 1, 2013 at 5:33
  • not a very java question tho... Commented Dec 1, 2013 at 9:22
  • @SpiderPig n will be the size of the list where n can be of any size Commented Dec 3, 2013 at 7:12

4 Answers 4

7

Counting Sort can operate in O(n) in your case. Have a look on wikipedia's definition

counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items

http://en.wikipedia.org/wiki/Counting_sort

5

You would usually need a non-comparison sort to sort in O(n) time, but this is if you are already given certain information. There are three "main" non-comparison sort algorithms to choose from: counting sort, radix sort, and bucket sort.

Use counting sort if you know that the input are small integers. By Wikipedia, "counting sort is only suitable to use in situations where the variation in keys is not significantly greater than the number of items." http://en.wikipedia.org/wiki/Counting_sort

You can use radix sort if all the numbers you are sorting have the same number of integers. Ex: 211, 311, 122.

Bucket sort may seem like the best option for you. Your approach sounds good, but you do not need an array from 1 to 2n with an index for each number. In bucket sort, you can have an array from 1 to 20, and have something like a linked list within each element. So if you were placing the number 109 it could correspond to the index 10.

0

You can use radix sort here. When you have restriction on the input, like in your case, it works well. Check out the algorithm here - http://en.wikipedia.org/wiki/Radix_sort

1
  • 1
    Counting is better than radix for such a well defined problem. Commented Dec 1, 2013 at 9:21
0

You can use radix sort, counting sort, there are lots of O(n) sorts.

But the question is making it really easy for you ...

In Java:

int [n] input = {your list of n numbers}; int [2*n] listinorder; int [n] output;

for (int i=0, i<2n, i++) listinorder[i] = -1; // set all values of listinorder to -1

for (int i=0, i < n, i++) listinorder[input[i]] = input[i]; // set the ith value of listinorder to i

int j=0;

for (int i=0, i<2n, i++) if (listinorder(i)  != -1) {output[j] = i; j++};

In English:

  1. Establish an array (say listinorder) of length 2n.
  2. Set every value to -1. Step through the input list.
  3. For each value i in the input list, set the corresponding value of listinorder to i (ie listinorder[i]=i). Then step through the values of listinorder which aren't -1 to produce the sorted list.

All O(n).

1
  • "listinorder[i]=i" - you're assuming any given number only appears once in the input. Commented Dec 3, 2013 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.