Sorting a list of integers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robin9876@hotmail.com

    #1

    Sorting a list of integers

    I have a list of integers (example sub set of data below), what is the
    best method of sorting these in code?

    1, 872
    5, 1283
    8, 343
    9, 123

  • matt urbanowski

    #2
    Re: Sorting a list of integers

    If you've only got a small amount of data then the easiest way to
    program it would be to use a bubble sort
    Take a look at http://www.osix.net/modules/article/?id=158

    Matt


    On 10 Jan, 16:52, robin9...@hotma il.com wrote:
    I have a list of integers (example sub set of data below), what is the
    best method of sorting these in code?
    >
    1, 872
    5, 1283
    8, 343
    9, 123

    Comment

    • lord.zoltar@gmail.com

      #3
      Re: Sorting a list of integers


      matt urbanowski wrote:
      If you've only got a small amount of data then the easiest way to
      program it would be to use a bubble sort
      Take a look at http://www.osix.net/modules/article/?id=158
      >
      Matt
      >
      >
      Bubble sort is O(n^2), isn't it? I'm not sure how Array.Sort is
      implemented, but it's probably better than O(n^2).
      To OP, put the numbers into an array and then call Array.Sort.

      Comment

      • Brian Gideon

        #4
        Re: Sorting a list of integers


        lord.zol...@gma il.com wrote:
        Bubble sort is O(n^2), isn't it? I'm not sure how Array.Sort is
        implemented, but it's probably better than O(n^2).
        To OP, put the numbers into an array and then call Array.Sort.
        I'd use Array.Sort as well. It uses the quicksort algorithm so it's
        faster and most importantly it's already implemented for us.

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Sorting a list of integers

          <lord.zoltar@gm ail.comschrieb:
          >If you've only got a small amount of data then the easiest way to
          >program it would be to use a bubble sort
          >Take a look at http://www.osix.net/modules/article/?id=158
          >
          Bubble sort is O(n^2), isn't it?
          That's true. Bubble sort is only a good choice for arrays which are already
          mostly sorted.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • robin9876@hotmail.com

            #6
            Re: Sorting a list of integers

            I thought that the array list option only allows one value to be set in
            the array and still allowed to be sorted?

            Herfried K. Wagner [MVP] wrote:
            <lord.zoltar@gm ail.comschrieb:
            If you've only got a small amount of data then the easiest way to
            program it would be to use a bubble sort
            Take a look at http://www.osix.net/modules/article/?id=158
            Bubble sort is O(n^2), isn't it?
            >
            That's true. Bubble sort is only a good choice for arrays which are already
            mostly sorted.
            >
            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

            Comment

            • Tom Shelton

              #7
              Re: Sorting a list of integers

              On 2007-01-11, robin9876@hotma il.com <robin9876@hotm ail.comwrote:
              I thought that the array list option only allows one value to be set in
              the array and still allowed to be sorted?
              You mean does it have to contain unique values? No. Array.Sort makes no such
              requirement.

              --
              Tom Shelton

              Comment

              • robin9876@hotmail.com

                #8
                Re: Sorting a list of integers

                I mean if the rows have two values but then sorting on one of the
                columns and not an amalgamation of the columns.

                Tom Shelton wrote:
                On 2007-01-11, robin9876@hotma il.com <robin9876@hotm ail.comwrote:
                I thought that the array list option only allows one value to be set in
                the array and still allowed to be sorted?
                >
                You mean does it have to contain unique values? No. Array.Sort makes no such
                requirement.
                >
                --
                Tom Shelton

                Comment

                • sloan

                  #9
                  Re: Sorting a list of integers

                  See:

                  Advanced IComparer // Sorting on Multiple Values


                  <robin9876@hotm ail.comwrote in message
                  news:1168447952 .961485.275940@ i39g2000hsf.goo glegroups.com.. .
                  I have a list of integers (example sub set of data below), what is the
                  best method of sorting these in code?
                  >
                  1, 872
                  5, 1283
                  8, 343
                  9, 123
                  >

                  Comment

                  • Tom Leylan

                    #10
                    Re: Sorting a list of integers

                    Don't mean to toot my own horn... but I took a list at code referenced at
                    that link and frankly I wouldn't do it that way. For a couple of reasons
                    but in so far as I can tell by just reading the code it may not work
                    properly. Not saying it "doesn't" just that it might not.

                    Why? Because the QuickSort used in .Net is not a "stable sort". That means
                    that subsequent sorts of the same data are not guaranteed to maintain order
                    when two items are identical (i.e. already sorted.) A stable sort algorithm
                    wouldn't move two items if they are equal and a non-stable sort algorithm
                    would (and does). It gains speed this way but the result is if you sort one
                    column and then sort another column the first sort can change making them
                    unsorted again. It could be that the fellow accomodates this in his code
                    but I couldn't see it.

                    What I did to solve it (some years ago) is introduce a stable sort option
                    which insures that multiple calls maintain earlier sorts. Instead of having
                    to pass an array of values or run it through a special routine you simply
                    ask for something along the lines of: object.SortStab le( item ) on as many
                    items as you want.

                    The code would have to be adapted a bit but if anybody is interested you
                    should be able to find it here: http://www.searchcsla.com/Search.aspx where
                    it was archived. Enter "Sort" for the search criteria and you'll see a
                    couple of threads including "Stable Sort Solved" which is where I posted the
                    code.

                    Seems a shame to have the code go to waste if somebody can use it.

                    Tom


                    "sloan" <sloan@ipass.ne twrote in message
                    news:eQdzNXoNHH A.4604@TK2MSFTN GP06.phx.gbl...
                    See:
                    >
                    Advanced IComparer // Sorting on Multiple Values

                    >
                    <robin9876@hotm ail.comwrote in message
                    news:1168447952 .961485.275940@ i39g2000hsf.goo glegroups.com.. .
                    >I have a list of integers (example sub set of data below), what is the
                    >best method of sorting these in code?
                    >>
                    >1, 872
                    >5, 1283
                    >8, 343
                    >9, 123
                    >>
                    >
                    >

                    Comment

                    Working...