Assignment 3 - Computational Thinking
Assignment 3 - Computational Thinking
1) Here, n = 13, or we can say that in the list there are 13 elements. To calculate the largest
number of key comparisons we can use the following formula: log2(n+1) and if the result
is a floating point number, we have to compensate it.( example: if result = 2.8, the actual
result will be 3). So, the largest number of comparisons is: log2(13+1) = log2(14) ≈ 3.8.
Therefore, the result is 4.
b) Code:
from plot import FunPlot
from math import *
x.show()
We can see that the more numbers we have the longer it takes to find the one that we are
looking for with the linear algorithm, whereas, the binary one seems to remain at a fairly stable
number of steps, which is coherent since, in theory the binary algorithm is a lot quicker than the
linear one.
3) There we have the flowchart for the binary search:
In order to make it, we analyzed the possibility of n being equal to the number we are
looking for, smaller or larger. In the case of it being larger, we move ‘to the right’ as
stated in the lecture, whereas if it is smaller, we move to the ‘left’. That is continued until
the number corresponds to the search key.
4) a) Considering we are using an unsorted list, we cannot use binary search since this search
algorithm only works for a sorted list. Although it might take longer to find the smallest number
using the linear search algorithm, we don’t have another option. Therefore, the linear search
seems to be the way to do it .
b)
c) List: 42, 36, 21, 55, 32, 21, 55, 32, 18, 29.
Using the linear search algorithm, we look through each number.
● So, 42 is for now the only number, so automatically the smallest.
● Then, 36 is smaller, so it becomes the smallest number.
● Continuing, 21 < 36, so there is a new smallest number.
● 55 > 21, therefore, the smallest number stays the same.
● 32 > 21; nothing changes.
● 21 = 21, so no change.
● 55 > 21, 32 > 21, continuing.
● 18 < 21. The smallest number in the list is now 18.
● Finally, 29 > 18, so 18 is the smallest number in the list.
To conclude, this is how the linear search algorithm finds the smallest number in a list.
for i in list1:
if i < smol:
smol = i
5) a) Using binary search, we first have to find the middle element fo the list, using the formula
stated in the lecture which is n/2. Therefore, in this case, we have to start at the 20/2 = 10th
element (if the list was starting at 0, we would have used the (n-1)/2 formula, but it is not the
case here), which is Hanson.
● By comparing Casy and Hanson, it is clear that H is higher in the alphabet than C,
therefore, we move to the left part and repeat the process.
● We find the middle element, or the 10/2 = 5th element, which is Davis. Still, D is higher in
the alphabet than C. We continue, still moving to the left.
● We compare Casy to the 5/2 = 2.5 = 3rd (if the result is a floating number we have to
compensate, because 2.5 is not part of the list) element, Boyer. Indeed, C is higher than
B in the alphabet so we move to the right this time.
● This list goes from Boyer to Davis, so Boyer is the 1st term and Davis the 3rd and last
term. We now compare Casy to the 3/2 = 1.5 = 2nd element, which, is indeed Casy.
● To conclude, we found Casy in 4 steps/comparisons, using the binary search.
b) Using linear search, we compare Casy to each number, starting with Allen, then Baley, Boyer
and Casy, which corresponds to the person we were looking for. In this case, linear search is a
lot quicker because Casey is situated at the start of the list, so the search does not go on for
long, even though there are the same amount of steps, which are 4.
6)
2 4 6 6 3 3 3 3 9 9 9 9 9 9 9 9
1 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8
2 2 2 2 4 4 4 4 8 8 8 8 8 8 8 8
4 4 4 4 4 4 4 4 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
In order to solve this question I have used the method of guess and check, first to try and find a
pattern. Afterwards, I realized that going backwards it is more efficient and could therefore lead
to the result needed. By going backwards, I was able to transform the 16 x 16 numbers into 8 x
16 numbers, which then can lead to having 2 separate 8 number piles, then one pile is divided
in two equal 4 number piles, then one of the 4 number piles is divided into 2, so, having in the
second row 2x 2 number piles, 1 x 4 number piles and 1 x 8 number piles. (note that in the
second row there are fairly small numbers, dictated by the first row. If there would’ve been larger
numbers, the sequence would end sooner, therefore, not in 7 steps). In the end, it does not
matter which numbers appear in the first row, but how many times they appear, because that is
what dictates the further rows. To conclude, here we have an example of the numbers that the
student could have written on the board in the first place.