Bubble Sort

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Bubble Sort

 
-1
  #21
May 8th, 2008
The School of Hard Knocks is within each of us. Its experience, and millions of hours testing various algorithms to find the most efficient.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Bubble Sort

 
0
  #22
May 8th, 2008
But thats time-consuming and probably suitable for people who are not handy-fully
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,833
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Bubble Sort

 
1
  #23
May 8th, 2008
Depends on how much sleep you need.

You can always stick a finger in a powersocket and hope your brain gets somehow rearranged (improved), but my instinct is telling me that the change of succes will be greater if you just practice , practice and practice
Last edited by niek_e; May 8th, 2008 at 11:16 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Bubble Sort

 
0
  #24
May 8th, 2008
Oh well, easier said than done, but I will try
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 33
Reputation: DigitalPackrat is an unknown quantity at this point 
Solved Threads: 1
DigitalPackrat's Avatar
DigitalPackrat DigitalPackrat is offline Offline
Light Poster

Re: Bubble Sort

 
1
  #25
May 8th, 2008
Cleaner?
  1. for(int sort = 1; sort == 1;) {
  2.  
  3. sort = 0;
  4.  
  5. for(int i = 0; i < n - 1; i++) {
  6.  
  7. if(a[i] < a[i+1]) {
  8.  
  9. int temp = a[i];
  10. a[i] = a[i+1];
  11. a[i+1] = temp;
  12.  
  13. sort = 1;
  14. }
  15. }
  16. }
Last edited by DigitalPackrat; May 8th, 2008 at 12:51 pm. Reason: errors
[Catchy Signature Here]
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Bubble Sort

 
0
  #26
May 12th, 2008
I alwayz thought that there is only one way to do for loop statement which is for(int x = 0; x < 10; x++) but what DigitalPackrat did demonstrate something else and it also proves me wrong..... OK now can you please explain to me what that for loop does or this for loop does???
for(int sort = 1; sort == 1; )
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,833
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Bubble Sort

 
0
  #27
May 12th, 2008
a for loop works like this:
  1. for ( init-expression ; cond-expression ; loop-expression )
  2. {
  3. statement
  4. }
Your example only uses the init-expression amd cond-expression. So if 'sort' isn't changed in the statement-block this is an endless loop (you set 'sort' to 1, then loop while it's 1). This is normally written as:
while(1) or for(;;)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Bubble Sort

 
0
  #28
May 12th, 2008
So all in all u are saying I cant use for loop the way DigitalPackrat Did, that means what he demonstrated is unacceptable or illigal in C++
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,833
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Bubble Sort

 
0
  #29
May 12th, 2008
Sure you can. DigitalPackrat sets sort to 0 in the loop, so it won't be endless. I was just trying to give you an insite on for-loops .
I prefer another way to write this loop:
  1. int sorted = 0;
  2. while (!sorted)
  3. {
  4. }
It reads a bit easier.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Bubble Sort

 
0
  #30
May 12th, 2008
Ok thanx, I just wanted to know coz really I thought there is no other way to do a for loop except the one I alwayz see
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC