0% found this document useful (0 votes)
46 views

Linear Search&binary Search

The document discusses two algorithms for searching an array: linear search and binary search. Linear search sequentially checks each element of the array to see if it matches the target value. Binary search divides the array in half at each step, eliminating half of the remaining elements based on whether the target is lower or higher than the middle element. It provides example code and sample outputs demonstrating each search approach finding and not finding values in test arrays.

Uploaded by

Anik Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Linear Search&binary Search

The document discusses two algorithms for searching an array: linear search and binary search. Linear search sequentially checks each element of the array to see if it matches the target value. Binary search divides the array in half at each step, eliminating half of the remaining elements based on whether the target is lower or higher than the middle element. It provides example code and sample outputs demonstrating each search approach finding and not finding values in test arrays.

Uploaded by

Anik Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

linear search

import java.util.Scanner;
class LinearSearchExample
{
public static void main(String args[])
{
int counter, num, item, array[];
//To capture user input
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();
//Creating array to store the all the numbers
array = new int[num];
System.out.println("Enter " + num + " integers");
//Loop to store each number in array
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();
System.out.println("Enter the search value:");
item = input.nextInt();
for (counter = 0; counter < num; counter++)
{
if (array[counter] == item)
{
System.out.println(item+" is present at location "+(counter+1));
/*Item is found so to stop the search and to come out of the
loop use break statement*/
break;
}
}
if (counter == num)
System.out.println(item + " doesn't exist in array.");
}
}
Output 1:
Enter number of elements:
6
Enter 6 integers
22
33
45
1
3
99
Enter the search value:
45
45 is present at location 3

Output 2:
Enter number of elements:
4
Enter 4 integers
11
22
4
5
Enter the search value:
99
99 doesn't exist in array.
binary search
import java.util.Scanner;
class BinarySearchExample
{
public static void main(String args[])
{
int counter, num, item, array[], first, last, middle;
//To capture user input
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();

//Creating array to store the all the numbers


array = new int[num];

System.out.println("Enter " + num + " integers");


//Loop to store each number in array
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();

System.out.println("Enter the search value:");


item = input.nextInt();
first = 0;
last = num - 1;
middle = (first + last)/2;

while( first <= last )


{
if ( array[middle] < item )
first = middle + 1;
else if ( array[middle] == item )
{
System.out.println(item + " found at location " + (middle + 1) + ".");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println(item + " is not found.\n");
}
}

Output 1:
Enter number of elements:
7
Enter 7 integers
4
5
66
77
8
99
0
Enter the search value:
77
77 found at location 4.

Output 2:
Enter number of elements:
5
Enter 5 integers
12
3
77
890
23
Enter the search value:
99
99 is not found.

You might also like