bubble sort

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 3
Reputation: paynegm is an unknown quantity at this point 
Solved Threads: 0
paynegm paynegm is offline Offline
Newbie Poster

bubble sort

 
0
  #1
Nov 5th, 2008
I have the code below and I keep getting an error saying that swap isnt a function. Just wondering if anyone had any idea why. Thanks


[code]
void swap(double *x, double *y ){
double temp;
temp = *x;
*x = *y;
*y = temp;

void sort(double h[], int size){
int i, j;
for (i=(size-1); i>=0; i--){
for(j=1; j<=i; j++){
if (h[j-1] > h[j]){
swap(*(h+(j-1)),*(h+j);
}
}
}

}


[code=C]
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: bubble sort

 
0
  #2
Nov 5th, 2008
that is one hideous looking bubble sort.

Search the forums for a simpler solution which uses temporary variables.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: bubble sort

 
0
  #3
Nov 5th, 2008
1. Where is the closed brace terminated swap function body?
2. Use code tag properly:
[code=c]
... snippet ...
[/code]
3. Keep it simpler (avoid index minus offset where possible):
  1. for (j=0; j<i; j++) {
  2. if (h[j] > h[j+1]) {
4. Think about swap call arguments: pass pointers, not values...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,976
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: bubble sort

 
0
  #4
Nov 5th, 2008
Your swap function has a missing } at the end.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
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