0% found this document useful (0 votes)
72 views6 pages

Assignment 3 - Computational Thinking

The document discusses computational thinking concepts including: 1) Calculating the number of comparisons needed for a binary search of a list with 13 elements (4 comparisons). 2) Comparing the efficiency of average linear and binary searches on lists of various sizes, showing binary is much faster. 3) Creating a flowchart demonstrating the steps of a binary search. 4) Using linear search to find the smallest number in an unsorted list since binary search only works on sorted lists. 5) Walking through an example of using binary search to find an element in a list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views6 pages

Assignment 3 - Computational Thinking

The document discusses computational thinking concepts including: 1) Calculating the number of comparisons needed for a binary search of a list with 13 elements (4 comparisons). 2) Comparing the efficiency of average linear and binary searches on lists of various sizes, showing binary is much faster. 3) Creating a flowchart demonstrating the steps of a binary search. 4) Using linear search to find the smallest number in an unsorted list since binary search only works on sorted lists. 5) Walking through an example of using binary search to find an element in a list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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.

2) a) In order to calculate the length of an average successful search, we simply have to


use the following formulas for a sequential search (according to the lecture): (n + 1)/2,
and for a binary search: log2(n).
In this case, n equals to 100 000, therefore
● Average for linear/sequential: (100 000 + 1) / 2 = 50 000.5
● Average for binary: log2(100 000) ≈ 16.6
● So, indeed the average binary successful search is 50 000.5/16.6 ≈ 3010 times
quicker that the average sequential one.
This shows that a binary search is a lot quicker than the linear one, which makes sense
since during a sequential search, one has to go through all the terms of the list, while during a
binary search, this is not needed.

b) Code:
from plot import FunPlot
from math import *

def fn1(x): return x


def fn2(x): return ceil(log(x+1, 2))
x = FunPlot(x_min=0, x_max = 200, title="Worst case result search algorithms")
x.add_function(fn1, label="Linear")
x.add_function(fn2, label="Binary")

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.

d) list1 = [ 42, 36, 21, 55, 32, 18, 29]


smol = list1[0]

for i in list1:
if i < smol:
smol = i

print "Smallest number is: ", smol

OR: list1 = [ 42, 36, 21, 55, 32, 18, 29]

print "Smallest number is: ", min(list1)

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.

You might also like