Professional Documents
Culture Documents
Practical List
Subject code : CE355 Semester : 5 Academic : 2022-23
Year
1. Factorial Iterative.
Code:
U & P U Patel Department of Computer Engineering Page
CE355-DAA Student ID: 20ce120
Output:
Graph :
Code :
//_Achyut_Krishna_(20ce120)
#include <iostream>
using namespace std;
int count1 = 0;
int count2 = 0;
void fib_Itr(int n)
{
int t1 = 0, t2 = 1, nextTerm = 0;
for (int i = 1; i <= n; i++)
{
cout << t1 << " ";
nextTerm = t1 + t2;
count1++;
t1 = t2;
count1++;
t2 = nextTerm;
count1++;
}
}
int fib_Rec(int n)
{
if (n == 0 || n == 1)
{
count2++;
return n;
}
else
{
count2++;
return ((fib_Rec(n - 1)) + (fib_Rec(n - 2)));
}
}
int main()
{
int n, x = 0;
cout << "Enter the number of terms: ";
cin >> n;
fib_Itr(n);
cout << "\nThe instruction count for iterative is : " << count1 << endl;
while (x <= n)
{
cout << fib_Rec(x) << " ";
x++;
}
cout << "\nThe instruction count for recursive is : " << count2 << endl;
return 0;
Output :
Graph :
Linear Search
Code :
//_Achyut_Krishna_(20ce120)
#include <iostream>
using namespace std;
int count = 0;
int linearSearch(int a[], int n, int key)
{
for (int i = 0; i < n; i++)
{
count++;
if (key == a[i])
{
return 1;
}
}
return -1;
}
int main()
{
int key;
int a[10] = {30, 45, 50, 45, 10, 64, 70, 80, 90, 100};
int b[12] = {70, 80, 90, 30, 20, 40, 64, 50, 45, 50, 3, 5};
int c[15] = {50, 64, 70, 80, 40, 4, 45, 23, 64, 10, 80, 90, 99, 100, 101};
int d[17] = {5, 47, 50, 64, 67, 75, 64, 70, 80, 3, 40, 45, 49, 42, 55, 50,
65};
int e[20] = {90, 100, 101, 30, 33, 40, 45, 47, 5, 50, 64, 70, 80, 62, 10,
20, 30, 45, 111, 101};
// 1
cout << "\n1st :\n";
key = 22, count = 0;
cout << linearSearch(a, 10, key);
cout << "\n The instruction count for worst case is : " << count << endl;
key = 5, count = 0;
cout << linearSearch(d, 17, key);
cout << "\n The instruction count for best case is : " << count << endl;
Graph :
Binary Search
Code :
//_Achyut_Krishna_(20ce120)
#include <iostream>
using namespace std;
int count = 0;
int binarySearch(int a[], int l, int r, int key)
{
int mid;
if (l <= r)
{
mid = (l + r) / 2;
// If found at mid, then return it
if (a[mid] == key)
{
count++;
return 1;
}
// Search the left half
else if (a[mid] < key)
{
count++;
return binarySearch(a, mid + 1, r, key);
}
// Search the right half
else
{
count++;
return binarySearch(a, l, mid - 1, key);
}
}
return -1;
}
int main()
{
int key;
int a[10] = {10, 20, 30, 45, 50, 64, 70, 80, 90, 100};
int b[12] = {9, 10, 20, 23, 30, 45, 50, 64, 70, 80, 90, 100};
int c[15] = {10, 11, 20, 30, 40, 45, 50, 64, 70, 80, 90, 99, 100, 101,
110};
int d[17] = {2, 3, 5, 10, 11, 12, 20, 29, 30, 33, 40, 45, 47, 50, 64, 67,
70};
int e[20] = {1, 2, 3, 5, 10, 11, 12, 20, 30, 45, 47, 49, 50, 64, 67, 70,
80, 90, 100, 101};
// 1
Graph :
Bubble Sort:
U & P U Patel Department of Computer Engineering Page
CE355-DAA Student ID: 20ce120
Code:
Output:
Best Case: