Hello, I have a problem with this code. I'm implementing the bubble sort function. But something curious happens when running this code. The variable n that indicates the amount of vector space in the main function is modified after executing the bubbleSort () function. And I'm not understanding the reason for this change since it does not pass the pointer from variable n to the sort function. Could someone please explain the reason to me?
The n always modifies to the highest 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;
}