
Concept explainers
- Create a program named MergeSort.java then copy the following code to your program:
- Write a main method that accepts three numbers N, A, B (assume A<B) from the command argument list, then use the number N to create an N-element int array.
public static void mergeSort(int[] arr){
mergeSortRec(arr, 0, arr.length-1);
}
private static void mergeSortRec(int[] arr, int first, int last){
if(first<last){
int mid=(first+last)/2;
mergeSortRec(arr, first, mid);
mergeSortRec(arr, mid+1,last);
merge(arr, first, mid, mid+1, last);
}
}
private static void merge(int[] arr, int leftFirst,
int leftLast, int rightFirst, int rightLast){
int[] aux=new int[arr.length];
//extra space, this is downside of this
int index=leftFirst;
int saveFirst=leftFirst;
while(leftFirst<=leftLast && rightFirst <=rightLast){
if(arr[leftFirst]<=arr[rightFirst]){
aux[index++]=arr[leftFirst++];
}else{
aux[index++]=arr[rightFirst++];
}
}
while(leftFirst<=leftLast){
aux[index++]=arr[leftFirst++];
}
while(rightFirst<=rightLast)
aux[index++]=arr[rightFirst++];
for(index=saveFirst; index<=rightLast; index++)
arr[index]=aux[index];
}
- Assign random numbers between [A, B] to each of the N elements of the array.
- Call mergeSort method to sort the array.
- Write instructions to record the time spent on sorting.
- (Optional) you may write code to verify the array was sorted successfully.
- Once completed, test your program with different numbers N, A, B and screenshot your result. Your result should include the number of elements and sorting time.
- Please include the following in the Word document you created for the assignment for the final submission:
- Copy/paste your completed code in MergeSort.java
- Insert at least one screenshot of the running output in #7 above
- Answer this question: What is the average Big O for this sort?

Step by stepSolved in 3 steps with 3 images

- in java programming answer the following: Given an array called myArr (created as ArrayList<T>) and contains the {1,2,3,4,5,6,7}. (a) what would be the contents of this array after myArr.add(10). b) write the content of myArr after myArr.add(4, 11);arrow_forwardplease read the directions carefullyarrow_forward. Given the following parallel arrays: int[] arrProdNumber = new int[] { 100, 115, 125, 142, 163, 199, 205, 388 }; string[] arrProdDesc = new string[] { "Kitten", "Cat", "Bird", "Chicken", "Dog", "Puppy", "Aardvark", "Zebra" }; Write a C# program that includes these arrays. Your program should then read a product number from the user and search the product number array for that number. If it is found, then your program should report the product number they entered and the corresponding product description. If the product number they enter is not in the first array, then your program should indicate that item number was not found. Here’s a sample run: //first runEnter product number: 115Item #115's product description is Cat//second runEnter product number: 1963Item #1963 does not exist in your inventory Some Random Number Generation HintsRandom rndNumber = new Random();Console.WriteLine(rndNumber.Next()); //random integerConsole.WriteLine(rndNumber.Next(101)); //random integer…arrow_forward
- In Java Assume code that has imported Scanner and instantiated it for keyboard input as stdln. Wtire a code fragment that does the following: 1. Uses a for loop to fill each array element with the square root of that elements index valuearrow_forwardWrite a loop that counts how many elements in an array are equal to zero. arrays.cpp 1 #include // sizet 2 int countZeros (const int values[], size_t size) { int count = 0; 3 4 for (int i; i using namespace std; 3 2 4 int countZeros(const int values[], size_t size); 5 int main() { int a[] = {1, 2, 0, 3}; cout <« countZeros (a, 4) <« endl; cout « "Expected: 1" « endl; 6 7 8 9 10 11 int b[] = {0, 2, 0, 3}; cout <« countZeros (b, 4) <« endl; cout « "Expected: 2" « endl; 12 13 14 15 int cl] -{1, 0, θ, 0, 0 ; cout <« countZeros (c, 5) <« endl; cout « "Expected: 4" « endl; 16 17 18 19 } CodeCheck Reset Testers Running Tester.cpp pass fail fail 1 Expected: 1 Expected: 2 Expected: 4 Score 1/3arrow_forwardIn Java list all the values of the array after the function is called A. static void Fun(int[][]list){ int temp=list[1][2]; list[1][2]=list[0][2]; list[0][2]=temp; } public static void main(String[] args) { int [][]arr={{10,20,30},{100,200,300}}; Fun(arr); } 10,20,30,300 B. static void Fun(int[]list) { list[3]=5;} public static void main(String[] args) { int []arr={5,6,7,9}; Fun(arr); } C. static void Fun(int[][]list){ list[1][0]=0;} public static void main(String[] args) { int [][]arr={{-2,-3,-4},{5,6,7,9}}; Fun(arr); }arrow_forward
- Method Details: public static java.lang.String getArrayString(int[] array, char separator) Return a string where each array entry (except the last one) is followed by the specified separator. An empty string will be return if the array has no elements. Parameters: array - separator - Returns: stringThrows:java.lang.IllegalArgumentException - When a null array parameter is providedarrow_forwardCreate a program named MergeSort.java then copy the following code to your program: mergeSort.txt Write a main method that accepts three numbers N, A, B (assume A Assign random numbers between [A, B] to each of the N elements of the array. Call mergeSort method to sort the array. Write instructions to record the time spent on sorting. (Optional) you may write code to verify the array was sorted successfully. Once completed, test your program with different numbers N, A, B and screenshot your result. Your result should include the number of elements and sorting time. mergeSort.txt public static void mergeSort(int[] arr){ mergeSortRec(arr, 0, arr.length-1);}private static void mergeSortRec(int[] arr, int first, int last){ if(first int mid=(first+last)/2;mergeSortRec(arr, first, mid); mergeSortRec(arr, mid+1,last); merge(arr, first, mid, mid+1, last); }}private static void merge(int[] arr, int leftFirst, int leftLast, int rightFirst, int rightLast){ int[] aux=new…arrow_forwardParagraph Styles Editing 5. Given the following series of n integers:1+2+3++n-2+n-1+nWrite a Java method that returns the sum of the series. 6. Given the following function:f(x)-2xFor, determine 7. Consider an array that stores the following sequence of integers:10, 15, 20, 25, 30Write a Java method that returns the sum of the integers stored in the array. The array must be passed to the method as an argument. I OFocus Earrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





