This code helps you to understand the different Searching algorithms. The sorting algorithms depicted in this code are:
- Linear Search
- Binary Search
- Binary Search with recursion
The example code is in Java (version 1.8 or higher will work).
In computer science, a search algorithm is any algorithm which solves the Search problem, namely, to retrieve information stored within some data structure, or calculated in the search space of a problem domain. Examples of such structures include but are not limited to a Linked List, an Array data structure, or a Search tree. The appropriate search algorithm often depends on the data structure being searched, but also on any a priori knowledge about the data.
Search algorithms can be classified based on their mechanism of searching. Linear search algorithms check every record for the one associated with a target key in a linear fashion.[3][4] Binary, or half interval searches, repeatedly target the center of the search structure and divide the search space in half.
For more information, see
switch(ch)
{
case 1:
LinearSearch(a,n,num);
break;
case 2:
BinarySearch(a,n,num);
break;
case 3:
int first=0;
int last=n;
BinarySearchRec(a,n,num,first,last);
break;
}
You can select any algorithm from the list and then enter an array. Following that, enter a term that you want to search in the list.
To compile the code, simply run the javac Search.java
.
To run the code, type java Search
javac Search.java
java Search