Question: CS Java Question. Algorithm: sortIt() Minimum Statements. How many statements would be executed in a call to sortIt() when the array size is zero (n

CS Java Question. Algorithm: sortIt()

Minimum Statements. How many statements would be executed in a call to sortIt() when the array size is zero (n == 0) or one (n == 1)?

Best Case Scenario. Under what conditions would the minimum number of statements be executed for an array where n is large? Would the algorithm execute a different number of statements if the elements in the array were already in sorted order? Reverse order? Random order? All the same? What is the growth function under the best case conditions?

Worst Case Scenario. Under what conditions would the maximum number of statements be executed for an array where n is large? (Already in some kind of sorted order? Duplicates?) What is the growth function under the worst-case conditions?

What is the runtime order (big-O) of sortIt() based on the above growth functions?

Code:

* Take an int[] and reorganize it so they are in ascending order.

* @param array ints that need to be ordered

*/

public static void sortIt(int[] array) {

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;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!