2.3.1. Analysis, Design and Comparison of Algorithms
2.3.1. Analysis, Design and Comparison of Algorithms
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
Analysis of Algorithms
Time of Complexity
The time complexity of an algorithm is how much time it requires to solve a particular
problem. The time complexity is measured using a notation called big-o notation, which
shows the effectiveness of the algorithm. It shows an upper limit for the amount of time
taken relative to the number of data elements given as an input. This is good because it
allows you to predict the amount of time it takes for an algorithm to finish given the number
of data elements.
You can think of this as a graph, as the number of data elements entered against the time
taken to complete the algorithm. This will be helpful for showing the relationships between
time and the number of elements inputted. These are shown below.
Big-O notation is written in the form O(n), where n is the relationship between n: the
number of inputted entities, and O(n) is the time relationship. Below are examples of
different big o notations:
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
When calculating the time complexity, you should think logically through the algorithm.
Below are a few examples:
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
This example is logarithmically related to the Logarithmic Time Complexity:
number of items inputted (it’s important to
understand logs for this, they will be explained Logarithms are explained below.
later on):
For example:
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
What a Polynomial Time Complexity graph looks like:
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
What a Logarithmic Time Complexity graph looks like:
As you can see from the graph, the best time complexity for an
algorithm as the number of inputted items increases is the linear
time complexity. The order goes:
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
Logarithms
A logarithm is the inverse of an exponential, an operation that determines how many times
a certain number (base) is multiplied by itself to reach another number. It might help to
check the extra resources for more information on this.
x y = log(x)
1 (20) 0
8 (23) 3
1024 (210) 10
Space Complexity
The space complexity of an algorithm is the amount of storage the algorithm takes. Space
complexity is commonly expressed using Big O (O(n)) notation. Algorithms store extra
data whenever they make a copy, this isn’t ideal. When working with lots of data, it’s not a
good idea to make copies. As this will take lots of storage which is expensive.
Designing Algorithms
An algorithm is a series of steps that completes a task. When you design an algorithm
your main objective is to complete a task, the next objectives are to get the best time
complexity and the best space complexity. When you try to minimise the time and space
complexity you might get conflicted thinking about which one of the two complexities are
more important. It is entirely dependant on the situation, below are some examples:
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
To reduce the space complexity, you make sure you perform all of the changes on the
original pieces of data. To reduce the time complexity, try to reduce the number of
embedded loops as possible. Try to reduce the number of items you have to complete the
operations on, for example the divide a conquer algorithm accomplishes this and results in
logarithmic time complexity.
Comparison of Algorithms
The exam board will mostly compare the time complexity. Occasionally they will mention
space complexity although it’s important to just understand the smaller the space
complexity the better the algorithm is.
As you can see, the linear search algorithm has a single while loop in it, this is why it’s a
linear time complexity algorithm
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
Binary Search Algorithm
A binary search algorithm is a divide and conquer algorithm, this means it splits the list into
smaller lists until it finds the item it’s searching for, since the size of the list is halved every
time it’s a Big-O notation of O(log(n)).
________________________________________________________________________
function binarySearch(list, item)
found = False
index = -1
first = 0
last = length(list) - 1
while first <= last and found = False
midpoint = int ( first + last) / 2 )
if list[midpoint] = item then
found = True
index = midpoint
else
if list[midpoint] < item then
first = midpoint + 1
else
last = midpoint - 1
endif
endif
endwhile
return index
endfunction
________________________________________________________________________
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc
Bubble Sort Algorithm
The bubble sort algorithm passes through the list evaluating pairs of items and ensuring
the larger value is above the smaller value. It has a polynomial Big-O notation, O(n2).
Below is the algorithm:
________________________________________________________________________
function bubbleSort(list, item)
found = False
i = 0
while found = False and i < length(item)
if list[i] > list[i+1]
temp = list[i]
list[i] = list[i+1]
list[i+1] = temp
endif
i = i +1
endwhile
return list
endfunction
________________________________________________________________________
https://bit.ly/pmt-cc
https://bit.ly/pmt-edu https://bit.ly/pmt-cc