public static void sort(int[] A, int i, int j) {
if (i >= j) return;
int m = (i + j) / 2;
sort(A, i, m);
sort(A, m + 1, j);
if (A[m] > A[j]) {
// swap A[j] and A[m]
int temp = A[j];
A[j] = A[m];
A[m] = temp;
}
sort(A, i, j - 1);
}
I tried the master theorem to find out the complexity of this function, but I guess it can't be used in this case.
The algorithm seems to split the array in half, then it compares the element at index m with the element at index j and swaps them. However, the recursive call sort(A, i, j - 1) is confusing me.
Can $T(n)=T(n/2)+T(n/2)+T(n-1)+O(1)$ be a possible solution?