473,387 Members | 1,420 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,387 software developers and data experts.

Sorting an array of ints using a for loop

How is it possible to sort an array of ints using a for loop?

I recently tried the suggestion a newsgroup user offered on how to perform
this, but find that it doesn't work. He suggested the code below:

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

}

The output i get though is:
1,99,66,66

I'd love to find how to get this to work. It's nuts and bolts programming,
that I ought to know.

cheers,
CR


Nov 17 '05 #1
7 14376
Try throwing out all of that extra sort code... and put the following in
there instead:

Array.Sort(list);

Brendan
"CodeRazor" wrote:
How is it possible to sort an array of ints using a for loop?

I recently tried the suggestion a newsgroup user offered on how to perform
this, but find that it doesn't work. He suggested the code below:

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

}

The output i get though is:
1,99,66,66

I'd love to find how to get this to work. It's nuts and bolts programming,
that I ought to know.

cheers,
CR

Nov 17 '05 #2
Brendan, I appreciate your input, but i'm already aware of the Array.Sort()
method.

I'd like to learn how you could sort an array in a loop if you wanted to.

regards,
CR

Nov 17 '05 #3
Oops, sorry about that. I forgot to mention... I’ve run your code several
times on a couple of different PC’s, all with different values in the
array... and each time it sorts the list properly.

For a good reference on different sorting methods, take a look at
http://www.cs.ubc.ca/spider/harrison...ting-demo.html which provides
some source code (Java unfortunately) as well as some nifty java demos that
demonstrate the technique used and it’s relative speed.

When it comes to some good C# examples, take a look at
http://www.codeproject.com/csharp/cssorters.asp.

Brendan
"CodeRazor" wrote:
Brendan, I appreciate your input, but i'm already aware of the Array.Sort()
method.

I'd like to learn how you could sort an array in a loop if you wanted to.

regards,
CR

Nov 17 '05 #4
Oh.

How did the code i posted work? I put a response.write in it to output the
results and they were wrong. Is it a case of just repositioning my
response.write?.....

could you show me exactly how you could get it to output the correct results.

thank you
(BTW, thanks for the links you sent previously)
Nov 17 '05 #5
All I did was add a Console.Writeline() to yours, resulting in:

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

for(int x = 0; x < list.Length; x++)
Console.Write(list[x] + " " );

Brendan

"CodeRazor" wrote:
Oh.

How did the code i posted work? I put a response.write in it to output the
results and they were wrong. Is it a case of just repositioning my
response.write?.....

could you show me exactly how you could get it to output the correct results.

thank you
(BTW, thanks for the links you sent previously)

Nov 17 '05 #6
Brendan, you're a star.

thank you very much.
Nov 17 '05 #7
Normally, the way to do a bubble sort is with two nested loop, rather than
your back'n'forth method. If you add the loop counter in your as it is in
mine, you'll see that this way does less work.
static void Sort2()
{
// int[] list = new int[5]{12,1,105,99,66};
int[] list = new int[]{6,5,4,3,2,1};
int length = list.GetLength(0)-1;
int lp = 0;
for(int top = length; top > 0; --top)
{
for (int i=0;i<top; i++)
{
lp++;
if (list[i] > list[i+1])
{
Console.WriteLine("Comparing {0}, {1}", i, i+1);
int a=list[i+1];
list[i+1]=list[i];
list[i]=a;
}
}
}
for(int x = 0; x < list.Length; x++)
Console.Write(list[x] + " " );

Console.WriteLine("Times through loop: {0}", lp);
}

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:D9**********************************@microsof t.com...
How is it possible to sort an array of ints using a for loop?

I recently tried the suggestion a newsgroup user offered on how to perform
this, but find that it doesn't work. He suggested the code below:

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

}

The output i get though is:
1,99,66,66

I'd love to find how to get this to work. It's nuts and bolts programming, that I ought to know.

cheers,
CR

Nov 17 '05 #8

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

Similar topics

9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
23
by: yatindran | last post by:
hai this is my 2d array. int a = { {5,2,20,1,30,10}, {23,15,7,9,11,3}, {40,50,34,24,14,4}, {9,10,11,12,13,14}, {31,4,18,8,27,17}, {44,32,13,19,41,19}, {1,2,3,4,5,6},
3
by: quiz123 | last post by:
Hi folks, I have a problem. my function call is void Rotate_n(int array, int arraySize,int n); //precondition: arraysize>=1 && n>=0 && n<=arraySize //post condition: Values in the arrary are...
2
by: wing ver ka gundam | last post by:
THanks~ this is the example and my quote are below Write a program in JAVA that allows the user to input 5 ints. The program will then output these five values in reverse order. The program will...
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
5
by: boss1 | last post by:
hi all, i have a problem with loop in select statement.i m using code : <select name = "s" size = "1" > <option selected>P-Code</option> <option...
6
by: =?Utf-8?B?RGFu?= | last post by:
I am reposting a question from about 3 weeks ago ("sorting capability"). I have an aspx page in which I get the data from a database dynamically, through C# code, by creating a dynamic table...
3
by: dondigital | last post by:
I'm trying to figure out the best approach to grab data from an array. So heres the deal. I have byte array called curveData filled will hex bytes that I've downloaded from a unit under test....
3
by: Dan Witten | last post by:
Hi, got a question regarding a MySQL database. The database is for tracking online votes. The table is simple: VoteID VoteDate UserName (of person voting) (Then 8 fields of candidates) if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.