Answered step by step
Verified Expert Solution
Question
1 Approved Answer
HELP!!! 2 other answers on Chegg wrong e learned in this lesson that Merge Sorts are recursive. One of the favorite topics that College Board
HELP!!! other answers on Chegg wrong
e learned in this lesson that Merge Sorts are recursive. One of the favorite topics that College Board likes to ask is how many times a recursive method is called. With that in mind, lets figure out how many times our recursive method is called for a given merge sort.
For this exercise, you are given the mergeSort and the makeRandomArray helper methods. Using the static count variable, add an incrementer in the mergeSort method to count how many times it is called.
Then, in the main method, create a random array of sizes k and k Run the array through the sort and print out the results of the counter. Dont forget to reset the counter between runs!
You should pay attention to the pattern that you see. Does this pattern surprise you?
Sample Output
Total Recursive calls for : Results Hidden
Total Recursive calls for : Results Hidden
Total Recursive calls for : Results Hidden
Total Recursive calls for : Results Hidden
Challenge: See if you can write this with a loop!
import java.util.ArrayList;
public class Main
private static int numCalls;
public static void mainString args
public static void mergeSortint current, int length
if length
return;
int mid length ;
int left new intmid;
int right new intlength mid;
for int i ; i mid; i
lefti currenti;
for int i mid; i length; i
righti mid currenti;
mergeSortleft mid;
mergeSortright length mid;
mergecurrent left, right;
public static void mergeint current, int left, int right
int leftSize left.length;
int rightSize right.length;
int i j k ;
while i leftSize && j rightSize
if lefti rightj
currentk lefti;
else
currentk rightj;
while i leftSize
currentk lefti;
while j rightSize
currentk rightj;
public static int makeRandomArrayint number
int array new intnumber;
ArrayList sorted new ArrayListnumber;
Create the sorted list
for int i ; i number; i
sorted.addi;
Now shuffle it
int index ;
while sortedsize
int randomIndex intMathrandomsortedsize;
arrayindex sorted.removerandomIndex;
index ;
return array;
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