Ordering items in an array

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

    Ordering items in an array

    My array is

    int[] list = new int[3]{20,99,6};

    Using a for-loop, how can I order the output of this array. I don't want to
    use a sorted list.

    I know this is straightforward but can't figure it out.

    thank you!
  • Adam Barker

    #2
    Re: Ordering items in an array

    int[] i = new int[5] { 10, 9, 15, 2, 8 };

    Array.Sort(i);

    "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
    news:50077FB1-577F-49D9-81F6-74B42E2B123A@mi crosoft.com...[color=blue]
    > My array is
    >
    > int[] list = new int[3]{20,99,6};
    >
    > Using a for-loop, how can I order the output of this array. I don't want
    > to
    > use a sorted list.
    >
    > I know this is straightforward but can't figure it out.
    >
    > thank you![/color]


    Comment

    • Marinus Holkema

      #3
      Re: Ordering items in an array

      But if you realy, realy need a for-loop:

      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++;

      }







      "Adam Barker" wrote:
      [color=blue]
      > int[] i = new int[5] { 10, 9, 15, 2, 8 };
      >
      > Array.Sort(i);
      >
      > "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
      > news:50077FB1-577F-49D9-81F6-74B42E2B123A@mi crosoft.com...[color=green]
      > > My array is
      > >
      > > int[] list = new int[3]{20,99,6};
      > >
      > > Using a for-loop, how can I order the output of this array. I don't want
      > > to
      > > use a sorted list.
      > >
      > > I know this is straightforward but can't figure it out.
      > >
      > > thank you![/color]
      >
      >
      >[/color]

      Comment

      • CodeRazor

        #4
        Re: Ordering items in an array

        Thaks Adam

        Comment

        • Adam Barker

          #5
          Re: Ordering items in an array

          Despite looking awful, this method is actually far far quicker than
          Array.Sort() !

          "Marinus Holkema" <MarinusHolkema @discussions.mi crosoft.com> wrote in
          message news:55984DB2-12AA-492D-99CE-4A1C1695120C@mi crosoft.com...[color=blue]
          > But if you realy, realy need a for-loop:
          >
          > 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++;
          >
          > }
          >
          >
          >
          >
          >
          >
          >
          > "Adam Barker" wrote:
          >[color=green]
          >> int[] i = new int[5] { 10, 9, 15, 2, 8 };
          >>
          >> Array.Sort(i);
          >>
          >> "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
          >> news:50077FB1-577F-49D9-81F6-74B42E2B123A@mi crosoft.com...[color=darkred]
          >> > My array is
          >> >
          >> > int[] list = new int[3]{20,99,6};
          >> >
          >> > Using a for-loop, how can I order the output of this array. I don't
          >> > want
          >> > to
          >> > use a sorted list.
          >> >
          >> > I know this is straightforward but can't figure it out.
          >> >
          >> > thank you![/color]
          >>
          >>
          >>[/color][/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Ordering items in an array

            Adam Barker <adam@NO_SP_A M> wrote:[color=blue]
            > Despite looking awful, this method is actually far far quicker than
            > Array.Sort() ![/color]

            While it may be quicker to do a bubble sort for a few items, it gets
            *much* slower over larger arrays.

            Try the following program with different sizes. On my box, an array
            with 100,000 elements took 0.06s to sort with Array.Sort, and 13.8s to
            sort with the bubble sort. Of course, the time taken will depend on the
            actual data (a bubble sort is very quick on data which is already
            sorted).

            using System;

            class Test
            {
            static void Main(string[] args)
            {
            int size = int.Parse(args[0]);

            int[] testData = new int[size];
            Random rng = new Random();
            for (int i=0; i < size; i++)
            {
            testData[i] = rng.Next(size);
            }

            int[] testData2 = (int[]) testData.Clone( );

            DateTime start = DateTime.Now;
            Array.Sort(test Data);
            DateTime end = DateTime.Now;
            Console.WriteLi ne ("Array.Sort : {0}", end-start);

            start = DateTime.Now;
            BubbleSort(test Data2);
            end = DateTime.Now;
            Console.WriteLi ne ("BubbleSort : {0}", end-start);
            }

            static void BubbleSort (int[] array)
            {
            int length = array.GetLength (0)-1;
            for (int i=0;i<length;)
            {
            if (array[i] > array[i+1])
            {
            int a=array[i+1];
            array[i+1]=array[i];
            array[i]=a;
            if (i>0)
            i--;
            }
            else
            i++;

            }
            }
            }


            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Daniel Jin

              #7
              Re: Ordering items in an array

              it's really not. bubble sort has a running time of O(n^2). while
              Array.Sort() uses quick sort which has the same worst time scenario, but
              typically you can expect an average of n * log n, which works great on large
              list as Jon has demonstrated.

              "Adam Barker" wrote:
              [color=blue]
              > Despite looking awful, this method is actually far far quicker than
              > Array.Sort() !
              >
              > "Marinus Holkema" <MarinusHolkema @discussions.mi crosoft.com> wrote in
              > message news:55984DB2-12AA-492D-99CE-4A1C1695120C@mi crosoft.com...[color=green]
              > > But if you realy, realy need a for-loop:
              > >
              > > 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++;
              > >
              > > }
              > >
              > >
              > >
              > >
              > >
              > >
              > >
              > > "Adam Barker" wrote:
              > >[color=darkred]
              > >> int[] i = new int[5] { 10, 9, 15, 2, 8 };
              > >>
              > >> Array.Sort(i);
              > >>
              > >> "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
              > >> news:50077FB1-577F-49D9-81F6-74B42E2B123A@mi crosoft.com...
              > >> > My array is
              > >> >
              > >> > int[] list = new int[3]{20,99,6};
              > >> >
              > >> > Using a for-loop, how can I order the output of this array. I don't
              > >> > want
              > >> > to
              > >> > use a sorted list.
              > >> >
              > >> > I know this is straightforward but can't figure it out.
              > >> >
              > >> > thank you!
              > >>
              > >>
              > >>[/color][/color]
              >
              >
              >[/color]

              Comment

              Working...