Skip to the content. Back to Wb 2021 01 11

Algorithms

Tools for Analysis and Comparison

Mathematical Concepts

Understanding functions

Understanding exponentials

Understanding factorials

Determining Algorithmic Complexity

Definitions

1 ≪ logn ≪ n ≪ n2 ≪ 2x

O(1) (Constant time)

O(logn) (Logarithmic time)

O(n) (linear time)

O(n2) (Polynomial time)

O(2n) (Exponential time)

O(2n) describes an algorithm where the time taken to execute will double with every additional item added to the data set. The execution time grows in exponential time and quickly becomes very large; e.g. printing all binary numbers that can be represented in n bits

Practice

Let f(n) = 5n4+2n+logn
Then f(n) has complexity O(n4)

Determine O for functions with the following number of instructions:

What is the time complexity of the code below?

int count_to(int N) {
    int count = 0;
    for (int i = N; i > 0; i /= 2) {
        for (int j = 0; j < i; j++) {
            count += 1;
        }
    }
    return count;
}

Place the following algorithms in order of time complexity, with the most efficient algorithm first:

CAFDBE

Explain why algorithms with time complexity O(n!) are generally considered not to be helpful in solving a problem. Under what circumstances would such an algorithm be considered?

Complexity and Data Structures

Search algorithms

Linear and Binary

int linear_search(int search_in[], int nelem, int looking_for) {
    for (int i = 0; i < nelem; i++) {
        if (search_in[i] == looking_for) {
            return i;
        }
    }
    return -1
}
int iterative_binary_search(int search_in[], int nelems, int looking_for) {
    int start = 0;
    int end = nelems;
    while (start != end) {
        int midpoint = (end - start) / 2 + start;
        if (search_in[midpoint] == looking_for) {
            return midpoint;
        }
        if (search_in[midpoint] < looking_for) {
            start = midpoint + 1;
        } else {
            end = midpoint;
        }
    }
    return -1;
}

What is the maximum number of items what would need to be examined to find a particular item in a binary search of one million items? 19 = floor(log2(1,000,000))

Does the list need to be sorted? Yes

The list of positive even numbers up to and including 1000 is stored in a variable x.

An attempt is made to find the number 607 in this list. Show the first three stages for:

int recursive_binary_search(int search_in[], int start, int end, int looking_for) {
    if (start == end) {
        return -1;
    }
    int midpoint = (end - start) / 2 + start;
    if (search_in[midpoint] == looking_for) {
        return midpoint;
    }
    if (search_in[midpoint] < looking_for) {
        start = midpoint + 1;
    } else {
        end = midpoint;
    }
    return binary_search(search_in, start, end, looking_for);
}
#include <stddef.h>
#include <stdbool.h>
typedef struct _node {
    int value;
    struct _node* left;
    struct _node* right;
} node;
bool recursive_bst_search(int looking_for, node* current_node) {
    if (current_node == NULL) {
        return false;
    }
    if (current_node->item == looking_for) {
        return true;
    }
    if (current_node->item > looking_for) {
        return recursive_bst_search(looking_for, current_node->left);
    } else {
        return recursive_bst_search(looking_for, current_node->right);
    }
}