0

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

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);
            }
        }
    }

}

Edited by Reverend Jim: Fixed formatting

4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by ddanbe
0

that is one hideous looking bubble sort.

Search the forums for a simpler solution which uses temporary variables.

0

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):

for (j=0; j<i; j++) {
    if (h[j] > h[j+1]) {

4. Think about swap call arguments: pass pointers, not values...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.