You are on page 1of 18

CE355-DAA Student ID: 20ce120

Charotar University of Science and Technology [CHARUSAT]

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

Chandubhai S. Patel Institute of Technology [CSPIT]


U & P U. Patel Department of Computer Engineering

Practical List
Subject code : CE355 Semester : 5 Academic : 2022-23
Year

Subject : Design and Analysis of Algorithm


name

Aim: Implement and analyse algorithms given below.


1. Factorial Iterative.
2. Factorial Recursive.
3. Fibonacci Series Iterative.
4. Fibonacci Series Recursive.
5. Linear Search.
6. Binary Search.

1. Factorial Iterative.

Code:
U & P U Patel Department of Computer Engineering Page
CE355-DAA Student ID: 20ce120

Output:

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

Graph :

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

2. Fibonacci Series (Iterative and Recursive).

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

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;

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

Output :

Graph :

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

3. Linear Search and Binary Search.

 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 = 30, count = 0;


    cout << linearSearch(a, 10, key);
    cout << "\n The instruction count for best case is :  " << count << endl;

    key = 70, count = 0;


    cout << linearSearch(a, 10, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    // 2

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

    cout << "\n2nd :\n";


    key = 22, count = 0;
    cout << linearSearch(b, 12, key);
    cout << "\n The instruction count for worst case is :  " << count << endl;

    key = 70, count = 0;


    cout << linearSearch(b, 12, key);
    cout << "\n The instruction count for best case is :  " << count << endl;

    key = 20, count = 0;


    cout << linearSearch(b, 12, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    // 3
    cout << "\n3rd :\n";
    key = 22, count = 0;
    cout << linearSearch(c, 15, key);
    cout << "\n The instruction count for worst case is :  " << count << endl;

    key = 50, count = 0;


    cout << linearSearch(c, 15, key);
    cout << "\n The instruction count for best case is :  " << count << endl;

    key = 23, count = 0;


    cout << linearSearch(c, 15, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    // 4
    cout << "\n4th :\n";
    key = 22, count = 0;
    cout << linearSearch(d, 17, 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;

    key = 80, count = 0;


    cout << linearSearch(d, 17, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    // 5
    cout << "\n5th :\n";
    key = 22, count = 0;
    cout << linearSearch(e, 20, key);
    cout << "\n The instruction count for worst case is :  " << count << endl;

    key = 90, count = 0;

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

    cout << linearSearch(e, 20, key);


    cout << "\n The instruction count for best case is :  " << count << endl;

    key = 70, count = 0;


    cout << linearSearch(e, 20, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    return 0;
}
Output:

Graph :

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

 Binary Search

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

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

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

    cout << "\n1st :\n";


    key = 22, count = 0;
    cout << binarySearch(a, 0, 10, key);
    cout << "\n The instruction count for worst case is :  " << count << endl;
    key = 64, count = 0;
    cout << binarySearch(a, 0, 10, key);
    cout << "\n The instruction count for best case is :  " << count << endl;
    key = 30, count = 0;
    cout << binarySearch(a, 0, 10, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    // 2
    cout << "\n2nd :\n";
    key = 22, count = 0;
    cout << binarySearch(b, 0, 12, key);
    cout << "\n The instruction count for worst case is :  " << count << endl;
    key = 50, count = 0;
    cout << binarySearch(b, 0, 12, key);
    cout << "\n The instruction count for best case is :  " << count << endl;
    key = 80, count = 0;
    cout << binarySearch(b, 0, 12, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    // 3
    cout << "\n3rd :\n";
    key = 22, count = 0;
    cout << binarySearch(c, 0, 15, key);
    cout << "\n The instruction count for worst case is :  " << count << endl;
    key = 64, count = 0;
    cout << binarySearch(c, 0, 15, key);
    cout << "\n The instruction count for best case is :  " << count << endl;
    key = 20, count = 0;
    cout << binarySearch(c, 0, 15, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    // 4
    cout << "\n4th :\n";
    key = 22, count = 0;
    cout << binarySearch(d, 0, 17, key);
    cout << "\n The instruction count for worst case is :  " << count << endl;
    key = 30, count = 0;
    cout << binarySearch(d, 0, 17, key);
    cout << "\n The instruction count for best case is :  " << count << endl;
    key = 64, count = 0;
    cout << binarySearch(d, 0, 17, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    // 5

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

    cout << "\n5th :\n";


    key = 22, count = 0;
    cout << binarySearch(e, 0, 20, key);
    cout << "\n The instruction count for worst case is :  " << count << endl;
    key = 47, count = 0;
    cout << binarySearch(e, 0, 20, key);
    cout << "\n The instruction count for best case is :  " << count << endl;
    key = 3, count = 0;
    cout << binarySearch(e, 0, 20, key);
    cout << "\n The instruction count for average case is :  " << count <<
endl;
    return 0;
}
Output :

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

Graph :

Bubble Sort:
U & P U Patel Department of Computer Engineering Page
CE355-DAA Student ID: 20ce120

Code:

Output:

U & P U Patel Department of Computer Engineering Page


CE355-DAA Student ID: 20ce120

Best Case:

U & P U Patel Department of Computer Engineering Page

You might also like