Course topic 2 exercises

Pseudocode

Consider the pseudocode below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 Mystery1(Array[a1, a2, ... , aN]):
   B := []
   FOR elem IN Array LOOP
     IF first THEN
       B[0] := elem
     ELSEIF elem > LAST(B)
       APPEND(B, elem)
     END IF
   END LOOP
   RETURN  B

What would the Mystery1 algorithm return when its input is [3, 2, 5, 5, 8, 17, 16]

Consider the pseudocode below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 Mystery2(Int A, Int B):
   res := 0
   WHILE B > 0 LOOP
     IF B % 2 = 1 THEN
       res := res + A
     ENDIF
     A := A * 2
     B := B / 2
   END LOOP
   IF res = 0 THEN
     RETURN A
   ELSE
     RETURN res
   ENDIF

What would the Mystery 2 algorithm return with A=4 and B=3 as its input? Division rounds down to an integer.

What does the Mystery2 algorithm compute?

Insertion sort

[JSAV Placeholder: sorting_insertion_sort]