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

0% found this document useful (0 votes)
21 views10 pages

Term Work 4th Sem

Uploaded by

tvesag11
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)
21 views10 pages

Term Work 4th Sem

Uploaded by

tvesag11
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/ 10

IV-SEM-TCS-409(DAA-LAB)

PROBLEM STATEMENT 1 : Given an array of nonnegative integers, design a linear


algorithm and implement it using a program to find whether given key element is present in
the array or not. Also, find total number of comparisons for each input case. (Time
Complexity = O(n), where n is the size of input).

CODE:
#include <iostream>
#include <fstream>
using namespace std;
// Linear search function
int linear_search(int *arr, int n, int key,int &c)
{
for (int i = 0; i < n; i++)
{
c++;
if (arr[i] == key)
{
return 1;
break;
}
}
return 0;
}
int main()
{
ifstream inputFile("input.txt"); // Opening the input file
ofstream outputFile("output.txt"); // Opening the output file
if (!inputFile.is_open())
{
cerr << "Error opening the input file!" << endl;
return 1;

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

}
if (!outputFile.is_open())
{
cerr << "Error opening the output file!" << endl;
return 1;
}
int t,c;
inputFile >> t; // Read number of test cases from input file
for (int i = 0; i < t; i++)
{
int n;
inputFile >> n; // Reading size of array from input file
int *arr = new int[n]; //dynamic memory allocation for array
for (int j = 0; j < n; j++)
inputFile >> arr[j]; // Reading elements of array from input fil
int key;
inputFile >> key; // Reading the key from input file
int p = linear_search(arr, n, key, c);
if (p)
outputFile << "Present "<<c<<endl;
else
outputFile << "Not Present "<<c<<endl;
delete[] arr; // Free memory allocated for array
}
inputFile.close(); // Closing the input file
outputFile.close(); // Closing the output file
return 0;
}

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

PROBLEM STATEMENT 2 : Given an array of nonnegative integers, design a linear


algorithm and implement it using a program to find whether given key element is present in
the array or not. Also, find total number of comparisons for each input case. (Time
Complexity = O(n), where n is the size of input) Sample I/O Problem - 1: Given an already
sorted array of positive integers, design an algorithm and implement it using a program to
find whether given key element is present in the array or not. Also, find total number of
comparisons for each input case. (Time Complexity = O(nlogn), where n is the size of input).
CODE:
#include <iostream>
#include <fstream>
using namespace std;
int binary_search(int *arr, int n, int key, int &c) // Binary Search function
{
int left = 0;
int right = n - 1;
while (left <= right)
{
c++;
int mid = left + (right - left) / 2;
if (arr[mid] == key)
return 1;
else if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return 0;
}

int main()
{
ifstream inputFile("input.txt"); // Opening the input file

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

ofstream outputFile("output.txt"); // Opening the output file


if (!inputFile.is_open()) {
cerr << "Error opening the input file!" << endl;
return 1;}
if (!outputFile.is_open()) {
cerr << "Error opening the output file!" << endl;
return 1;
}
int t,c;
inputFile >> t; // Read number of test cases from input file
for (int i = 0; i < t; i++)
{
int n;
inputFile >> n; // Reading size of array from input file
int *arr = new int[n]; //dynamic memory allocation for array
for (int j = 0; j < n; j++)
inputFile >> arr[j]; // Reading elements of array from input fil
int key;
inputFile >> key; // Reading the key from input file
int p = binary_search(arr, n, key, c);
if (p)
outputFile << "Present "<<c<<endl;
else
outputFile << "Not Present "<<c<<endl;
delete[] arr; // Free memory allocated for array
}
inputFile.close(); // Closing the input file
outputFile.close(); // Closing the output file
return 0;
}

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

PROBLEM STATEMENT 3 : Given an already sorted array of positive integers, design an


algorithm and implement it using a program to find whether a given key element is present in
the sorted array or not. For an array arr[n], search at the indexes arr[0], arr[2],
arr[4],.....,arr[2k] and so on. Once the interval (arr[2k] < key < arr[ 2k+1] ) is found, perform
a linear search operation from the index 2k to find the element key. (Complexity < O(n),
where n is the number of elements need to be scanned for searching).

CODE:

#include <iostream>
#include <fstream>
using namespace std;
// Jump Search function
int jump_search(int arr[],int n,int k,int &c)
{
int j,l,r;
if(arr[0]==k)
return 1;
for(j=1;j<n;j=j*2)
{
c++;
if(arr[j]==k)
return 1;
if((k>arr[j] && j*2>n) || (k>arr[j] && k<arr[j*2]))
{
l=j+1;
if(j*2>n)
r=n;
else r=j*2;
while(l<=r)
{
c++;

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

int mid=l+(r-l)/2;
if(k==arr[mid])
return 1;
if(k<arr[mid])
r=mid-1;
if(k>arr[mid])
l=mid+1;
}
}
}
return 0;
}

int main()
{
ifstream inputFile("input.txt"); // Opening the input file
ofstream outputFile("output.txt"); // Opening the output file
if (!inputFile.is_open())
{
cerr << "Error opening the input file!" << endl;
return 1;
}
if (!outputFile.is_open())
{
cerr << "Error opening the output file!" << endl;
return 1;
}
int t,c;
inputFile >> t; // Read number of test cases from input file
for (int i = 0; i < t; i++)

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

{
int n;
inputFile >> n; // Reading size of array from input file
int *arr = new int[n]; //dynamic memory allocation for array
for (int j = 0; j < n; j++)
inputFile >> arr[j]; // Reading elements of array from input file
int key;
inputFile >> key; // Reading the key from input file
int p = jump_search(arr, n, key, c);
if (p)
outputFile << "Present "<<c<<endl;
else
outputFile << "Not Present "<<c<<endl;
delete[] arr; // Free memory allocated for array
}
inputFile.close(); // Closing the input file
outputFile.close(); // Closing the output file
return 0;
}

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A


IV-SEM-TCS-409(DAA-LAB)

NAME: TVESA GUPTA ROLL NUMBER: 71 SECTION: A

You might also like