We demonstrate two search algorithms (e.g. finding the position of an element from a data structure - in this case a list), the Simple Search and the Binary Search. The relationship between their time complexity and the size of the data structure, is also examined. Through simulations, we see that on average, the Simple Search runs in linear time (\(O(n)\)) and that the Binary Search runs in log time (\(O(\log(n))\)).

The search algorithms are written in Python and then passed to R for plotting.

Helper functions

import random
def sorted_list(n):
    """ Returns a sorted list of n unique numbers """
    list_out = [_i for _i in range(n)]
    return list_out
def random_list(n):
    """ Returns a randomised list of n unique numbers """
    list_out = sorted_list(n)
    random.shuffle(list_out)
    return list_out

Comparing run times

We can see by comparing the average simulated run times of the simple and binary search, that they run in linear and log time, respectively.

# passing list from python to R
avg_searches_simple <- py$avg_searches_simple
avg_searches_binary <- py$avg_searches_binary


n = 1:(length(avg_searches_simple))
linear <- n / 2
log_2 <- log(x = n, base = 2)

par(mfrow = c(1,2))
plot(n, avg_searches_simple, main = "Simple Search", ylab = "Average search steps", xlab = "Number of elements")
lines(linear, col = "red")
plot(n, avg_searches_binary, main = "Binary Search", ylab = "Average search steps", xlab = "Number of elements")
lines(log_2, col = "red")