| | |
bubble sort
![]() |
•
•
Join Date: Apr 2008
Posts: 27
Reputation:
Solved Threads: 0
Hi,
Can anyone help me in this code bubble sort in c++,,,
this is my code.......
Can anyone help me in this code bubble sort in c++,,,
this is my code.......
C++ Syntax (Toggle Plain Text)
void main() { int A[5]={7,2,10,15,3}; int i,j,swap; for(j=0;j<5;j++) { for(i=0;i<5;i++) { if(A[i]>A[i+1]) { swap=A[i]; A[i]=A[i+1]; A[i+1]=swap; } } } for(i=0;i<5;i++) cout<<A[i]<<endl; }
So what's wrong with it?
- no results
- all the numbers the same?
- it isn't sorting at all?
- sorts in the wrong order?
- it doesn't compile (hint, void main is wrong to begin with).
Show some analysis of the problem as well, don't just dump the code, say "it doesn't work" and expect people to do the rest of the work (and guess what you really wanted).
- no results
- all the numbers the same?
- it isn't sorting at all?
- sorts in the wrong order?
- it doesn't compile (hint, void main is wrong to begin with).
Show some analysis of the problem as well, don't just dump the code, say "it doesn't work" and expect people to do the rest of the work (and guess what you really wanted).
•
•
Join Date: Apr 2007
Posts: 14
Reputation:
Solved Threads: 1
is it outputting
2
7
10
3
15
I bet it is, you wonder what might be the problem now huh?
Writing out in words what your doing goes a long way
to me this means:
2
7
10
3
15
I bet it is, you wonder what might be the problem now huh?
Writing out in words what your doing goes a long way
for(j=0;j<5;j++)
{
for(i=0;i<5;i++)
{
if(A[i]>A[i+1])
{
swap=A[i];
A[i]=A[i+1];
A[i+1]=swap;
}
}
}to me this means:
- start at address 0 of the A[i] interger array
- if A[0] (which is 7 ) is greater than A[1](2) switch positions--true
- then next start at A[1] (which is now 7) if A[1] greater than A[2] switch --false so it moves on
- ----
- The problem occurs here if A[3] (15) greater than A[4] (3) switch. -- so that leaves you with the output above, the swap function you have is correct but it should not be in that conditional statement. Think harder.
Last edited by scarface3288; May 18th, 2008 at 3:41 am.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
http://en.wikipedia.org/wiki/Bubble_sort shows CORRECT bubble-sort algorithm.
![]() |
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: Iterator assertion failure. How?
- Next Thread: Game of Life. Please Help!
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






