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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started