Related questions
Concept explainers
java
Given Array :
ARRAY[] = [11, 21, 33, 40, 50]
Write a full and complete recursive implementation of the Binary Search
Algorithm such that you find your target element 21 in O(log2(n)) time
complexity overall and O(1) space complexity. Here, n is the length of
the list of input integers (array). We will not be considering any memory
used by recursion.
Task :- Write a Java code to implement binary search.
Java code :-
import java.io.*;
class Main {
public static int binarySearch(int[] arr, int low, int high, int target) {
if(low > high)
return -1;
int mid = (low+high)/2;
if(arr[mid]==target)
return mid;
if(arr[mid] > target)
// search in left half
return binarySearch(arr,low,mid-1,target);
else
// search in right half
return binarySearch(arr,mid+1,high,target);
}
public static void main(String[] args) {
int[] arr = {11,21,33,40,50};
int index = binarySearch(arr,0,arr.length-1,21);
if(index==-1)
System.out.println("target is not found in given array.");
else
System.out.println("target is found at index " + index);
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images
- Write a java class named First_Last_Recursive_Merge_Sort that implements the recursive algorithm for Merge Sort. You can use the structure below for your implementation. public class First_Last_Recursive_Merge_Sort { //This can be used to test your implementation. public static void main(String[] args) { final String[] items = {"Zeke", "Bob", "Ali", "John", "Jody", "Jamie", "Bill", "Rob", "Zeke", "Clayton"}; display(items, items.length - 1); mergeSort(items, 0, items.length - 1); display(items, items.length - 1); } private static <T extends Comparable<? super T>> void mergeSort(T[] a, int first, int last) { //<Your implementation of the recursive algorithm for Merge Sort should go here> } // end mergeSort private static <T extends Comparable<? super T>> void merge(T[] a, T[] tempArray, int first, int mid, int last) { //<Your implementation of the merge algorithm should go here> } // end merge //Just a quick method to display the whole array. public...arrow_forwardconvert the recursion code to alterative code in java public int array11(int[] nums, int index) { if (index == nums.length) return 0; if (nums[index] == 11) return 1 + array11(nums, index + 1); return array11(nums, index + 1); }arrow_forwardJava programming homework please helparrow_forward
- Write a program to implement insertion sort and also test this using an unsorted array in javaarrow_forwardJava Quick Sort but make it read the data 10, 7, 8, 9, 1, 5 from a file not an array // Java implementation of QuickSort import java.io.*; class GFG { // A utility function to swap two elements static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } /* This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ static int partition(int[] arr, int low, int high) { // pivot int pivot = arr[high]; // Index of smaller element and // indicates the right position // of pivot found so far int i = (low - 1); for (int j = low; j <= high - 1; j++) { // If current element is smaller // than the pivot if...arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education