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

DS Program1

E

Uploaded by

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

DS Program1

E

Uploaded by

pandeyyash041
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Program-01.

Q :- WAP To Perform Linear & Binary Search On An Array ?

(i) Linear Search On Array

1. It’s a sequential search which is done over all items .


2. Every item is checked and if a match is found then that particular
item is returned, otherwise the search continues till the end of the
data collection .

<Psuedo Code For Linear Search>

procedure linear_search (list, value)


for each item in the list
if (match item == value)
return the item's location
end if
end for
end procedure

(ii) Binary Search On Array


1. This search works on the principle of divide and conquer.
2. For this, the data collection should be in the sorted form.
3. Binary search looks for a particular item by comparing the middle
most item of the collection.
4. If a match occurs, then the index of item is returned. If the middle
item is greater than the item, then the item is searched in the sub-
array to the left of the middle item.
5. Otherwise, the item is searched for in the sub-array to the right of
the middle item.
6. This process continues on the sub-array as well until the size of the
sub array reduces to zero.
<Psuedo Code For Binary Search>
BinarySearch(A[0,1,..(N-1)] , value) {
low = 0 , high = N - 1
while (low <= high){
// invariants
value > A[i] for all i < low ;
value < A[i] for all i > high ;
mid = (low + high) / 2;
if (A[mid] > value) { high = mid – 1 ; }
else if (A[mid] < value){ low = mid + 1 }
else { return mid; }
return not_found;
}

Code For Linear And Binary Search : -


#include<iostream>
using namespace std;

void PrintArray(int arr[] , int size); //Function Prototype……


void LinearSearch(int arr[] , int size , int Key){
for(int i=0 ; i < size ; i++){
if(arr[i] == Key){
cout<<"BY Linear Search ::: KEY ";
cout<<Key<<" FOUND AT INDEX "<<i<<" IN THE ARRAY ";
PrintArray(arr , size);
return;
}
}
cout<<"BY Linear Search ::: KEY ";
cout<<Key<<" NOT FOUND IN THE ARRAY ";
PrintArray(arr , size);
}
void BinarySearch(int arr[] , int size , int Key){
int start=0 , end=size-1;
while(start <= end){
int mid=(start + end)/2;
if(arr[mid] == Key){
cout<<"BY Binary Search ::: KEY ";
cout<<Key<<"FOUND AT INDEX"<<mid<<"IN ARRAY";
PrintArray(arr , size);
return;
}
if(arr[mid] > Key){ end = mid-1; }
else { start = mid+1; }
}
cout<<"BY Binary Search ::: KEY";
cout<<Key<<"NOT FOUND IN THE ARRAY ";
PrintArray(arr , size);
}
void PrintArray(int arr[],int size){
cout<<"{ ";
for(int i=0 ; i<size ; i++){
if (i == (size-1) ) { cout<<arr[i]<<" }"<<endl ; }
else { cout<<arr[i]<<", " ; }
}
}

int main() {
int key;
int arr[7]={1,2,3,4,5,6,7};
cout<<"ENTER THE KEY YOU WANT TO SEARCH IN THE ARRAY ::: ";
cin>>key;
LinearSearch(arr,7,key);
BinarySearch(arr,7,key);
return 0;
}

OUTPUT
Viva-Questions :-
Q :- The sequential search, also known as ?
Sol :- The sequential search, also known as Linear Search.

Q :- What is the Primary Req. for the implementation of


binary search in an array?
Sol :- The primary requirement for implementing Binary
search in an array is that the array must be sorted. The Binary
search Algorithm is based on the Divide & Conquer
Technique, which means that it will divide the array In Sub-
Array.

Q :- Is Binary Search Applicable on Array and


LinkedList ?
Sol :- Yes , Binary Search Can Be Applied On Array And Linked
List If They Are Sorted.

Q :- What is the Principle of working of Binary search?


Sol :- Binary Search Is Based On The Principle Of Divide And
Conquer In Which We Divide The Array In Sub-Array.

Q :- Which Searching Algorithm is Efficient one?


Sol :- Binary Search Is Efficient Method As Compared To
Linear Search as The Time Complexity Of Linear Search Is O(n)
Whereas , In Binary Search , Time Complexity Is O(Logn) .

You might also like