void bubblesort(int *A, int n) {
int i, aux;
for(i=0; i<n-1; i++){
if(A[i]>A[i+1]){
aux = A[i];
A[i] = A[i+1];
A[i+1] = aux;
}
}
if(n-1>1)
bubblesort(A, n-1);
}
void bubblesortI(int *A, int n) {
int i, aux, j;
int troca=0; //1
for(i=0; i<n-1; i++){ //Pìor C. = 2n Melhor C. = 1;
for(j=0;j<n-i-1; j++){ //Pior C. = n²+n-2 Melhor C. = 2n
if(A[j]>A[j+1]){ //Pior C. = (n²+n)/2 Melhor C. = n-1
aux = A[j]; //Pior C. = (n²+n)/2 Melhor C. = 0
A[j] = A[j+1]; //Pior C. = (n²+n)/2 Melhor C. = 0
A[j+1] = aux; //Pior C. = (n²+n)/2 Melhor C. = 0
troca =1; //Pior C. = (n²+n)/2 Melhor C. = 0
}
}
if(!troca) //Pior C. = n-1 Melhor C. = 1;
break;
}
//Pior caso: O(n²) Melhor caso: O(n)
}
I am studying ordering and complexity and I ended up doing the recursive and iterative bubblesort code and wanted to define the order of them, but I can not figure out how to calculate the complexity of recursive codes.
In front of the iterative code is the cost of each line.
PS: If someone has a costing and complexity calculation tool for recursive algorithms, I'll be happy.