I wrote a code that finds the median value of an unsorted array. What is this code's big O? Can you explain? Can we optimize runtime complexity?
public static int medianElement(int[] array,int low, int high) {
int[] tmpArray = new int[high - low + 1];
for (int i = 0; i < high - low; i++) {
tmpArray[i] = array[low + i];
}
boolean changed = true;
while (changed) {
changed = false;
for (int i = 0; i < high - low; i++) {
if (tmpArray[i] > tmpArray[i + 1]) {
changed = true;
swap(tmpArray, i, i + 1);
}
}
}
return tmpArray[(high - low + 1) / 2];
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}