473,809 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1655
int[] i = new int[5] { 10, 9, 15, 2, 8 };

Array.Sort(i);

"CodeRazor" <Co*******@disc ussions.microso ft.com> wrote in message
news:50******** *************** ***********@mic rosoft.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*******@disc ussions.microso ft.com> wrote in message
news:50******** *************** ***********@mic rosoft.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.mi crosoft.com> wrote in
message news:55******** *************** ***********@mic rosoft.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*******@disc ussions.microso ft.com> wrote in message
news:50******** *************** ***********@mic rosoft.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_A M> 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(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 - <sk***@pobox.co m>
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.mi crosoft.com> wrote in
message news:55******** *************** ***********@mic rosoft.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*******@disc ussions.microso ft.com> wrote in message
news:50******** *************** ***********@mic rosoft.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
1701
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 buttons to move the models up and down the list. I am unable to come up with a solution for getting the order from the list however.
2
587
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 to provide users with a mechanism to change the order of items that are listed on the page.For example, the _New York Times_ website (http://www.nytimes.com) lists a bunch of top news articles, and normally these are ordered by purely mechanical...
6
6490
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 database and I am trying to allow the user to order the rows in the table using up and down arrows. The function I am using is:
2
1847
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
6108
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 the list back to the database. First of all, I implemented the re-ordering through client-side javascript, which worked great (also it didn't require a postback every time an item was re-ordered). For testing purposes, I'm outputting the contents...
3
1441
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 anybody tell my why the order gets changed? Thanks Marc '***************** OUTPUT **************************
21
1664
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 functions, I thought I could do something like: if L.index('A') < L.index('D'): # do some stuff But I didn't know if maybe there was a preferred method for this type of
0
1959
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 columns that should be displayed and in which order the columns are shown. After user has selected the visible columns and the order he presses ok. The grid is first cleared from rows, then I add all visible columns and then all hidden. Grid is...
5
2672
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. For those who are sure going to ask why I am asking this....I use the Hashtable for conveniently cross-referencing pairs of information...and I am now trying to populate a combo box based on the hashtable contents using the key as the combo...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.