Thread: Sorting in 0(n) time

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    31

    Sorting in 0(n) time

    What algorithm should I use to sort n numbers between 0 - n^2-1 in O(n) time?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't sort in O(N) time. The best you can do is O(NLogN) time. Try Quicksort, Mergesort or Heapsort.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would be big news if they could break the O(N) barrier. Still, O(n log n) is almost O(N), so it's not that bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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?

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    31
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by KBriggs View Post
    Is O(n) possible if something is known about the way your values are distributed?
    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.
    Last edited by Elysia; 06-12-2010 at 11:43 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KBriggs View Post
    Are all the numbers between 0 and n^2-1 there?
    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

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    How about if you make an array that counts the occurences of each number.

    pseudocode: the original array if numbers is called array

    Code:
    int occurrences[n^2-1]:
    
    for (i=0;i<n;i++)
    {
         occurences[array[i]]++;
    }
    Then you could just output (or build a new array of) all the nonzero entries of occurences.

    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.


    They can't be (unless n==1). There are n numbers.
    Lol. Der.
    Last edited by KBriggs; 06-12-2010 at 11:50 AM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KBriggs View Post
    Then you could just output (or build a new array of) all the nonzero entries of occurences.

    Is that O(n)? You are only traversing the array once...
    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

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KBriggs
    Is O(n) possible if something is known about the way your values are distributed?
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Heh, yeah I realized that when I actually wrote the code for it. I wasn't thinking beyond the first step, I suppose.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by KBriggs View Post
    How about if you make an array that counts the occurences of each number.

    pseudocode: the original array if numbers is called array

    Code:
    int occurrences[n^2-1]:
    
    for (i=0;i<n;i++)
    {
         occurences[array[i]]++;
    }
    Then you could just output (or build a new array of) all the nonzero entries of occurences.

    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.
    If each number occurs at most once and is relatively small, this can be an efficient sort in O(N) time. It's also usually known as Bucket Sort.
    But that's the best scenario, not the worst.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM