Notes

  • Different algorithms can be created but still complete the same task
  • Algorithms that appear similar can yield different results
  • Boolean expressions can be written as equivalent conditional statements, and the opposite can be true
  • Different algorithms can be developed to solve similar problems
  • However, small changes to algorithms could also yield vastly different results
  • Modifications and combinations of algorithms can create new ideas
  • Algorithm: A process or set of rules to be followed in calculations or other problem solving operations, especially by a computer
  • Binary Search: starts at the middle of a sorted data set of numbers and eliminates half of the data; this process repeats until the desired value is found or all elements have been eliminated
  • Data has to be sorted in order
  • Binary search is often more efficient than a sequential search

Question 1

IF (isCold OR isRaining) {
    stayInside <-- True
}
ELSE {
    stayInside <-- False
}

Question 2

Psuedocode and python

numbers <-- [RANDOM(1,10), RANDOM(1,10), RANDOM(1,10), RANDOM(1,10)]
topScore <-- numbers[1]
IF topScore < numbers[2] {
    topScore <-- numbers[2]
}
IF topScore < numbers[3] {
    topScore <-- numbers[3]
}
IF topScore < numbers[4] {
    topScore <-- numbers[4]
}
DISPLAY (topScore)
import random

numbers = []
counter = 1
topScore = 0

while counter <= 4:
    current = random.randint(1, 10)
    numbers.append(current)
    counter += 1

for i in range(0, len(numbers)):
    if numbers[i] > topScore:
        topScore = numbers[i]
    else:
        topScore = topScore
    

print(numbers)
print("Your high score is " + str(topScore) + "!")
[6, 10, 7, 1]
Your high score is 10!

Question 3

REPEAT UNTIL (goalReached){
    IF (CAN_MOVE(FORWARD)) {
        MOVE_FORWARD
    }
    ELSE{
        IF (CAN_ROTATE(RIGHT)) {
        ROTATE_RIGHT
        }
        ELSE {
        ROTATE_LEFT
        }
    }
}

Question 4

See image

Question 5

  • To select the number 69, you must go through binary search. First, you must find the middle value by adding the first(1) and last index(8) value and dividing by 2. Since that value is a decimal(3.5), round up(4), and the element is 6.
  • This is not the intended value, so take each half of the list, before the middle and after the middle and repeat binary search. Repeating this process for the first half finds a middle value of that portion(1+4/2 = 2.5, round to index 3, whose element has a value of 3). For the back half of the list, we repeat this value, with the 6-8th elements, selecting index 7, which has a value of 11.
  • Since the value of 69 is not found, we must repeat the binary search process. Now, the back half only has the endpoints to be checked, indices 6 and 8. Index 6 is 9, which is not the intended value, but index 8 is the intended value of 69.

Question 6

See image

Question 7

An order that can be used for binary search is:

  • ["Market", "Ralphs", "store", "Target", "Walmart"] This is because the strings are in alphabetical order, which would make sense.

Question 8

Binary search is more efficient than sequential search because binary search allows the computer to check exponentially more numbers each search. A binary search algorithm keeps on multiplying the amount of numbers checked by 2 each time. The sequential search checks each element individually, which will probably take a longer time, since the algorithm must go through every item of a list.

Question 9

See image

To find the number 36, it would take 2 checks. The middle value on the first check would be 16, the 3rd index((1+5)/2). Then, we take the parts of the list on each side of the middle element. Before 16 are 64 and 36, while after 16 are 11 and 9. For the first part of the list there are 2 indices, 1 and 2. Using the binary search formula, (1+2)/2 = 1.5, which rounds up to the element at index 2, which is 36, the designated value.

def binary_search(list, value):
    bottom = 0
    top = len(list) - 1
    mid = 0
 
    while bottom <= top:
        mid = (bottom + top) // 2
        if list[mid] < value:
            bottom = mid + 1
        elif list[mid] > value:
            top = mid - 1
        else:
            return mid
    return -1

numbers = [1, 2, 3, 4, 5]
x = 4

test = binary_search(numbers, x)

if test != -1:
    print("Element", str(x), "is present at index", str(test))
else:
    print("Element is not present in array")
Element 4 is present at index 3