First, pay attention to the indentation of your program, which is horrible. It helps others (and yourself) to understand it. If you did, you would see that a {
is missing after the first for
and the corresponding }
before the end of the function, and because of that, it does not do anything you wanted.
In addition, it is good practice in C to declare the variables only when they will be used to the smallest possible scope. C is not Pascal which requires variables to be declared at the beginning of the function.
Another detail is bits/stdc++.h
is a C ++ header, not C's. For C, use stdio.h
and stdbool.h
.
And there's also a return
left at the end of bubbleSortM
which is unnecessary, since that's the last thing of the function.
Your corrected program looks like this:
#include <stdio.h>
#include <stdbool.h>
void bubbleSortM(int A[], int n) {
for (int i = 1; i < n; i++) {
bool troca = 0;
for (int j = n - 1; j >= i; j--) {
if (A[j - 1] > A[j]) {
int aux = A[j - 1];
A[j - 1] = A[j];
A[j] = aux;
troca = 1;
}
}
if (!troca) {
return;
}
}
}
void printVetor(int A[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", A[i]);
}
}
int main() {
int A[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(A) / sizeof(A[0]);
bubbleSortM(A, n);
printf("Vetor ordenado: \n");
printVetor(A, n);
}
See here working on ideone.