| | |
simplified bubble sort c++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 11
Reputation:
Solved Threads: 0
hi guys ...
this is a bubble sort program that i made
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
this is a bubble sort program that i made
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; #include<iomanip> using std::setw; int main() { const int arraysize = 10; int Bubble[ arraysize ]; cout << "Please enter 10 integers: " << endl; for ( int i = 0; i < arraysize; i++ ) cin >> Bubble[ i ]; cout <<"unsorted array:\n"; for ( int j = 0; j < arraysize; j++ ) cout << setw( 4 ) << Bubble[ j ]; for(int y=0; y < arraysize; y++) { for ( int k =0; k < arraysize -1-y; k++ ) if(Bubble[k]>Bubble[k+1]) { int temp = Bubble[k+1]; Bubble[k+1] = Bubble[k]; Bubble[k] = temp; } } cout << "\nSorted Bubble array:\n"; for (int l = 0; l < arraysize; l++ ) cout << setw( 4 ) << Bubble[ l ]; cout << endl; return 0; }
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.
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.
and please next time use code tags.
Last edited by Ancient Dragon; Nov 21st, 2006 at 2:40 pm.
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.
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.
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.
I don't accept change; I don't deserve to live.
•
•
•
•
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- bubble sort function w/ array (C++)
- How do i change this JAVA Insertion sort object to Bubble Sort object (Java)
- Bubble Sort of a file of records in Pascal pls (Pascal and Delphi)
- Bubble sort & File output jibrish errors??? (C)
Other Threads in the C++ Forum
- Previous Thread: c++ recursive palindrome function
- Next Thread: Need help with "error LNK2001 unresolved external symbol"
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






