Ordering items in an array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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 > list[i+1])
{
int a=list[i+1];
list[i+1]=list;
list=a;
if (i>0)
i--;
}
else
i++;

}







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

Array.Sort(i);

CodeRazor said:
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!
 
Despite looking awful, this method is actually far far quicker than
Array.Sort() !

Marinus Holkema said:
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 > list[i+1])
{
int a=list[i+1];
list[i+1]=list;
list=a;
if (i>0)
i--;
}
else
i++;

}







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

Array.Sort(i);

CodeRazor said:
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 said:
Despite looking awful, this method is actually far far quicker than
Array.Sort() !

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 = rng.Next(size);
}

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

DateTime start = DateTime.Now;
Array.Sort(testData);
DateTime end = DateTime.Now;
Console.WriteLine ("Array.Sort: {0}", end-start);

start = DateTime.Now;
BubbleSort(testData2);
end = DateTime.Now;
Console.WriteLine ("BubbleSort: {0}", end-start);
}

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

}
}
}
 
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 said:
Despite looking awful, this method is actually far far quicker than
Array.Sort() !

Marinus Holkema said:
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 > list[i+1])
{
int a=list[i+1];
list[i+1]=list;
list=a;
if (i>0)
i--;
}
else
i++;

}







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

Array.Sort(i);

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!

 
Back
Top