Question: Hello, what I'm trying to do is call the insertionsort into the segment3 method. import java.util.Random; public class NanoSeconds { public static void main (String[]
Hello, what I'm trying to do is call the insertionsort into the segment3 method. import java.util.Random; public class NanoSeconds { public static void main (String[] args) { System.out.println(".................................................................................."); System.out.println("n\t\t|\t100\t|\t10000\t|\t1000000\t|\t100000000"); System.out.println("..................................................................................");
System.out.print("Segment 3"); Segment3(100); Segment3(10000); Segment3(1000000); Segment3(100000000); System.out.println(" "+".................................................................................."); } public static void Segment3(int n) { long startTime, stopTime, elaspedTime; startTime = System.nanoTime(); int[] arr = new int[n]; Random random = new Random(); for(int i = 0; i < n; i++){ arr[i] = random.nextInt(n); } new NanoSeconds().bSearch(arr, random.nextInt(n)); stopTime = System.nanoTime(); elaspedTime = stopTime - startTime; System.out.print("\t|\t" + elaspedTime); }
static void insertionsort(int[] a) { int len = a.length;
for (int i = 1; i < len; i++) { int key = a[i]; int j = i - 1; while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; j--; } a[j + 1] = key; } }
int bSearch(int[] arr, int key) { int lo = 0, mid , hi = arr.length - 1; while(lo <= hi) { mid = (lo + hi)/2; if(key < arr[mid]) hi = mid - 1; else if(arr[mid] < key) lo = mid + 1; else return mid; } return - 1; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
