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


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)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
