473,394 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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!
Nov 17 '05 #1
6 1637
int[] i = new int[5] { 10, 9, 15, 2, 8 };

Array.Sort(i);

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.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!

Nov 17 '05 #2
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:
int[] i = new int[5] { 10, 9, 15, 2, 8 };

Array.Sort(i);

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.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!


Nov 17 '05 #3
Thaks Adam
Nov 17 '05 #4
Despite looking awful, this method is actually far far quicker than
Array.Sort() !

"Marinus Holkema" <Ma************@discussions.microsoft.com> wrote in
message news:55**********************************@microsof t.com...
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:
int[] i = new int[5] { 10, 9, 15, 2, 8 };

Array.Sort(i);

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.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!


Nov 17 '05 #5
Adam Barker <adam@NO_SP_AM> wrote:
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[i] = 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[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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
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:
Despite looking awful, this method is actually far far quicker than
Array.Sort() !

"Marinus Holkema" <Ma************@discussions.microsoft.com> wrote in
message news:55**********************************@microsof t.com...
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:
int[] i = new int[5] { 10, 9, 15, 2, 8 };

Array.Sort(i);

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.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!


Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Matt Fletcher | last post by:
Hi guys, I am trying to allow the models in a mysql database to be ordered by the site owner. I was thinking along the lines of a <SELECT> list containing the model names and Up and Down...
2
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need...
6
by: Brendan.Collins | last post by:
Hi I have a javascript problem that has been annoying me for two days now and thought that a javascript expert might have the magic solution. I am populating a table dynamically from the...
2
by: Ken Durden | last post by:
Is there any way to control ordering of items in intellisense via attributes? For example, I have the following enum: public enum ESeverity { Acceptable, Low, Medium,
3
by: Stimp | last post by:
I have a listbox of values that I populate from a database. I want the user to be able to re-order the list (by first selecting an item and then clicking 'up' or 'down' buttons) and then save...
3
by: marc | last post by:
I want to create a strongly typed collection and use DictionaryBase I add a number of objects but when i loop through the list the data is ordered in a strange maner. How is this possible? Can...
21
by: John Salerno | last post by:
If I want to make a list of four items, e.g. L = , and then figure out if a certain element precedes another element, what would be the best way to do that? Looking at the built-in list...
0
by: bappelsin | last post by:
Hello, I have a problem with the datagridview. I have a view with around 25 different columns. To make life easier for the users I have implemented a popup dialog where the user can select which...
5
by: =?Utf-8?B?VG9t?= | last post by:
Cannot not seem to make any sense of the order that my key/values end up in when added to the Hashtable...ideally, I would like to be able to sort the keys/values...but not thinking it is possible....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.