C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 12th, 2003, 03:32 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
bubble sort

I am getting into simple algorithms. I am starting with the bubble sort which was suggested by a friend. He ended up giving me the code to look at. Here is the code and the questions I have:

#include <iostream.h>

int main() {

int array[] = {22, 23, 21, 20};
int n = 4;
for ( int i = 0; i < n-1; i++ ) { <--- Why n-1?
for ( int j = 1; j < n-i; j++ ) { <--- n-i now?
if ( array[j-1] > array[j] ) {
int temp;
temp = array[j-1]; <----Why did he make a temp array?
array[j-1] = array[j];
array[j] = temp;
}

}
}

for (int x = 0; x < 4; x++) {
cout << array[x] << "\n";

}

cout << "\nThanks for using the bubble sort program!\n\n";
return 0;

}

I do understand that after one cycle the highest number is at the end, so the n-1 tells the program it doesn't need to check the last number...but I think im missing something. Thanks in advance guys.

-andy
__________________
hmmm...

Last edited by andy3109 : February 12th, 2003 at 03:36 PM.

Reply With Quote
  #2  
Old February 12th, 2003, 03:46 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 9 m 24 sec
Reputation Power: 4080
Code:
if ( array[j-1] > array[j] ) { 
int temp; 
temp = array[j-1]; <----Why did he make a temp array?
array[j-1] = array[j];
array[j] = temp;
}

He's not making a temp array. All he's doing is declaring a temp variable and using it to swap the values of two array elements. Let's say you have two variables a and b and want to swap the values. This is how you would do it:
Assign the value of 'a' to a temp variable, say 'c'
Assign the value of 'b' to 'a'
Assign the value of 'c' to 'b'

As for what the rest of it does, I'd suggest you study how the bubble sort algorithm works. Start with this article http://www.devarticles.com/art/1/76/2
Note that the code that the author presents in that article is a bubble sort, but is less optimal than your friend's code (because it performs longer loops than is actually necessary). However, the concept of the bubble sort is explained pretty decently there. Hope this helps!

Reply With Quote
  #3  
Old February 12th, 2003, 04:01 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
andy,

The temporary variable is required to swap values. You can't do this:

int a=5
int b=10;

a=b;
b=a;

Do you see why? If you set a equal to b, then a equals 10(the 5 is overwritten), then if you set b equal to a, b will equal 10, which was it's original value. Somehow you have to save the value for a or else you will lose it. That's where the temporary variable comes into play.

I admit those index values are not easy to follow, so a good programmer would explain them with comments. Otherwise, you need to read up on the algorithm used and go through the loop by hand recording values for all the variables to see what's happening. Generally speaking though, the outer loop(i loop) is going to be peformed 3 times(n-1=3) which means the code is going to traverse the array three times doing swaps, but each time through the array you don't have to perform as many swaps(n-i gets smaller).

Last edited by 7stud : February 12th, 2003 at 04:36 PM.

Reply With Quote
  #4  
Old February 12th, 2003, 04:49 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
That was a great link for explaining how the bubble sort works.

I gathered from the article that since the largest value is moved to the end of the array the first time through the array, subsequent examinations don't need to look at the last element, and the next time through the array, the second largest number is second from the end, so the last two elements don't need to be examined subsequently, and so forth, hence the (n-i) in the inner loop of the code andy posted.

Last edited by 7stud : February 12th, 2003 at 04:52 PM.

Reply With Quote
  #5  
Old February 12th, 2003, 07:07 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 9 m 24 sec
Reputation Power: 4080
Quote:
Originally posted by 7stud
I gathered from the article that since the largest value is moved to the end of the array the first time through the array, subsequent examinations don't need to look at the last element, and the next time through the array, the second largest number is second from the end, so the last two elements don't need to be examined subsequently, and so forth, hence the (n-i) in the inner loop of the code andy posted.


Yep, that's exactly the optimization I was talking about.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > bubble sort

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap