440,640 Members | 1,581 Online
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

# Ordering items in an array

 P: n/a 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 Replies

 P: n/a int[] i = new int[5] { 10, 9, 15, 2, 8 }; Array.Sort(i); "CodeRazor" 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

 P: n/a 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 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" 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

 P: n/a Thaks Adam Nov 17 '05 #4

 P: n/a Despite looking awful, this method is actually far far quicker than Array.Sort() ! "Marinus Holkema" 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 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" 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

 P: n/a Adam Barker 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 array[i+1]) { int a=array[i+1]; array[i+1]=array[i]; array[i]=a; if (i>0) i--; } else i++; } } } -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 17 '05 #6

 P: n/a 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" 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 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" 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 discussion thread is closed

Replies have been disabled for this discussion.