2
Hello, I have a problem with this code. I am implementing the function of Bubble Sort. But something curious happens when running this code. The variable n that indicates the amount of vector spaces in the main function is modified after executing the bubbleSort() function. And I don’t understand the reason for this modification since it doesn’t pass the pointer of the variable n to the sorting function. Could someone please explain to me why?
n always modifies to the higher value of the vector.
#include <stdio.h>
#include <stdlib.h>
int ASC(int x, int y) {
    return x < y;
}
int DSC(int x, int y) {
    return x > y;
}
void bubbleSort(int *list, int start, int size, int cmp(int, int)) {
    int cont, hold;
    for(; start < size-1; start++) {
        for(cont = 0; cont < size-start; cont++) {
            if(cmp(list[cont+1], list[cont])) {
                hold = list[cont];
                list[cont] = list[cont+1];
                list[cont+1] = hold;
            }
            printf("hold = %d\n", hold);
            printf("cont = %d\n\n", cont);
        }
    }
}
int main() {
    int vetor[] = {1,8,7,14,5};
    int n = sizeof(vetor)/sizeof(int);
    printf("n = %d\n", n);
    bubbleSort(vetor, 0, n, ASC);
    printf("n = %d\n", n);
    int i;
    for(i = 0; i < n; i++) {
        printf("%d ", vetor[i]);
    }
    return 0;
}
I did a test on my machine (Linux Fedora 24) and the value of
nnay is modified...– zentrunix