Sorting an array of ints using a for loop

G

Guest

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 > list[i+1])
{
int a=list[i+1];
list[i+1]=list;
list=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
 
G

Guest

Try throwing out all of that extra sort code... and put the following in
there instead:

Array.Sort(list);

Brendan
 
G

Guest

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
 
G

Guest

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/Java/sorting-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
 
G

Guest

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)
 
G

Guest

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

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

Brendan
 
J

James Curran

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 > list[i+1])
{
Console.WriteLine("Comparing {0}, {1}", i, i+1);
int a=list[i+1];
list[i+1]=list;
list=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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top