Question: this is my java code, how to output linear Search,binarySearch, interpolationSearch, jump Search for random number ? import java.util.Random; import java.util.*; import java.lang.Math; import java.util.ArrayList;

this is my java code, how to output linear Search,binarySearch, interpolationSearch, jump Search for random number ?

import java.util.Random; import java.util.*; import java.lang.Math; import java.util.ArrayList; import java.util.Arrays; public class Assgnment1 {

public static int[] random_list(int max_int, int min, int size, boolean do_sort) { int[] arr = new int[size]; Random rand = new Random(); for(int i=0; i= l) { int mid = l + (r - l) / 2; // If the element is present at the middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present in right subarray return binarySearch(arr, mid + 1, r, x); } return -1; } public static int interpolationSearch(int arr[], int lo, int hi, int x) { int pos; if (lo <= hi && x >= arr[lo] && x <= arr[hi]) { // Probing the position with keeping uniform distribution in mind. pos = lo + (((hi - lo) / (arr[hi] - arr[lo]))* (x - arr[lo])); // Condition of target found if (arr[pos] == x) return pos; // If x is larger, x is in right sub array if (arr[pos] < x) return interpolationSearch(arr, pos + 1, hi, x); // If x is smaller, x is in left sub array if (arr[pos] > x) return interpolationSearch(arr, lo, pos - 1,x); } return -1; } public static int search(int arr[], int x) { int n = arr.length; for (int i = 0; i < n; i++) { if (arr[i] == x) return i; } return -1; } public static int jumpSearch(int[] arr, int x) { int n = arr.length; // Finding block size to be jumped int step = (int)Math.floor(Math.sqrt(n)); // Finding the block where element is // present (if it is present) int prev = 0; while (arr[Math.min(step, n)-1] < x) { prev = step; step += (int)Math.floor(Math.sqrt(n)); if (prev >= n) return -1; } // Doing a linear search for x in block // beginning with prev. while (arr[prev] < x) { prev++; // If we reached next block or end of // array, element is not present. if (prev == Math.min(step, n)) return -1; } // If element is found if (arr[prev] == x) return prev; return -1; } public static void main(String[] args) { Random rand = new Random(); int key; int max_int = 1000000; int size = 10000; int min=5; int idx=0; int[]arr = random_list(max_int, min, size, true); for(int i=0; i

}

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!