0% found this document useful (0 votes)
71 views14 pages

Source Code:: Implementation of Searching Algorithms Over An Array Based List Linear Search Binary Search

The document describes Atif Jalal's work on implementing searching algorithms like linear search and binary search on an array-based list. It includes source code for linear search, binary search, searching for XA+YB and calculating the dot product of two vectors, linear search on a string array, binary search and insertion sort on an integer array, using binary search to find specific elements, and using binary search to find and delete elements from a number list. The tasks involve writing and testing programs for various searching and sorting algorithms on arrays and vectors.

Uploaded by

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

Source Code:: Implementation of Searching Algorithms Over An Array Based List Linear Search Binary Search

The document describes Atif Jalal's work on implementing searching algorithms like linear search and binary search on an array-based list. It includes source code for linear search, binary search, searching for XA+YB and calculating the dot product of two vectors, linear search on a string array, binary search and insertion sort on an integer array, using binary search to find specific elements, and using binary search to find and delete elements from a number list. The tasks involve writing and testing programs for various searching and sorting algorithms on arrays and vectors.

Uploaded by

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

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 1:

Source Code:
#include <string>

#include <iostream>

using namespace std;

void st(int arr[],int size,int item)

int beg=1;

int end=size;

int mid=(beg+end)/2;

int i;

for (i=0;i<size;i++)

if(beg<=end && arr[mid]!=item)

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;

cout<<item<<" is present in array...";

cout<<"\nYour New Array After Deleting "<<item<<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;

cout<<item<<" is not present in array...!";

int main()

int arr[10]={11,22,33,36,45,52,57,60,64,78};

int a;

cout<<"\nYour Array Contain the following elements : ";

for(int p=0;p<10;p++)

cout<<arr[p]<<",";

cout<<"\n\nEnter The number Which Mantion in Above : ";

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

Test the program using A= (16, -6,7), B= (4,2,-3), X= 2, Y= -5

Source Code:
#include <iostream>

using namespace std;

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];

cout<<endl<<endl<<"After Performing XA + YB : ";

double sum=addd+add;

cout<<sum<<endl;

cout<<endl<<"After Performing A . B : "<<multiply;

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>

using namespace std;

void searchString(string A[],int size, string target)

int j;

for(j=0; j < size; j++)

if(A[j] == target)

cout << A[j] << endl;

j=j+1;

cout<<"location :"<<j<<endl;

else(A[j] != target);

cout<<"\n\n"<<target<<" is not present in array!";

}
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 arr[5] = { "atif","imran","zohaib","hassan","zain" };

string target;

cout<<endl<<"You Array is : ";

for(int i=0;i<5;i++)

cout<<arr[i]<<", ";

cout<<endl<<"\n\nEnter The Target Which You want to Find : ";

cin>>target;

searchString( arr, 5, 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>

using namespace std;


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 arr[4]={"a","b","c","d"};

int position,lowerbound,upperbound;

lowerbound=0;

upperbound = 4-1;

int comparisonCount = 1;

position = ( lowerbound + upperbound) / 2;

string key;

cout<<"Your Array Contain the following elements : ";

for(int p=0;p<4;p++)

cout<<arr[p]<<",";

cout<<endl;

cout<<"\nEnter The Target Which You want to Find : ";

cin>>key;

while((arr[position] != key) && (lowerbound <= upperbound))

comparisonCount++;

if (arr[position] > key)

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 = (lowerbound + upperbound) / 2;

if (lowerbound <= upperbound)

position=position+1;

cout<< "\n\nThe number was found in array subscript "<< position<<endl<<endl;

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 arr[2]={52, 33};

int position,lowerbound,upperbound;

lowerbound=0;

upperbound = 2-1;

position = ( lowerbound + upperbound) / 2;

int key;

cout<<"Your Array Contain the following elements : ";

for(int p=0;p<2;p++)

cout<<arr[p]<<",";

cout<<endl;

cout<<"\nEnter The Target Which You want to Find : ";

cin>>key;

while((arr[position] != key) && (lowerbound <= upperbound))

if (arr[position] > 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 = (lowerbound + upperbound) / 2;

if (lowerbound <= upperbound)

position=position+1;

cout<< "\nThe number was found in array subscript : "<< position<<endl<<endl;

//cout<<"Your Result Is Printed:"<<arr[position-1]<<endl;

else

cout<< "\n\nSorry, the number is not present in this array.! "<<endl;

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>

using namespace std;


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

void st(int arr[],int size,int item)

int beg=1;

int end=size;

int mid=(beg+end)/2;

int i;

for (i=0;i<size;i++)

if(beg<=end && arr[mid]!=item)

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;

cout<<item<<" is present in array...";

cout<<"\nYour New Array After Deleting "<<item<<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;

cout<<item<<" is not present in array...!";

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;

cout<<"\nYour Array Contain the following elements : ";

for(int p=0;p<10;p++)

cout<<arr[p]<<",";

cout<<"\n\nEnter The number Which Mantion in Above : ";

cin>>a;

st(arr,10,a);

return 0;

Output:

You might also like