+ Reply to Thread
Results 1 to 2 of 2

Thread: C# sorting a list with O(N) performance

  1. #1
    simonthecat's Avatar
    simonthecat is offline x10Hosting Member simonthecat is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    56

    C# sorting a list with O(N) performance

    For a college class I am trying to write a program that is has linear performance. The solution I wrote depends upon having a list that is sorted. Well, my teacher told me that I would not get full credit if I have to use the C# sort function. So, I am trying to write my own sort function that is linear. Its doing some strange stuff with the variables, but it seems close. Any ideas on if what I am trying to do looks plausible? Here is what I have so far:

    Code:
            static void linearIntListSort(ref List<int> inputList)
            {
                bool runLoop = true;            
                int index = 0;
                int numberSorted = 0;
                int dummyVariable = -2147483648;
                int lastNumber = dummyVariable;
                int listSize = inputList.Count;
                while (runLoop)
                {
                    if (lastNumber > inputList[index])
                    {
                        inputList[(index-1)] = inputList[index];
                        inputList[index] = lastNumber;
                    }
                    else
                    {
                        numberSorted++;
                    }
    
                    if (numberSorted == listSize)
                    {
                        runLoop = false;
                    }
    
                    lastNumber = inputList[index];
                    if (index + 1 == listSize) // if end of list is reached
                    {
                        index = 0;
                        numberSorted = 0;
                        lastNumber = dummyVariable;
                    }
    
                    index++;
                }
            }
    Last edited by simonthecat; 01-28-2011 at 12:47 PM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: C# sorting a list with O(N) performance

    No, that won't work. Your loop is basically one iteration of a bubble sort. It will place the maximal element at the end of the list, and bubble a few other elements upwards, but won't sort the list. Try the list 2,1,0.

    All comparison-based sorts have a lower time-complexity bound of Ω(n*log(n)). Fortunately for you, there are other properties of integers to base sorting on, which result in the Ω(n) performance you're looking for. Ask your teacher for details, or read over the linked pages closely.
    Last edited by misson; 01-31-2011 at 12:46 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. [PHP/MySql] Sorting After Select
    By iamcameron in forum Programming Help
    Replies: 3
    Last Post: 01-13-2010, 11:57 AM
  2. Array sorting in php
    By drf1229 in forum Programming Help
    Replies: 2
    Last Post: 01-06-2010, 08:53 PM
  3. Sorting and filtering functions
    By gptsven in forum Review My Site
    Replies: 2
    Last Post: 10-04-2009, 02:28 PM
  4. [PHP] sorting 2-dimensional array by timestamp-key
    By bonzo meier in forum Programming Help
    Replies: 0
    Last Post: 02-19-2008, 04:28 PM
  5. help with a sorting table in php
    By oiwio in forum Programming Help
    Replies: 6
    Last Post: 02-04-2008, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers