What algorithm should I use to sort n numbers between 0 - n^2-1 in O(n) time?
What algorithm should I use to sort n numbers between 0 - n^2-1 in O(n) time?
I don't think there are any O(n) sorts. They are almost all either O(n^2) or O(n log n).
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Is O(n) possible if something is known about the way your values are distributed?
Are all the numbers between 0 and n^2-1 there? Because if they are, you could just make an array and set array[i]=i, and who cares where they started?
It's a question from my DSA exam: Suppose you have n integers in the range from 0 to n^2-1. Explain how to sort them in O(n) time.
Last edited by Delia; 06-12-2010 at 11:59 AM.
Possibly. If you would call a sorted insert a sort, then it's fully possible.
A sorted insert should take O(log n) time.
Delia: You can't sort in O(N) time.
If they want O(N) time, then there must be extra conditions that allow us to understand the data better to make us able to do better decisions.
They can't be (unless n==1). There are n numbers.
It's true Delia, you cannot sort a random list in O(n) time. There's a comparison of sorts here if you want:
Sorting algorithm - Wikipedia, the free encyclopedia
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
How about if you make an array that counts the occurences of each number.
pseudocode: the original array if numbers is called array
Then you could just output (or build a new array of) all the nonzero entries of occurences.Code:int occurrences[n^2-1]: for (i=0;i<n;i++) { occurences[array[i]]++; }
Is that O(n)? You are only traversing the array once...
Strictly speaking you aren't reordering the elements so it isn't a sort algorithm in that sense, but you can use it as such.
Lol. Der.They can't be (unless n==1). There are n numbers.
Last edited by KBriggs; 06-12-2010 at 11:50 AM.
Ah, but then you need to do a comparison for each element of the n^2-1 sized array! I believe the point of big O notation is that it discards evaluation of all but the most frequent operation -- which with sorting, is the number of comparisons to type.
So that would be O(n+n^2).
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Yes. For example, looking at the parameters of the problem, it may be possible to apply something like a radix sort, since we are dealing with integers in a finite range. The assertion that one cannot generally sort in O(n) time holds true for comparison based sorts, but those are not the only sorting algorithms available.Originally Posted by KBriggs
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Indeed, and O(n+n^2) is O(n^2). So you are worse off than sorting.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Heh, yeah I realized that when I actually wrote the code for it. I wasn't thinking beyond the first step, I suppose.
Bucket Sort is the answer.
It isn't a comparison sort, and the numbers you're sorting have to be within the range of the size of the array that you can create, but it's a wonderful sorter if it can be used.
No other sorting algorithm can touch it for speed.