Bhumika DAA File
Bhumika DAA File
AIM:To implement Linear Search and Binary Search and analyse its time
complexity.
@LINEAR SEARCH:
PROGRAM CODE:
#include<iostream>
using namespace std;
int main()
{
int a[10],i,n,num,index;
clock_t t1,t2;
float t;
t1=clock();
cout<<"_______BHUMIKA_______\n";
cout<<"\nEnter number of elements for an array:";
cin>>n;
cout<<"\nEnter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter a number to search:";
cin>>num;
for(i=0;i<n;i++)
{
if(a[i]==num)
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
{
index=i;
break;
}
}
cout<<"\nThe elements is found at index number:"<<index;
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}
OUTPUT:
OUTPUT:
a. MERGE SORT
PROGRAM CODE:
#include <iostream>
using namespace std;
void Merge(int *a, int low, int high, int mid)
{
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
clock_t t1,t2;
float t;
t1=clock();
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
temp[k] = a[i];
k++;
i++;
}
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
else
{
temp[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
temp[k] = a[i];
k++;
i++;
}
while (j <= high)
{
temp[k] = a[j];
k++;
j++;
}
for (i = low; i <= high; i++)
{
a[i] = temp[i-low];
}
}
MergeSort(arr, 0, n-1);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
t2=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}
PROGRAM CODE:
#include<iostream>
#include<cstdlib>
using namespace std;
// Swapping two values.
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Partition(int a[], int low, int high)
{
int pivot, index, i;
index = low;
pivot = high;
for(i=low; i < high; i++)
{
if(a[i] < a[pivot])
{
swap(&a[i], &a[index]);
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
index++;
}
}
swap(&a[pivot], &a[index]);
return index;
}
int RandomPivotPartition(int a[], int low, int high)
{
int pvt, n, temp;
n = rand();
pvt = low + n%(high-low+1);
swap(&a[high], &a[pvt]);
return Partition(a, low, high);
}
int QuickSort(int a[], int low, int high)
{
int pindex;
if(low < high)
{
pindex = RandomPivotPartition(a, low, high);
QuickSort(a, low, pindex-1);
QuickSort(a, pindex+1, high);
}
return 0;
OUTPUT:
PROGRAM CODE:
#include <iostream>
using namespace std;
void BubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1; ++j)
{
if (arr[j] > arr[j+1])
{
arr[j] = arr[j]+arr[j+1];
arr[j+1] = arr[j]-arr[j + 1];
arr[j] = arr[j]-arr[j + 1];
}
}
}
}
int main()
{
int n, i;
BubbleSort(arr, n);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}
OUTPUT:
PROGRAM CODE:
#include <iostream>
using namespace std;
void SelectionSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = i+1; j < n; ++j)
{
if (arr[i] > arr[j])
{
arr[i] = arr[i]+arr[j];
arr[j] = arr[i]-arr[j];
arr[i] = arr[i]-arr[j];
}
}
}
}
int main()
{
int n, i;
OUTPUT:
PROGRAM CODE:
#include <iostream>
using namespace std;
void MaxHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;
while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
BellmanFord(graph, V, E, 0);
t2=clock();
OUTPUT:
PROGRAM CODE:
#include <bits/stdc++.h>
#define N 4
using namespace std;
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
if(board[i][j])
cout << "Q ";
else cout<<". ";
printf("\n");
}
}
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}
bool solveNQUtil(int board[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1))
return true;
board[i][col] = 0;
}
}
return false;
}
bool solveNQ()
if (solveNQUtil(board, 0) == false) {
cout << "Solution does not exist";
return false;
}
printSolution(board);
return true;
}
int main()
{
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
solveNQ();
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
OUTPUT:
OUTPUT:
PROGRAM CODE:
#include<iostream>
#include<string>
using namespace std;
int longestCommonSubsequece(string str1, string str2, int len1, int len2)
{
int i, j;
int LCS[len1+1][len2+1];
for(i=0;i<=len1;i++)
LCS[i][0]=0;
for(j=0;j<=len2;j++)
LCS[0][j]=0;
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
{
LCS[i][j]=1+LCS[i-1][j-1];
}
}
int main()
{
string str1,str2;
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
cout<<"Enter first string ";
getline(cin, str1);
cout<<"Enter second string ";
getline(cin, str2);
int len1=str1.length();
int len2=str2.length();
cout<<"Length of longest common subsequence is
"<<longestCommonSubsequece(str1,str2,len1,len2);
OUTPUT:
PROGRAM CODE:
@ NAÏVE STRING MATCHING ALGORITHM:
#include <bits/stdc++.h>
using namespace std;
void search(string& pat, string txt)
{
int M = pat.size();
int N = txt.size();
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
if (j
== M)
cout << "Pattern found at index " << i << endl;
}
}
int main()
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
{
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
string txt = "AABAACAADAABAAABAA";
string pat = "AABA";
search(pat, txt);
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}
OUTPUT:
OUTPUT:
int i = 0;
int j = 0;
while ((N - i) >= (M - j)) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
printf("Found pattern at index %d ", i - j);
j = lps[j - 1];
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
void computeLPSArray(char* pat, int M, int* lps)
{
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
OUTPUT: