Question: I have a java program and I want you to fix the errors because it has some errors and I will show you the questions

I have a java program and I want you to fix the errors because it has some errors and I will show you the questions so you know what to do

 I have a java program and I want you to fixthe errors because it has some errors and I will show you

This is my program:

import java.util.Random;

public class Performance {

/*Function to sort array using insertion sort*/

public static void insertionSort(int arr[])

{

int n = arr.length;

for (int i=1; i

{

int key = arr[i];

int j = i-1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (j>=0 && arr[j] > key)

{

arr[j+1] = arr[j];

j = j-1;

}

arr[j+1] = key;

}

}

public static boolean searchUseLoop(int[] arr, int targetvalue) {

for(int i=0; i

if(arr[i] == targetvalue)

return true;

}

return false;

}

public static void main(String[] args) {

int SIZE = 10000;

int arr[] = new int[SIZE];

Random rand = new Random();

// generating random numbers

for(int i=0; i

arr[i] = rand.nextInt(SIZE)+1; //1-SIZE

}

// generatign a random number in range 1-SIZE to search

int key = rand.nextInt(SIZE)+1;

long start , end;

start = System.currentTimeMillis();

searchUseLoop(arr, key); // searching

end = System.currentTimeMillis();

System.out.println("Time taken to search: "+(end - start));

// creating a copy to sort using insertion

int []copy = new int[SIZE];

for(int i=0; i

copy[i] = arr[i];

start = System.currentTimeMillis();

insertionSort(copy);// sorting using insertion sort

end = System.currentTimeMillis();

System.out.println("Time taken to sort using insertion sort: "+(end - start));

start = System.currentTimeMillis();

Arrays.sort(arr);

end = System.currentTimeMillis();

System.out.println("Time taken to sort using Arrays.sort: "+(end - start));

start = System.currentTimeMillis();

Arrays.binarySearch(arr, key);

end = System.currentTimeMillis();

System.out.println("Time taken to search using Arrays.binarySearch: "+(end - start));

}

}

1) Write a program to do the following a. Define an array of size (Constant value or passing a value to main method) b. Fill the array with random numbers between 1-SIZE c. Find how long it takes to search a number from the array d. Find how long it takes to sort using Insertion e. Find how long it takes to sort using Arrays.sort f. Find out how long it takes to search using Arrays.BinarySearch g. Try with array size of (100, 10000, 100000) Insertion Sort public static int[ insertionSort(int] data) // run from second element to last for (int i - l; ?

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!