Source Code:: Implementation of Searching Algorithms Over An Array Based List Linear Search Binary Search
Source Code:: Implementation of Searching Algorithms Over An Array Based List Linear Search Binary Search
Task 1:
Source Code:
#include <string>
#include <iostream>
int beg=1;
int end=size;
int mid=(beg+end)/2;
int i;
for (i=0;i<size;i++)
if(item<arr[mid])
end=mid-1;
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
}
else
beg=mid+1;
mid= (beg+end)/2;
if (arr[mid]==item)
cout<<endl;
for(int j=mid;j<size-1;j++)
arr[j]=arr[j+1];
size=size-1;
for(int o=0;o<size;o++)
cout<<arr[o]<<endl;
return;
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
else
cout<<endl<<endl;
int main()
int arr[10]={11,22,33,36,45,52,57,60,64,78};
int a;
for(int p=0;p<10;p++)
cout<<arr[p]<<",";
cin>>a;
st(arr,10,a);
return 0;
Output:
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
Task 2: Suppose A and B are n-elements vector array in memory and X an Y are scalars. Write a
program to find.
a) XA + YB
b) A. B
Source Code:
#include <iostream>
int main()
int arra[3]={16,-6,7};
int arrb[3]={4,2,3};
int x=2;
int y=5;
cout<<"x=2 , y=5"<<endl<<endl;
int addd=0,add=0,multiply=0;
cout<<"Array A "<<endl;
for(int c=0;c<3;c++)
cout<<arra[c];
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
addd=x*arra[c];
cout<<",";
cout<<endl<<endl<<"Array B "<<endl;
for(int d=0;d<3;d++)
cout<<arrb[d];
add=y*arrb[d];
cout<<",";
for(int e=0;e<3;e++)
multiply=arra[e]*arrb[e];
double sum=addd+add;
cout<<sum<<endl;
return 0;
Output:
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
Task 3: Translate the linear search algorithm into a program which either find the location LOC
where ITEM appears in ARRAY or return LOC=0
Source Code:
#include <iostream>
#include <string>
int j;
if(A[j] == target)
j=j+1;
cout<<"location :"<<j<<endl;
else(A[j] != target);
}
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
}
int main ( )
string target;
for(int i=0;i<5;i++)
cout<<arr[i]<<", ";
cin>>target;
return 0;
Output:
Task 4: Translate binary search and insertion algorithm into a program which finds either the
location LOC where ITEM appears in ARRAY or the location LOC where ITEM should be inserted into
ARRAY. (For object 3, 4, and 5 take an array of 10 elements given below)
Source Code:
#include <iostream>
#include <string>
int main()
string arr[4]={"a","b","c","d"};
int position,lowerbound,upperbound;
lowerbound=0;
upperbound = 4-1;
int comparisonCount = 1;
string key;
for(int p=0;p<4;p++)
cout<<arr[p]<<",";
cout<<endl;
cin>>key;
comparisonCount++;
upperbound = position - 1;
else
lowerbound = position + 1;
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
}
position=position+1;
cout<< "The binary search found the number after " << comparisonCount<< "
comparisons.\n";
else
cout<< "\n\nSorry, the number is not present in this array. \nThe binary search made
"<<comparisonCount << " comparisons."<<endl;
return 0;
Output:
Task 5: Write a program which uses Binary Search to search the elements 52,and 33 and print it.
Source Code:
#include <iostream>
#include <string>
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
using namespace std;
int main()
int position,lowerbound,upperbound;
lowerbound=0;
upperbound = 2-1;
int key;
for(int p=0;p<2;p++)
cout<<arr[p]<<",";
cout<<endl;
cin>>key;
upperbound = position - 1;
else
{
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
lowerbound = position + 1;
position=position+1;
else
return 0;
Output:
Task 6: Write a program which uses Binary Search to search the elements 45,and 78 and delete it.
NUMBERS=[11,22,33,36,45,52,57,60,64,78]
Source Code:
#include <string>
#include <iostream>
int beg=1;
int end=size;
int mid=(beg+end)/2;
int i;
for (i=0;i<size;i++)
if(item<arr[mid])
end=mid-1;
else
beg=mid+1;
mid= (beg+end)/2;
if (arr[mid]==item)
{
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
cout<<endl;
for(int j=mid;j<size-1;j++)
arr[j]=arr[j+1];
size=size-1;
for(int o=0;o<size;o++)
cout<<arr[o]<<endl;
return;
else
cout<<endl<<endl;
int main()
int arr[10]={11,22,33,36,45,52,57,60,64,78};
Atif Jalal Lab 04: Implementation of searching algorithms over an Array based List
02-235191-027 Linear Search
BS (IT)-3A Binary Search Date: 10 July, 2020
int a;
for(int p=0;p<10;p++)
cout<<arr[p]<<",";
cin>>a;
st(arr,10,a);
return 0;
Output: