Java binary search program using recursion

Example package com.w3spoint;   public class Test { public static int binarySearch(int[] data,int start, int end,int key){ if (start < end) { int mid = start + (end – start) / 2; if (key < data[mid]) { return binarySearch(data, start, mid, key);   } else if (key > data[mid]) { return binarySearch(data, mid+1, end , … Read more

Java binary search program

Binary search Binary search is a search algorithm that finds the position of a target value within a sorted collection of data (we are taking array here). It is a fast search algorithm with run-time complexity of Ο(log n). It works on the principle of divide and conquer. Binary search compares the target value to … Read more

Java linear search program using recursion

Example package com.w3spoint;   public class Test { public static int recSearch(int data[], int l, int r, int key) { if (r < l) return -1; if (data[l] == key) return l; return recSearch(data, l+1, r, key); }   public static void main(String args[]){ int[] data= {50,27,30,50,70,9,19}; int key = 9;   int index = … Read more

Java linear search program

Linear search Linear search is a way of finding a target value within a collection of data. It is also known as sequential search. It sequentially checks each element of the collection data for the target value until a match is found or until all the elements have been searched. Basic algorithm Given a collection … Read more

java search algorithms examples

Search algorithm Search algorithm refers to a step-by-step procedure which is used to locate specific data among a collection of data. All search algorithms use a search key in order to proceed for the search operation. The efficiency of a search algorithm is measured by the number of times a comparison of the search key … Read more