-1

my code's not working. The idea was to make a function which would bubble swap a generic array but i don't know why it's not working if i could get some help it would be appreciated.

int compareInt(int *a, int *b){
    return (*a)-(*b);
}
void swap(void *a, void *b){
    void *aux;
    aux=a;
    a=b;
    b=aux;
}

void bubbleSort(void *v1, size_t dim, size_t bytes, int (*cmp)(void *, void *)){
    int i, j;
    for(i=0; i<dim1; i++)
        for(j=0;j<dim1-1-i; j++){
            if(cmp(v1+j*bytes, v1+(j+1)*bytes)>0)
                swap(v1+j*bytes, v1+(j+1)*bytes);
        }
}
void printVec(int *v1, int dim){
    int i;
    for(i=0; i<dim; i++)
        printf("%d  ",v1[i]);
    printf("\n");
}
int main (void){
    int v1[]={7,3,5,1,9,2};
    printVec(v1, 6);
    bubbleSort(v1, 6, sizeof(int), compareInt);
    printVec(v1,6);
    return 0;

}
  • Where is `dim1` in `bubbleSort`? Do you mean `dim`? (i.e.) As is, this won't compile – Craig Estey Nov 04 '18 at 20:18
  • See also [SO 5055-9106](https://stackoverflow.com/q/50559106) and [SO 4987-1989](https://stackoverflow.com/q/49871989) for two other questions with generic swap implementations. Note that you need to pass the size to the swap function in order to do the swap successfully. – Jonathan Leffler Nov 04 '18 at 20:28
  • variable (and parameter) names should indicate `content` or `usage` (or better, both) variable names like `v1` `a` `b` `dim`, etc are meaningless, even in the current context – user3629249 Nov 04 '18 at 21:13
  • what header files are your code actually including>? Do you expect us to gesss? – user3629249 Nov 04 '18 at 21:18
  • Note that the `compareInt()` function runs into undefined behaviour because of (signed) integer overflow if you compare a large enough positive number with a large enough negative number, which can happen in general. You'd probably do better with `return ((*a) > (*b)) - ((*a) < (*b));` or equivalent. This avoids overflow (and hence incorrect answers) at the cost of two comparisons instead of one subtraction. YMMV. – Jonathan Leffler Nov 04 '18 at 21:51

1 Answers1

1

You problem is in your swap function (besides some typos), as you only swap the pointers you pass as parameters and therefore leave the array unchanged.

void swap(void *a, void *b){
    int aux;
    aux=*((int*)a);
    *((int*)a)=*((int*)b);
    *((int*)b)=aux;
}

should do the trick for int. If you want a generic swap function a size parameter should be passed.

Also note that the pointer arithmetic for void * is only a compiler extension and is not in the standard. v1+j*bytes should be (char*)v1+j*bytes.

Osiris
  • 2,783
  • 9
  • 17
  • `swap` should probably be passed a [byte] `size` argument because OP wants the sort to be generic (i.e. `int` as shown, but (e.g.) `double` in the future) – Craig Estey Nov 04 '18 at 20:21
  • @CraigEstey Yes you are right, either it should be declared as `swapInt`, same as the compare function or the better approach to pass a size argument. – Osiris Nov 04 '18 at 20:23