| | |
Bubble Sort
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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.
Cleaner?
C++ Syntax (Toggle Plain Text)
for(int sort = 1; sort == 1;) { sort = 0; for(int i = 0; i < n - 1; i++) { if(a[i] < a[i+1]) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; sort = 1; } } }
Last edited by DigitalPackrat; May 8th, 2008 at 12:51 pm. Reason: errors
[Catchy Signature Here]
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.
a for loop works like this:
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:
C++ Syntax (Toggle Plain Text)
for ( init-expression ; cond-expression ; loop-expression ) { statement }
while(1) or for(;;) 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:
It reads a bit easier.
I prefer another way to write this loop:
cpp Syntax (Toggle Plain Text)
int sorted = 0; while (!sorted) { }
![]() |
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)
- simplified bubble sort c++ (C++)
- Bubble sort & File output jibrish errors??? (C)
Other Threads in the C++ Forum
- Previous Thread: Read File>>>>
- Next Thread: Data base connection with c++
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






