can someone tell me what is the time complexity of this algorithm? keep in mind: the second method (findMax) - run on the array based on the index that it gets, means that the method (findMax) doesn't run on all the array every time. I think that the time complexity of this algorithm is O(n) but maybe I'm wrong.
public class Q2 {
public static int[] replace(int []a)
{
for(int i = 0; i < a.length; i++ ){
if(i == a.length-1){
a[i] = 0;
}
int maxSubArry = findMax(a,i);
swap (a, i, maxSubArry);
}
return a;
}
public static int findMax (int[]a, int i)
{
// i = i +1;
int tmp = 0;
for(i = i +1; i<a.length; i++)
{
if(a[i] > tmp)
tmp = a[i];
}
return tmp;
}
public static void swap(int[]a, int i, int maxSubArry)
{
int temp = a[i];
a[i] = maxSubArry;
a[i+1] = temp;
}
}