944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 66420
  • C++ RSS
Nov 21st, 2006
0

simplified bubble sort c++

Expand Post »
hi guys ...

this is a bubble sort program that i made
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #include<iomanip>
  5. using std::setw;
  6.  
  7. int main()
  8.  
  9. {
  10. const int arraysize = 10;
  11. int Bubble[ arraysize ];
  12.  
  13. cout << "Please enter 10 integers: " << endl;
  14.  
  15. for ( int i = 0; i < arraysize; i++ )
  16. cin >> Bubble[ i ];
  17.  
  18.  
  19. cout <<"unsorted array:\n";
  20.  
  21.  
  22. for ( int j = 0; j < arraysize; j++ )
  23. cout << setw( 4 ) << Bubble[ j ];
  24.  
  25. for(int y=0; y < arraysize; y++)
  26.  
  27. {
  28.  
  29. for ( int k =0; k < arraysize -1-y; k++ )
  30.  
  31. if(Bubble[k]>Bubble[k+1])
  32.  
  33. {
  34.  
  35. int temp = Bubble[k+1];
  36.  
  37. Bubble[k+1] = Bubble[k];
  38.  
  39. Bubble[k] = temp;
  40.  
  41. }
  42.  
  43. }
  44.  
  45.  
  46. cout << "\nSorted Bubble array:\n";
  47.  
  48. for (int l = 0; l < arraysize; l++ )
  49. cout << setw( 4 ) << Bubble[ l ];
  50.  
  51. cout << endl;
  52. return 0;
  53. }

now the problem i am getting is that :
the data in the array may be already in the proper order or near proper order, this can be done in fewer passes than nine...
how can i modify this sort to check at the end of each pass if any swaps have been made. if none have been made then the data must been in proper order so the program should terminate if the swap has been made then atleast one more pass is needed


please helpp

thank you
Last edited by ~s.o.s~; Nov 21st, 2006 at 2:37 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1e9v85 is offline Offline
11 posts
since Nov 2006
Nov 21st, 2006
0

Re: simplified bubble sort c++

create a bool flag before the loop starts and set it to false. when a swap is made change the flag to true. on next loop iteration if the flag is still false then no swaps were performed during the previous loop iteration.

and please next time use code tags.
Last edited by Ancient Dragon; Nov 21st, 2006 at 2:40 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 21st, 2006
0

Re: simplified bubble sort c++

You cant as such check for such conditions in case of a simple sort like bubble sort.

Take for eg. this array.
1 2 3 4 5 6 8 7

During the first seven passes no swap is performed since the next number is always greater than the current but a final swap is done in the last pass to sort the array. You cant as such terminate a bubble sort based on the flag that swap is performed or not.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 22nd, 2006
0

Re: simplified bubble sort c++

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
You cant as such check for such conditions in case of a simple sort like bubble sort.

Take for eg. this array.
1 2 3 4 5 6 8 7

During the first seven passes no swap is performed since the next number is always greater than the current but a final swap is done in the last pass to sort the array. You cant as such terminate a bubble sort based on the flag that swap is performed or not.
Yes you can. You set the swap flag to FALSE between the two loops (inside the outer loop, before the inner loop). When the inner loop exits and a swap has been made, the swap flag should be TRUE. If no swap has been made, the flag should be FALSE and it's safe to exit.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Dec 1st, 2010
0
Re: simplified bubble sort c++
what does this program do?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
z3r0acidk is offline Offline
4 posts
since Jun 2010

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: New Operator, and std::bad_alloc
Next Thread in C++ Forum Timeline: case insensitive search help





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


Follow us on Twitter


© 2011 DaniWeb® LLC