I'm trying to find the order and growth function of this for loop inside a function which takes in an array of length n > 2.
This function orders the array in ascending order. I'm trying to find the order for a worst case scenario: when the array is ordered initially in descending order and the function therefore has to iterate through the array many times to sort it.
Here is the loop:
for (int next = 1; next < array.length; next++) {
int value = array[next];
int index = next;
while (index > 0 && value < array[index - 1]) {
array[index] = array[index - 1];
index--;
}
array[index] = value;
}
I've been racking my brains trying to figure it out. Writing tests, writing tons of functions down and I get close, but never right on. How would you go through such a loop to find it's order and growth function?
Any direction would greatly be appreciated. Thank you so much.