bubble sort

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

Join Date: Apr 2008
Posts: 27
Reputation: Lotus_2011 is an unknown quantity at this point 
Solved Threads: 0
Lotus_2011 Lotus_2011 is offline Offline
Light Poster

bubble sort

 
0
  #1
May 18th, 2008
Hi,
Can anyone help me in this code bubble sort in c++,,,
this is my code.......
  1. void main()
  2. {
  3. int A[5]={7,2,10,15,3};
  4. int i,j,swap;
  5. for(j=0;j<5;j++)
  6. {
  7. for(i=0;i<5;i++)
  8. {
  9. if(A[i]>A[i+1])
  10. {
  11. swap=A[i];
  12. A[i]=A[i+1];
  13. A[i+1]=swap;
  14. }
  15. }
  16.  
  17. }
  18. for(i=0;i<5;i++)
  19. cout<<A[i]<<endl;
  20.  
  21. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: bubble sort

 
0
  #2
May 18th, 2008
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).
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 14
Reputation: scarface3288 is an unknown quantity at this point 
Solved Threads: 1
scarface3288 scarface3288 is offline Offline
Newbie Poster

Re: bubble sort

 
0
  #3
May 18th, 2008
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

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:
  1. start at address 0 of the A[i] interger array
  2. if A[0] (which is 7 ) is greater than A[1](2) switch positions--true
  3. then next start at A[1] (which is now 7) if A[1] greater than A[2] switch --false so it moves on
  4. ----
  5. 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: bubble sort

 
0
  #4
May 18th, 2008
http://en.wikipedia.org/wiki/Bubble_sort shows CORRECT bubble-sort algorithm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 27
Reputation: Lotus_2011 is an unknown quantity at this point 
Solved Threads: 0
Lotus_2011 Lotus_2011 is offline Offline
Light Poster

Re: bubble sort

 
0
  #5
May 18th, 2008
-hi ,sorry for my way in explaning my problem.
-So I knew my default in my code ,
Its the second for loop
it should be for(i=0;i<4;i++),As i compare between A[i]&A[i+1],
-Another thing this is bubble sort
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