Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
44 views3 pages

Megwish C

Sequential search is a linear search that visits each element of an array one by one and compares it to the desired value. If a match is found, the search is complete. If no match is found for the last element, the value is not in the array. Binary search is faster but can only be used on a sorted array. It locates the middle element, compares it to the search value, and then searches either the first or second half of the array recursively until the value is found or not. Both algorithms are demonstrated with programs to search an initialized array of integers for a user-input value.

Uploaded by

Almas Zahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

Megwish C

Sequential search is a linear search that visits each element of an array one by one and compares it to the desired value. If a match is found, the search is complete. If no match is found for the last element, the value is not in the array. Binary search is faster but can only be used on a sorted array. It locates the middle element, compares it to the search value, and then searches either the first or second half of the array recursively until the value is found or not. Both algorithms are demonstrated with programs to search an initialized array of integers for a user-input value.

Uploaded by

Almas Zahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sequential Search:

Sequential search is also called linear search array desired value. It


follow the following search steps. It is simple way to search step.
Visit the first element of the value.
Compare its value with required value.
If the value of the array match with desired value search
to next element the search complete.

Program:
Write a program that initialize an array. It input a value from user and
search the number in the array.
#include <iostream.h>

#include<conio.h>

Void main()

int arr[10]={10,20,30,40,50,60,70,80,90,100};

int i ,n ,loc=-1;

clrscr();

cout<<" Enter value to find ";

cin>>n;

if(i=o;i<10;i++)

if(arr[i]==n)

loc=i;

if(loc==-1)
cout<<"value not found in the array";

else

cout <<" value fiund at index" <<loc;

getch();

Binary Search:
Binary search is quicker method of searching for values in the array.
Binary search is quicker method but it can only search sorted array. It
cannot apply on unsorted array. It locate the middle element of the array
compare with search element. If they are equal, search is successful and
index of middle element returned. If they are not equal, it reduced the
search to half of the element. If the search number is less then middle
element, it search the first half of the array.

Program: Write a
program that initialize an array of ten integers .It inputs integers from the
user and search the value in the array using the binary search.
#include <iostream.h>

#include <conio.h>

void main()

clrscr();

int array [10]={10,20,30,40,50,60,70,80,90,100};

int n, i, mid, start, end, loc;

loc=-1;

start=0;
end=9;

cout<<"Enter any number to find ";

cin>>n;

while(start<=end)

mid=(start+end)/2;

if(arr[mid]==n)

loc=mid;

break;

else if(n<arr[mid])

end=mid-1;

else

start=mid+1;

if (loc==-1)

cout<<n>>"not found"<<endl;

else

cout<<n<<"found at index"<<loc<<endl;

getch();

You might also like