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 7 14401
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
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
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
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)
All I did was add a Console.Writeli ne() 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(l ist[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)
Brendan, you're a star.
thank you very much.
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.WriteLi ne("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(l ist[x] + " " );
Console.WriteLi ne("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*******@disc ussions.microso ft.com> wrote in message
news:D9******** *************** ***********@mic rosoft.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
}
|
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},
|
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 rotated forward(left to right)
by n positions.
eg: if the original array is {1,2,3,4,5}
then, rotate_n(array,5,1); will make it{5,1,2,3,4}
my problem is, how do i manipulate the array without using temp??
Can anyone give me a pointer as to how i...
|
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 then output the values in order with a statement on how many of the remaining values are larger, smaller, or equal.
Example 1:
Input:
1
2
3
4
|
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 added. I'm not sure if MovePrevious is the best way to accomplish this. Here's an example of my code:
With rs
.MoveFirst
strData = Trim(!data)
Do While Not .EOF
...
| |
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 value="P01">P01 </option>
<option value="P02">P02 </option>
<option value="P03">P03 </option>
<option value="P04">P04 </option>
</select></td>
|
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 using TableHeaderCell and
TableHeaderRow.
The data is binded to the table by using a DataSet and Data Reader.
The responses on the forum made me think that it would be easier to sort the
data in the table (when clicking on the header column), by...
|
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. Basically, I am trying to loop through this array of 256 bytes. I want to grab the 1st byte (of a 4 byte chunk), so , then and so on…so every 4th byte starting with the first and throwing them into the rpmArray. Then I want to grab the bytes in between, ...
|
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 voted for field will contain a YES otherwise it's blank.
VoteSara
VoteDan
VoteTom
|
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...
|
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...
| |
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |