Bubble Sort

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

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

Bubble Sort

 
0
  #1
May 8th, 2008
Can anyone explain how bubble sort work or give an example or a simple code that is bubble sorting

Thanx in advance
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: Aug 2005
Posts: 15,509
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: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Bubble Sort

 
0
  #2
May 8th, 2008
Took about 10 seconds to find the answer here Here
Last edited by Ancient Dragon; May 8th, 2008 at 9:43 am.
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: Oct 2006
Posts: 2,936
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: 305
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Bubble Sort

 
0
  #3
May 8th, 2008
It's explained very well here
If you know some c++, it won't be too hard to write.
Come back if you have problems (and code)
Last edited by niek_e; May 8th, 2008 at 9:43 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
  #4
May 8th, 2008
Does it alwayz work like that, is thats how it is or there are any other ways to do it except the endless list of ununderstandable for statements
Last edited by Traicey; May 8th, 2008 at 9:54 am.
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: Aug 2005
Posts: 15,509
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: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Bubble Sort

 
0
  #5
May 8th, 2008
The bubble sort is probably the very easiest sort algorithm there is. What is so difficult to understand about this:
  1. for (i=0; i<n-1; i++) {
  2. for (j=0; j<n-1-i; j++)
  3. if (a[j+1] < a[j]) { /* compare the two neighbors */
  4. tmp = a[j]; /* swap a[j] and a[j+1] */
  5. a[j] = a[j+1];
  6. a[j+1] = tmp;
  7. }
  8. }

I don't really use the above algorithm, but this one
  1. for(int i = 0; i < n-1; i++)
  2. {
  3. for(int j = 0; i < n; j++)
  4. {
  5. if( a[i] < a[j] )
  6. {
  7. int temp = a[i];
  8. a[i] = a[j];
  9. a[j] = temp;
  10. }
  11. }
  12. }

And in C++ you can do it in just one line: But I doubt this is a satisfactory solution to your assignment.
  1. vector<int> array;
  2. // fill array is not shown
  3. //
  4. // now sort it
  5. std::sort(array.begin(),array.end());
Last edited by Ancient Dragon; May 8th, 2008 at 10:10 am.
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: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Bubble Sort

 
1
  #6
May 8th, 2008
> What is so difficult to understand about this:
Is that a trick question? Of course it's difficult to understand if you haven't learned how it works yet. Edward learned long ago that just because something is easy for her, it's not easy for everyone else, and vice versa.
If at first you don't succeed, keep on sucking until you do succeed.
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
  #7
May 8th, 2008
Yaeh!!!! I have seen all that maybe I just dont want to understand it but honestly I would be lieying I I say I understand all o that, I just wish to know why for (i=0; i<n-1; i++) {for (j=0; j<n-1-i; j++) are we substracting 1 and - 1 - i what does that mean, if Dragon U can be kind enough to explain that to me, I would go home and rest
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,936
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: 305
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Bubble Sort

 
0
  #8
May 8th, 2008
Originally Posted by Ancient Dragon View Post

I don't really use the above algorithm, but this one
  1. for(int i = 0; i < n-1; i++)
  2. {
  3. for(int j = 0; i < n; j++)
  4. {
  5. if( a[i] < a[j] )
  6. {
  7. int temp = a[i];
  8. a[i] = a[j];
  9. a[j] = temp;
  10. }
  11. }
  12. }
Correct me if I'm wrong but shouldn't this: for(int j = 0; i < n; j++) be:
for(int j = 0; j< n; j++) Or else you will get out of array-boundary => crash?
Last edited by niek_e; May 8th, 2008 at 10:26 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Bubble Sort

 
0
  #9
May 8th, 2008
Excuse me, wont the second code make an infinte loop, I mean, 'i' will be less than n the whole time
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
  #10
May 8th, 2008
Thank you for understanding Edward I thought I was just being defficult here or was just scared to think out of the box
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