Binary Search And Linear search
Binary Search
Binary search is one of the most efficient searching technique used in computer programming .basically there are two searching technique which are linear search and binary search.
linear search on the other hand is easy to understand as well as easy to implements .but binary search is easy to understand but hard to implements for student like me.2 year ago,i knew about binary search i understand the logic but don't make it program .i just understand like it is the same technique that we used in searching a word in the dictionary.as a result i failed to make a binary search program
but the problem that occur in making of binary search is that first of all i decided that i used the modularity concepts i.e, each and every logic should be kept inside the function/method.since you can reuse it .can the problem is that you can do binary search either by iteration or recursion .
iteration means solving problem with the helps of loops and recursion means solving problem with function/method.it is of two types direct recursion and indirect recursion .direct recursion means function calling itself whereas indirect recursion means function calling another function .
i finally make both programs
i.e,
Linear search and Binary search please note that i make linear search with iteration and binary search with recursion
the problem i faced in make binary search is that i make the binary search function void as a result it is not returning any value.
i make both programs in java and used IDE(Integrated Development Environment )
linear Search in java :
package linear_search; //
import java.util.Scanner;
public class linear_search {
static void linear(int b[],int temp)
{
int i;
int count=0;
for(i=0;i<b.length;i++)
{
if(temp==b[i])
{
count++;
}
}
if(count>=1)
{
System.out.print("Element is found at "+i+" location at "+count+" times");
}
else
System.out.print("element is not found");
}
public static void main(String args[])
{
int n,i,item;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the size");
n=sc.nextInt();
int a[]=new int[n];
System.out.print("Enter the elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter the element you want to search in array");
item=sc.nextInt();
linear(a,item);
}
}
Binary search in java:
package binary_search;
import java.util.Scanner;
public class binary_search {
static int fxn(int b[],int temp)
{
int beg=0;
int end=b.length-1;
int mid=0;
while(beg<=end)
{
mid=(beg+end)/2;
if(b[mid]==temp)
{
return mid;
}
else if(temp>=b[mid])
{ beg=mid+1;
}
else
end=mid-1;
//return 0;
}
return -1;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i,item;
System.out.print("Enter the size of array");
n=sc.nextInt();
int a[]=new int[n];
System.out.print("Enter the elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.print("Enter the item to search");
item=sc.nextInt();
int res=fxn(a,item);
System.out.print(res);
}
}
This comment has been removed by the author.
ReplyDelete