Sorting an array of ints using a for loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CodeRazor

    Sorting an array of ints using a for loop

    How is it possible to sort an array of ints using a for loop?

    I recently tried the suggestion a newsgroup user offered on how to perform
    this, but find that it doesn't work. He suggested the code below:

    int[] list = new int[5]{12,1,105,99,66 };
    int length = list.GetLength( 0)-1;
    for (int i=0;i<length;)
    {
    if (list[i] > list[i+1])
    {
    int a=list[i+1];
    list[i+1]=list[i];
    list[i]=a;
    if (i>0)
    i--;
    }
    else
    i++;

    }

    The output i get though is:
    1,99,66,66

    I'd love to find how to get this to work. It's nuts and bolts programming,
    that I ought to know.

    cheers,
    CR




  • Brendan Grant

    #2
    RE: Sorting an array of ints using a for loop

    Try throwing out all of that extra sort code... and put the following in
    there instead:

    Array.Sort(list );

    Brendan


    "CodeRazor" wrote:
    [color=blue]
    > How is it possible to sort an array of ints using a for loop?
    >
    > I recently tried the suggestion a newsgroup user offered on how to perform
    > this, but find that it doesn't work. He suggested the code below:
    >
    > int[] list = new int[5]{12,1,105,99,66 };
    > int length = list.GetLength( 0)-1;
    > for (int i=0;i<length;)
    > {
    > if (list[i] > list[i+1])
    > {
    > int a=list[i+1];
    > list[i+1]=list[i];
    > list[i]=a;
    > if (i>0)
    > i--;
    > }
    > else
    > i++;
    >
    > }
    >
    > The output i get though is:
    > 1,99,66,66
    >
    > I'd love to find how to get this to work. It's nuts and bolts programming,
    > that I ought to know.
    >
    > cheers,
    > CR
    >
    >
    >
    >[/color]

    Comment

    • CodeRazor

      #3
      RE: Sorting an array of ints using a for loop

      Brendan, I appreciate your input, but i'm already aware of the Array.Sort()
      method.

      I'd like to learn how you could sort an array in a loop if you wanted to.

      regards,
      CR

      Comment

      • Brendan Grant

        #4
        RE: Sorting an array of ints using a for loop

        Oops, sorry about that. I forgot to mention... I’ve run your code several
        times on a couple of different PC’s, all with different values in the
        array... and each time it sorts the list properly.

        For a good reference on different sorting methods, take a look at
        http://www.cs.ubc.ca/spider/harrison...ting-demo.html which provides
        some source code (Java unfortunately) as well as some nifty java demos that
        demonstrate the technique used and it’s relative speed.

        When it comes to some good C# examples, take a look at
        http://www.codeproject.com/csharp/cssorters.asp.

        Brendan


        "CodeRazor" wrote:
        [color=blue]
        > Brendan, I appreciate your input, but i'm already aware of the Array.Sort()
        > method.
        >
        > I'd like to learn how you could sort an array in a loop if you wanted to.
        >
        > regards,
        > CR
        >[/color]

        Comment

        • CodeRazor

          #5
          RE: Sorting an array of ints using a for loop

          Oh.

          How did the code i posted work? I put a response.write in it to output the
          results and they were wrong. Is it a case of just repositioning my
          response.write? .....

          could you show me exactly how you could get it to output the correct results.

          thank you
          (BTW, thanks for the links you sent previously)

          Comment

          • Brendan Grant

            #6
            RE: Sorting an array of ints using a for loop

            All I did was add a Console.Writeli ne() to yours, resulting in:

            int[] list = new int[5]{12,1,105,99,66 };
            int length = list.GetLength( 0)-1;
            for (int i=0;i<length;)
            {
            if (list[i] > list[i+1])
            {
            int a=list[i+1];
            list[i+1]=list[i];
            list[i]=a;
            if (i>0)
            i--;
            }
            else
            i++;
            }

            for(int x = 0; x < list.Length; x++)
            Console.Write(l ist[x] + " " );

            Brendan

            "CodeRazor" wrote:
            [color=blue]
            > Oh.
            >
            > How did the code i posted work? I put a response.write in it to output the
            > results and they were wrong. Is it a case of just repositioning my
            > response.write? .....
            >
            > could you show me exactly how you could get it to output the correct results.
            >
            > thank you
            > (BTW, thanks for the links you sent previously)[/color]

            Comment

            • CodeRazor

              #7
              RE: Sorting an array of ints using a for loop

              Brendan, you're a star.

              thank you very much.

              Comment

              • James Curran

                #8
                Re: Sorting an array of ints using a for loop

                Normally, the way to do a bubble sort is with two nested loop, rather than
                your back'n'forth method. If you add the loop counter in your as it is in
                mine, you'll see that this way does less work.


                static void Sort2()
                {
                // int[] list = new int[5]{12,1,105,99,66 };
                int[] list = new int[]{6,5,4,3,2,1};
                int length = list.GetLength( 0)-1;
                int lp = 0;
                for(int top = length; top > 0; --top)
                {
                for (int i=0;i<top; i++)
                {
                lp++;
                if (list[i] > list[i+1])
                {
                Console.WriteLi ne("Comparing {0}, {1}", i, i+1);
                int a=list[i+1];
                list[i+1]=list[i];
                list[i]=a;
                }
                }
                }
                for(int x = 0; x < list.Length; x++)
                Console.Write(l ist[x] + " " );

                Console.WriteLi ne("Times through loop: {0}", lp);
                }

                --
                --
                Truth,
                James Curran
                [erstwhile VC++ MVP]

                Home: www.noveltheory.com Work: www.njtheater.com
                Blog: www.honestillusion.com Day Job: www.partsearch.com

                "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
                news:D9E57733-772C-4E2A-B842-107F65199D59@mi crosoft.com...[color=blue]
                > How is it possible to sort an array of ints using a for loop?
                >
                > I recently tried the suggestion a newsgroup user offered on how to perform
                > this, but find that it doesn't work. He suggested the code below:
                >
                > int[] list = new int[5]{12,1,105,99,66 };
                > int length = list.GetLength( 0)-1;
                > for (int i=0;i<length;)
                > {
                > if (list[i] > list[i+1])
                > {
                > int a=list[i+1];
                > list[i+1]=list[i];
                > list[i]=a;
                > if (i>0)
                > i--;
                > }
                > else
                > i++;
                >
                > }
                >
                > The output i get though is:
                > 1,99,66,66
                >
                > I'd love to find how to get this to work. It's nuts and bolts[/color]
                programming,[color=blue]
                > that I ought to know.
                >
                > cheers,
                > CR
                >
                >
                >
                >[/color]


                Comment

                Working...