Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 22nd, 2005, 12:33 PM
nabil1983 nabil1983 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 36 nabil1983 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 7 m 44 sec
Reputation Power: 4
Can someone please help me out!!

Can someone explain this sort algorithm to me and how i can make a more efficient version, justifying the changes.

for (int p = 0; p<n-1; p++)
for (int counter = 0;counter < n-1; counter++)
if (a[counter]> a[counter+1])
swap(a, counter, counter+1);

Apreciate all help.

Reply With Quote
  #2  
Old April 22nd, 2005, 01:29 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
What happens if you set counter to equal p instead of zero?

Reply With Quote
  #3  
Old August 10th, 2005, 02:51 AM
NPH NPH is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 2 NPH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 10 sec
Reputation Power: 0
This code implements the bubble sort algorithm.

Code:
//do n-1 passes
for (int p = 0; p<n-1; p++)
{
	//each pass consists of going left to right in the array and swapping 
	//adjacent items that are out of order
	for (int counter = 0;counter < n-1; counter++)
	{
		//if out of order swap
		if (a[counter]> a[counter+1])
			swap(a, counter, counter+1);
	}
}


Suppose we have the following list: 1, 5, 2, 0. Bubble sort does the following:

Pass 1:
1, 5, 2, 0 - compare the 1 & 5. Out of order? No.
1, 5, 2, 0 - compare the 5 & 2. Out of order? Yes. Swap!
1, 2, 5, 0 - compare the 5 & 0. Out of order? Yes. Swap!
1, 2, 0, 5

Pass 2:
1, 2, 0, 5 - compare the 1 & 2. Out of order? No.
1, 2, 0, 5 - compare the 2 & 0. Out of order? Yes. Swap!
1, 0, 2, 5 - compare the 2 & 5. Out of order? No.

Pass 3:
1, 0, 2, 5 - compare the 1 & 0. Out of order? Yes. Swap!
0, 1, 2, 5 - compare the 1 & 2. Out of order? No.
0, 1, 2, 5 - compare the 2 & 5. Out of order? No.

Bubble sort does one more unnecessary pass and then stops.
For more help, www.NeedProgrammingHelp.com

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Can someone please help me out!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT