943,704 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2826
  • C++ RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
May 8th, 2008
-1

Re: Bubble Sort

The School of Hard Knocks is within each of us. Its experience, and millions of hours testing various algorithms to find the most efficient.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
May 8th, 2008
0

Re: Bubble Sort

But thats time-consuming and probably suitable for people who are not handy-fully
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
May 8th, 2008
1

Re: Bubble Sort

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 Nick Evan; May 8th, 2008 at 11:16 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 8th, 2008
0

Re: Bubble Sort

Oh well, easier said than done, but I will try
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
May 8th, 2008
1

Re: Bubble Sort

Cleaner?
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Light Poster
DigitalPackrat is offline Offline
33 posts
since Jan 2008
May 12th, 2008
0

Re: Bubble Sort

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???
Quote ...
for(int sort = 1; sort == 1; )
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
May 12th, 2008
0

Re: Bubble Sort

a for loop works like this:
C++ Syntax (Toggle Plain Text)
  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(;;)
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 12th, 2008
0

Re: Bubble Sort

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++
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
May 12th, 2008
0

Re: Bubble Sort

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:
cpp Syntax (Toggle Plain Text)
  1. int sorted = 0;
  2. while (!sorted)
  3. {
  4. }
It reads a bit easier.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 12th, 2008
0

Re: Bubble Sort

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
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Read File>>>>
Next Thread in C++ Forum Timeline: Data base connection with c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC