Professional Documents
Culture Documents
General Principles
Thinking Procedurally
Thinking Logically
Thinking Ahead
Thinking Concurrently
Thinking Abstractly
4. Computational thinking, problem-solving and programming 3 / 192
Identification
Identify the problem
Understand the problem
Formulate the problem
Development
Explore various alternatives
Selection
Choose the best alternative
Implementation
Implement the selected solution
4. Computational thinking, problem-solving and programming 5 / 192
Question
Identify the procedure appropriate to solving the following problems:
Bake a cake.
Go to the dining room.
Clean up the classroom tables.
4. Computational thinking, problem-solving and programming 8 / 192
Order of activities
Question 1
For the following problems present an order in which activities could be
undertaken that will lead to the desired result and an order in which
activities could be undertaken that will not lead to the desired result.
Baking a cake
Go to the dining room
Clean up the classroom desks
Question 2
Explain the importance of the order in which activities are performed.
4. Computational thinking, problem-solving and programming 11 / 192
If a solution exists, use it. If you have solved the same or a similar
problem before, just repeat the successful solution.
We usually do not consciously think, “I have seen this before, and I
know what to do” - we just do it.
Humans are good at recognising similar situations.
We do not have to learn how to go to the store and buy milk, then
to buy eggs, then to buy candy. We know that going to the store is
always the same and only what we buy is different.
4. Computational thinking, problem-solving and programming 12 / 192
Algorithm
An algorithm is a series of unambiguous instructions designed in order to
solve a problem and achieve a certain goal in a finite number of steps.
Examples
Representation of Algorithms
IB Pseudocode
http://ibcomp.fis.edu/pseudocode/pcode.html.
4. Computational thinking, problem-solving and programming 19 / 192
Comments
Arithmetical Expressions
Examples
Write an algorithm that takes as input two numbers and adds them.
4. Computational thinking, problem-solving and programming 22 / 192
Examples
Write an algorithm that takes as input two numbers and adds them.
1 A = input ( " Give the first number " )
2 B = input ( " Give the second number " )
3 S = A+B
4 output " The sum is " , S
Write an algorithm that takes as input the initial value of a product and
the value of VAT and computes the final value of the product
4. Computational thinking, problem-solving and programming 22 / 192
Examples
Write an algorithm that takes as input two numbers and adds them.
1 A = input ( " Give the first number " )
2 B = input ( " Give the second number " )
3 S = A+B
4 output " The sum is " , S
Write an algorithm that takes as input the initial value of a product and
the value of VAT and computes the final value of the product
1 I = input ( " Give a the initial value of the product " )
2 V = input ( " Give the VAT " )
3 F = I + V * I /100
4 output " The final value is " , F
4. Computational thinking, problem-solving and programming 23 / 192
Variables
CAUTION!!!!!
Variables in computer science do not behave as variables in mathematics.
X =3
4. Computational thinking, problem-solving and programming 24 / 192
X =5
X = 11
X = 17
Assignment of Variables
Assignment of variables
Assignment of variables
Assignment of variables
Examples
Examples
Interpreter of IB Pseudocode
http://ibcomp.fis.edu/pseudocode/pcode.html
7 mod 3
3 mod 7
5 div 2
2 div 5
15 + 9 div 2
3/2
4. Computational thinking, problem-solving and programming 31 / 192
7 mod 3 1
3 mod 7
5 div 2
2 div 5
15 + 9 div 2
3/2
4. Computational thinking, problem-solving and programming 31 / 192
7 mod 3 1
3 mod 7 3
5 div 2
2 div 5
15 + 9 div 2
3/2
4. Computational thinking, problem-solving and programming 31 / 192
7 mod 3 1
3 mod 7 3
5 div 2 2
2 div 5
15 + 9 div 2
3/2
4. Computational thinking, problem-solving and programming 31 / 192
7 mod 3 1
3 mod 7 3
5 div 2 2
2 div 5 0
15 + 9 div 2
3/2
4. Computational thinking, problem-solving and programming 31 / 192
7 mod 3 1
3 mod 7 3
5 div 2 2
2 div 5 0
15 + 9 div 2 19
3/2
4. Computational thinking, problem-solving and programming 31 / 192
7 mod 3 1
3 mod 7 3
5 div 2 2
2 div 5 0
15 + 9 div 2 19
3/2 1.5
4. Computational thinking, problem-solving and programming 32 / 192
Examples
Examples
What value could you give to X such that 3 is printed? (actually there
are infinite values . . . )
1 input X
2 X = X + 2
3 Y = X mod 4
4 output Y
What value could you give to A such that 2 is printed? (there are 3
positive integer values. Try and find them)
1 input A
2 A = A - 3
3 B = A div 3
4 output B
4. Computational thinking, problem-solving and programming 34 / 192
Conditions
Write a program that takes two numbers as input, A and B and print the
smallest of them. (If the numbers are equal the smallest is any of the
numbers).
4. Computational thinking, problem-solving and programming 35 / 192
Write a program that takes two numbers as input, A and B and print the
smallest of them. (If the numbers are equal the smallest is any of the
numbers).
1 input A
2 input B
3 if A < B then
4 output A
5 else
6 output B
7 end if
4. Computational thinking, problem-solving and programming 36 / 192
Example
Example
Example
Write a program that takes a number X as input and does the following.
If X ∈ [0, 5] it prints the message “Valid Number”
If X ∈
/ [0, 5] it prints the message “Not Valid Number”
4. Computational thinking, problem-solving and programming 37 / 192
Example
Write a program that takes a number X as input and does the following.
If X ∈ [0, 5] it prints the message “Valid Number”
If X ∈
/ [0, 5] it prints the message “Not Valid Number”
1 input X
2 if 0 <= X AND X <=5 then
3 output " Valid Number "
4 else
5 output " Not Valid Number "
6 end if
4. Computational thinking, problem-solving and programming 38 / 192
Example
Write a program that takes a number X as input and does the following.
If X is a multiple of 5 it prints the message “Valid Number”
If X is not a multiple of 5 it prints the message “Not Valid Number”
4. Computational thinking, problem-solving and programming 38 / 192
Example
Write a program that takes a number X as input and does the following.
If X is a multiple of 5 it prints the message “Valid Number”
If X is not a multiple of 5 it prints the message “Not Valid Number”
1 input X
2 if X mod 5 = 0 then
3 output " valid number "
4 else
5 output " Not valid number "
6 end if
4. Computational thinking, problem-solving and programming 39 / 192
Example
Write a program that takes a number X as input and does the following.
If X is a multiple of 5 and a multiple of 3 it prints the message
“Valid Number”
If X is not a multiple of 5 or not a multiple of 3 it prints the
message “Not Valid Number”
4. Computational thinking, problem-solving and programming 39 / 192
Example
Write a program that takes a number X as input and does the following.
If X is a multiple of 5 and a multiple of 3 it prints the message
“Valid Number”
If X is not a multiple of 5 or not a multiple of 3 it prints the
message “Not Valid Number”
1 input X
2 if X mod 15 = 0 then
3 output " valid number "
4 else
5 output " not valid number "
6 end if
4. Computational thinking, problem-solving and programming 40 / 192
Example
The if-then-else command can be used without the else part. In this case,
if the condition is true it executes the code in the if part. If the condition
is false nothing happens.
4. Computational thinking, problem-solving and programming 40 / 192
Example
The if-then-else command can be used without the else part. In this case,
if the condition is true it executes the code in the if part. If the condition
is false nothing happens.
Write an algorithm in IB Pseudocode that takes as input the grades of a
student in Mathematics, Physics and Computer Science and prints the
average. If the average is at least 4 it prints “You have passed”.
4. Computational thinking, problem-solving and programming 40 / 192
Example
The if-then-else command can be used without the else part. In this case,
if the condition is true it executes the code in the if part. If the condition
is false nothing happens.
Write an algorithm in IB Pseudocode that takes as input the grades of a
student in Mathematics, Physics and Computer Science and prints the
average. If the average is at least 4 it prints “You have passed”.
1 input A
2 input B
3 input C
4 D = ( A + B + C ) /3
5 output D
6 if D >= 4 then
7 output " You have passed "
8 end if
4. Computational thinking, problem-solving and programming 41 / 192
Example
Example
Example
Example
Example
Example
Example
Nested ifs
Nested ifs
Nested ifs
The most interesting case is when the if follows the else of an earlier if.
A long jump athlete has achieved the scores A, B and C in meters. Write an algorithm that reads
A, B and C and does the following:
computes the average of A, B and C , call it AVG , and prints it
if AVG ≥ 8 it prints “You have passed”
if 7.5 ≤ AVG < 8 it prints “You failed but you were close”
if AVG < 7.5 it prints “You failed :(”
1 input A
2 input B
3 input C
4 AVG = ( A + B + C ) /3
5 if AVG >= 8 then
6 output " You have passed "
7 else
8 if AVG >= 7.5 then
9 output " You failed but you were close "
10 else
11 output " You failed :( "
12 end if
13 end if
4. Computational thinking, problem-solving and programming 52 / 192
Example
1 input A
2 if A > 11 then
3 B = A div 3
4 B = 2 * B // command 1
5 else
6 if A > 3 then
7 B = A * 2
8 B = B mod 3 // command 2
9 else
10 B = A * A
11 B = B div 2 // command 3
12 end if
13 end if
14 output B
1 Which command will be executed and what will be the output if the
input for A is 11?
2 Which command will be executed and what will be the output if the
input for A is 3?
3 Find two values for A such that command 2 will be executed.
4 Find two values for A such that command 3 will not be executed.
4. Computational thinking, problem-solving and programming 53 / 192
Example
A store puts discounts on some of its products according to the following
table.
Value Discount
. . . ≤ 200 10%
200 < . . . ≤ 300 20%
300 < . . . 30%
Write an algorithm that takes as input the original value of a product and
computes its final value (after the discount) using the above table.
1 input V
2 if V <= 200 then
3 V = V - V /10
4 else
5 if V > 300 then
6 V = V - V * 0.3
7 else
8 V = V - V * 0.2
9 end if
10 end if
11 output V
4. Computational thinking, problem-solving and programming 54 / 192
Iterations
Assume that we want to print all the numbers from 1 to 10. We could
write the following code
1 output 1
2 output 2
3 output 3
4 output 4
5 output 5
6 output 6
7 output 7
8 output 8
9 output 9
10 output 10
4. Computational thinking, problem-solving and programming 54 / 192
Iterations
Assume that we want to print all the numbers from 1 to 10. We could
write the following code
1 output 1
2 output 2
3 output 3
4 output 4
5 output 5
6 output 6
7 output 7
8 output 8
9 output 9
10 output 10
Does this look practical?
4. Computational thinking, problem-solving and programming 55 / 192
Loops
Loops
The following two algorithms are equivalent.
I = 1
output I
I = 2
output I
I = 3
output I
I = 4
output I
I = 5
loop I from 1 to 10
output I
output I
I = 6
end loop
output I
I = 7
output I
I = 8
output I
I = 9
output I
I = 10
output I
4. Computational thinking, problem-solving and programming 57 / 192
Example
Write an algorithm that prints all the multiples of 5 that are less or equal
to 1000.
1 loop I from 0 to 1000
2 if I mod 5 = 0 then
3 output I
4 end if
5 end loop
4. Computational thinking, problem-solving and programming 58 / 192
Example
Example
Using a loop write an algorithm that computes the sum of the numbers
from 1 to 1000.
SUM = 0
loop I from 1 to 1000
SUM = SUM + I
end loop
output SUM
Example
Using a loop write an algorithm that computes the product of all the
numbers from 1 to 1000.
PRODUCT = 1
loop N from 1 to 1000
PRODUCT = PRODUCT * N
end loop
output PRODUCT
Example
Write an algorithm that takes a number N as input and prints all the
factors of N.
For example if the input is 15 the algorithm should print 1, 3, 5 and 15.
1 input N
2 loop X from 1 to N
3 if N mod X = 0 then // we check whether X is a factor of N
4 output X
5 end if
6 end loop
4. Computational thinking, problem-solving and programming 62 / 192
Boolean Values
Except for numerical values (i.e. numbers), variables may also take
boolean values.
What will the following algorithm print if X = 18? If X = 115?
1 input X
2 A = false
3 if X mod 2 = 0 then
4 A = true
5 end if
6 if A = true then
7 output " The number you gave is even "
8 else
9 output " The number you gave is odd "
10 end if
The purpose of boolean values is to check whether something holds or
not. Of course, as you know from topic 2, many people prefer using 1
instead of true and 0 instead of false.
4. Computational thinking, problem-solving and programming 63 / 192
Example
Example
Write an algorithm that takes A and B as input and outputs the solutions
(if any) of the equation A · x + B = 0. Make sure that your algorithm
provides the correct output for all the possible values of A and B.
1 input A
2 input B
3 if A = 0 then
4 if B = 0 then
5 output " Infinite solutions "
6 else
7 output " No solutions "
8 end if
9 else
10 X = - B /A
11 output " The only solution is " , X
12 end if
4. Computational thinking, problem-solving and programming 65 / 192
Example
1 input A
2 input B
3 input C
4 if A = 0 then // we solve B * X + C = 0
5 if B = 0 then
6 if C = 0 then
7 output " Infinite solutions "
8 else
9 output " No solutions "
10 end if
11 else
12 X = - C /B
13 output " The only solution is " , X
14 end if
15 else // we truly have a second degree equation
16 D = B ^2 - 4 * A * C
17 if D > = 0 then √
18 X1 = ( - B + √D ) /(2 * A )
19 X2 = ( - B - D ) /(2 * A )
20 output " The solutions are " , X1 , X2
21 else
22 output " No real solutions "
23 end if
24 end if
4. Computational thinking, problem-solving and programming 67 / 192
Example
Example
Write an algorithm that prints the sum of the factors of a given number
N. For example if N = 6, the algorithm should print 12, since
12 = 6 + 3 + 2 + 1.
1 input N
2 SUM = 0
3 loop I from 1 to N
4 if N mod I = 0 then
5 SUM = SUM + I
6 end if
7 end loop
8 output " The sum is " , SUM
4. Computational thinking, problem-solving and programming 69 / 192
Trace the following algorithmic fragment for N=6. Show all working in a
trace table.
SUM = 0
loop COUNT from 1 to ( N div 2)
if N mod COUNT = 0 then
SUM = SUM + COUNT COUNT SUM N mod COUNT = 0 output
0
end if
1 1 TRUE
end loop 2 3 TRUE
if SUM = N then 3 6 TRUE
output " perfect " perfect
else
output " not perfect "
end if
4. Computational thinking, problem-solving and programming 70 / 192
Trace the following algorithmic fragment for N=5. Show all working in a
trace table.
PRODUCT = 1
loop I FROM 1 to N
if I mod 2 = 0 then
PRODUCT = PRODUCT * I
else
PRODUCT = PRODUCT - I
end if
end loop
output PRODUCT
4. Computational thinking, problem-solving and programming 72 / 192
X <= 5 X output
TRUE 1 1
TRUE 2 2
TRUE 3 3
TRUE 4 4
TRUE 5 5
FALSE 6
4. Computational thinking, problem-solving and programming 73 / 192
In the while loop the condition is checked every time before entering the
loop. When the condition is not met any more the loop stops.
After the following algorithm what is the value of X ?
1 X = 1
2 loop while X <= 5
3 output X
4 X = X +1
5 end loop
4. Computational thinking, problem-solving and programming 73 / 192
In the while loop the condition is checked every time before entering the
loop. When the condition is not met any more the loop stops.
After the following algorithm what is the value of X ?
1 X = 1
2 loop while X <= 5
3 output X
4 X = X +1
5 end loop
After the while loop is executed it holds that X = 6
4. Computational thinking, problem-solving and programming 74 / 192
Loops
1 X = 1
2 loop while X > 0
3 X = X + 1
4 end loop
4. Computational thinking, problem-solving and programming 76 / 192
It is possible that while and for loops may not even start!
1 X = 0
2 loop while X > 1
3 X = X + 1
4 end loop
1 loop X from 5 to 1
2 output X
3 end loop
4. Computational thinking, problem-solving and programming 77 / 192
Example
Example
Example
Use a while loop in order to write an algorithm that prints all the
multiples of 7 that are less than or equal to 10000.
1st solution
1 I = 0
2 loop while I <= 10000
3 if I mod 7 = 0 then
4 output I
5 end if
6 I = I + 1
7 end loop
2nd solution
1 X =0
2 loop while X <= 10000
3 output X
4 X = X + 7
5 end loop
4. Computational thinking, problem-solving and programming 81 / 192
Complexity of Algorithms
Question
Should we care about the complexity of the algorithms?
4. Computational thinking, problem-solving and programming 82 / 192
Question
Should we care about the complexity of the algorithms?
Trace the following fragment for N=139 and L=3, by copying and
completing the trace table given below.
D = N div L
Z = 1
B = false
loop while Z < L D Z B Z <L output
D = D div L 46 1 false
Z = Z + 1 15 1 true true
B = NOT B
end loop 5 2 false true
if ( D 6= 0 AND B ) then 3 false
output (D , B ) 3, true
else
output (Z , NOT B )
end if
4. Computational thinking, problem-solving and programming 84 / 192
Arrays
An array is a way of storing related data in a row. Data could be
numbers, strings, characters, etc.
In an array data can be identified using a unique number called index.
The length of the array is the number of data stored in the array.
The indices of the array start 0 and go up to the length of the array
minus one.
The following is an example of an array of length 5 that contains integers.
index 0 1 2 3 4
data -11 53 137 8 1234
Arrays
Array
Array
Arrays in IB Pseudocode
Index 0 1 2 3 4
Data -11 53 137 8 1234
Write an algorithm that prints all the elements of the above array Scores.
1 loop I from 0 to 4
2 output Scores [ I ]
3 end loop
which is equivalent to
1 output Scores [0]
2 output Scores [1]
3 output Scores [2]
4 output Scores [3]
5 output Scores [4]
4. Computational thinking, problem-solving and programming 88 / 192
Arrays in IB Pseudocode
Write an algorithm that prints the sum of the elements of the above
array Scores.
1 SUM = 0
2 loop I from 0 to 4
3 SUM = SUM + Scores [ I ]
4 end loop
5 output " The sum of the elements is " , sum
4. Computational thinking, problem-solving and programming 89 / 192
Arrays in IB Pseudocode
Arrays in IB Pseudocode
Arrays in IB Pseudocode
25 mod 10 = 5
4. Computational thinking, problem-solving and programming 92 / 192
25 mod 10 = 5
123 mod 10 = 3
4. Computational thinking, problem-solving and programming 92 / 192
25 mod 10 = 5
123 mod 10 = 3
2342356 mod 10 = 6
4. Computational thinking, problem-solving and programming 92 / 192
25 mod 10 = 5
123 mod 10 = 3
2342356 mod 10 = 6
Hence X mod 10 is equal to the last digit of X.
4. Computational thinking, problem-solving and programming 92 / 192
25 mod 10 = 5
123 mod 10 = 3
2342356 mod 10 = 6
Hence X mod 10 is equal to the last digit of X.
25 div 10 = 2
4. Computational thinking, problem-solving and programming 92 / 192
25 mod 10 = 5
123 mod 10 = 3
2342356 mod 10 = 6
Hence X mod 10 is equal to the last digit of X.
25 div 10 = 2
123 div 10 = 12
4. Computational thinking, problem-solving and programming 92 / 192
25 mod 10 = 5
123 mod 10 = 3
2342356 mod 10 = 6
Hence X mod 10 is equal to the last digit of X.
25 div 10 = 2
123 div 10 = 12
456 div 10 = 45
4. Computational thinking, problem-solving and programming 92 / 192
25 mod 10 = 5
123 mod 10 = 3
2342356 mod 10 = 6
Hence X mod 10 is equal to the last digit of X.
25 div 10 = 2
123 div 10 = 12
456 div 10 = 45
2342356 div 10 = 234235
4. Computational thinking, problem-solving and programming 92 / 192
25 mod 10 = 5
123 mod 10 = 3
2342356 mod 10 = 6
Hence X mod 10 is equal to the last digit of X.
25 div 10 = 2
123 div 10 = 12
456 div 10 = 45
2342356 div 10 = 234235
Hence X div 10 discards the last digit of X.
4. Computational thinking, problem-solving and programming 93 / 192
Example
Example
Assume that the grades of N students in Mathematics are stored in array
called SCORES. The grades are integers from 0 to 100. A student has
passed if he/she has scored at least 50. Construct an algorithm that
Prints the number of students that have passed
Prints the average grade of the students that have passed.
1 PASSED = 0
2 SUM = 0
3 loop I from 0 to N -1
4 if SCORES [ I ] >= 50 then
5 PASSED = PASSED + 1
6 SUM = SUM + SCORES [ I ]
7 end if
8 end loop
9 if PASSED > 0 then
10 output PASSED , " students have passed "
11 AVG = SUM / PASSED
12 output " The average of the students that passed is " , AVG
13 else
14 output " No students passed "
15 end if
4. Computational thinking, problem-solving and programming 95 / 192
K N M K < 5? output
1 1 2 true 1, 2
2 3 4 true 3, 4
3 5 8 true 5,8
4 7 16 true 7 , 16
5 9 32 false
4. Computational thinking, problem-solving and programming 96 / 192
1 EVEN = true
2 loop while ( N > 0) and ( EVEN = true )
3 if ( N mod 10) mod 2 = 1 then
4 EVEN = false
5 end if
6 N = N div 10 // we forget the last digit of N
7 end loop
8 output EVEN
Example
Example
Example
Example
Example
Solve exercise 10 from past paper 1 (SL) from Friday 2 November 2018
(except
a 26
b
1 input BMI
2 if BMI < 18.5 then
3 output " underweight "
4 else
5 if BMI < 25.0 then
6 output " normal weight "
7 else
8 if BMI < 30.0 then
9 output " overweight "
10 else
11 output " obese "
12 end if
13 end if
14 end if
4. Computational thinking, problem-solving and programming 107 / 192
d
1 // we calculate the average BMI of the group
2 SUM = 0
3 loop I from 0 to 29
4 BMI = calcBMI ( HEIGHT [ I ] , WEIGHT [ I ])
5 SUM = SUM + BMI
6 end loop
7 AVG = SUM /30
8 // we print the names of the people in the group
9 // who have greater BMI than the average
10 loop I from 0 to 29
11 BMI = calcBMI ( HEIGHT [ I ] , WEIGHT [ I ])
12 if ( BMI > AVG ) then
13 output NAME [ I ]
14 end if
15 end loop
4. Computational thinking, problem-solving and programming 108 / 192
Solve exercise 10 from past paper 1 (SL) from Friday 4 November 2016
(except part (d)).
b
1 input LOCKER
2 if LOCKER mod 4 = 0 then
3 output " blue "
4 end if
5 if LOCKER mod 4 = 1 then
6 output " red "
7 end if
8 if LOCKER mod 4 = 2 then
9 output " white "
10 end if
11 if LOCKER mod 4 = 3 then
12 output " yellow "
13 end if
4. Computational thinking, problem-solving and programming 109 / 192
c
1 COUNT = 0
2 loop I from 0 to 2399
3 if PAINTED [ I ] = true then
4 COUNT = COUNT + 1
5 end if
6 end loop
7 output " The number of lockers that have been painted so
far
8 is " , COUNT
4. Computational thinking, problem-solving and programming 110 / 192
Algorithm Flowcharts
input or output
condition check
calculation
4. Computational thinking, problem-solving and programming 112 / 192
Answer
Notation
then A.length = 7
4. Computational thinking, problem-solving and programming 116 / 192
Remark
In the optimized version of linear search the algorithm stops exactly when
the first occurrence of X is found.
4. Computational thinking, problem-solving and programming 118 / 192
Array Bounds
Then A.length = 5 and the indices of A’s elements start at 0 and end at
4. Writing A[5], A[7] or A[-1] is wrong and can produce an error!
And in general we cannot write A[A.length] or A[A.length + 5]. So it is
good idea to verify that I ≥ 0 and I < A.length before writing A[I ].
4. Computational thinking, problem-solving and programming 120 / 192
Arrays bounds
The following pieces of code are wrong and will produce errors!
1 I = 0
2 loop while ( I <= A . length )
3 output A [ I ]
4 I = I +1
5 end loop
1 loop I from 0 to A . length
2 output A [ I ]
3 end loop
4. Computational thinking, problem-solving and programming 121 / 192
Binary Search
Binary Search
1 input X
2 FOUND = false
3 START = 0
4 END = A . length - 1
5 loop while ( START <= END ) AND ( NOT FOUND )
6 MID = ( START + END ) div 2
7 if X = A [ MID ] then
8 FOUND = true
9 else if X < A [ MID ] then
10 END = MID - 1
11 else
12 START = MID + 1
13 end if
14 end loop
15 output FOUND
4. Computational thinking, problem-solving and programming 124 / 192
Linear search can be used to find the position where an element occurs in
a given array.
1 input X
2 FOUND = false
3 loop I from 0 to A . length -1
4 if X = A [ I ] then
5 FOUND = true
6 output X , " was found at position " , I
7 end if
8 end loop
9 if NOT found then
10 output X , " was not found "
11 end if
4. Computational thinking, problem-solving and programming 126 / 192
Trace the following algorithm for A the following array and for X = 3.
3 4 3 5 3
1 input X
2 FOUND = false
3 loop I from 0 to A . length -1
4 if X = A [ I ] then
5 FOUND = true
6 output X , " was found at position " , I
7 end if
8 end loop
9 if NOT found then
10 output X , " was not found "
11 end if
I X = A[I]? FOUND output
false
0 true true 3 was found at position 0
1 false
2 true true 3 was found at position 2
3 false
4 true true 3 was found at position 4
4. Computational thinking, problem-solving and programming 127 / 192
Binary Search
Binary Search is the algorithm where given a sorted array A an
element X we search whether X occurs in A as follows:
1 Let M be A’s middle element.
2 If X = M then we have succeeded.
3 If X > M then we drop the first half of A (i.e. those elements that
are smaller than X )
4 If X < M then we drop the second half of A (i.e. those elements
that are smaller than X )
5 We repeat from the 1-st step with the new array until either X is
found or the array is exhausted.
In every step of the binary search algorithm we drop half of the
array’s element.
So, each time we divide the number of elements that are going to be
searched by 2.
Therefore the total number of steps performed by binary search is
proportional to log2 N, where N is the number of A’s elements.
Hence, we say that the running time of binary search is logarithmic.
Binary search can only be applied on sorted arrays.
4. Computational thinking, problem-solving and programming 129 / 192
1 input X
2 FOUND = false
3 START = 0
4 END = A . length - 1
5 loop while ( START <= END ) AND ( NOT FOUND )
6 MID = ( START + END ) div 2
7 if X = A [ MID ] then
8 FOUND = true
9 else if X < A [ MID ] then
10 END = MID - 1
11 else
12 START = MID + 1
13 end if
14 end loop
15 output FOUND
4. Computational thinking, problem-solving and programming 130 / 192
Until now we’ve seen arrays that contain only numbers. However, arrays
may contains strings as well.
When we say that an array of strings is sorted we mean that the strings
in the array are sorted alphabetically (or lexicographically).
For example the following array of Strings is sorted lexicographically.
(START<=END) AND (NOT FOUND) MID X = A[MID]? FOUND X < A[MID]? END START
false 6 0
true 3 false false 4
true 5 false false 6
true 6 false true 5
false
4. Computational thinking, problem-solving and programming 134 / 192
Sorting Algorithms
Bubble Sort
1st pass
1st pass
4 5 2 1 3 0 -1
4. Computational thinking, problem-solving and programming 136 / 192
1st pass
4 5 2 1 3 0 -1
4 2 5 1 3 0 -1
4. Computational thinking, problem-solving and programming 136 / 192
1st pass
4 5 2 1 3 0 -1
4 2 5 1 3 0 -1
4 2 1 5 3 0 -1
4. Computational thinking, problem-solving and programming 136 / 192
1st pass
4 5 2 1 3 0 -1
4 2 5 1 3 0 -1
4 2 1 5 3 0 -1
4 2 1 3 5 0 -1
4. Computational thinking, problem-solving and programming 136 / 192
1st pass
4 5 2 1 3 0 -1
4 2 5 1 3 0 -1
4 2 1 5 3 0 -1
4 2 1 3 5 0 -1
4 2 1 3 0 5 -1
4. Computational thinking, problem-solving and programming 136 / 192
1st pass
4 5 2 1 3 0 -1
4 2 5 1 3 0 -1
4 2 1 5 3 0 -1
4 2 1 3 5 0 -1
4 2 1 3 0 5 -1
4 2 1 3 0 -1 5
4. Computational thinking, problem-solving and programming 136 / 192
1st pass
4 5 2 1 3 0 -1
4 2 5 1 3 0 -1
4 2 1 5 3 0 -1
4 2 1 3 5 0 -1
4 2 1 3 0 5 -1
4 2 1 3 0 -1 5
After the first pass the largest element of the array is moved to the last
position of the array (in this case it is 5).
4. Computational thinking, problem-solving and programming 137 / 192
2nd pass
4 2 1 3 0 -1 5
4. Computational thinking, problem-solving and programming 137 / 192
2nd pass
4 2 1 3 0 -1 5
After the second pass the second largest element of the array is moved to
the last position of the array (in this case it is 4).
4. Computational thinking, problem-solving and programming 138 / 192
Then the 1st pass will simply move the 4 before 8, 9 and 10.
4. Computational thinking, problem-solving and programming 139 / 192
How many steps does bubble sort take when the array is already
sorted?
it takes only one pass, which lasts n − 1 steps
How many steps does bubble sort take when the array is sorted in
reverse (i.e. descending) order? it takes n − 1 passes, which together
last (n − 1) × (n − 1) steps
What is the best case performance of bubble sort? For which kind of
input does it take place?
What is the worst case performance of bubble sort? For which kind f
input does it take place?
4. Computational thinking, problem-solving and programming 141 / 192
Swapping variables
Swapping variables
Swapping variables
Selection Sort
1 Anarray is called linear if it has only 1 dimension. All the arrays you see in the SL
are linear.
4. Computational thinking, problem-solving and programming 147 / 192
5 4 2 -1 0 3
-1 4 2 5 0 3
-1 0 2 5 4 3
-1 0 2 5 4 3
-1 0 2 3 4 5
4. Computational thinking, problem-solving and programming 148 / 192
Selection
Unsorted
8 2 -2 0 3 4
Sorted Unsorted
-2 2 8 0 3 4
Sorted Unsorted
-2 0 8 2 3 4
Sorted Unsorted
-2 0 2 8 3 4
Sorted Unsorted
-2 0 2 3 8 4
Sorted
-2 0 2 3 4 8
4. Computational thinking, problem-solving and programming 150 / 192
1 min = A [0]
2 loop I from 1 to A . length - 1
3 if A [ I ] < min then
4 min = A [ I ]
5 end if
6 end loop
7 output min
1 min = 0
2 loop I from 1 to A . length - 1
3 if A [ I ] < min then
4 min = I
5 end if
6 end loop
7 output A [ min ]
How many comparisons does it take?
4. Computational thinking, problem-solving and programming 151 / 192
1 TEMP = A [ i ]
2 A[i] = A[j]
3 A [ j ] = TEMP
4. Computational thinking, problem-solving and programming 152 / 192
1 loop I from 0 to A . length - 2
2 MIN = I
3 loop J from I +1 to A . length -1
4 if A [ J ] < A [ I ] then
5 MIN = J
6 end if
7 end loop
8 TEMP = A [ I ]
9 A [ I ] = A [ MIN ]
10 A [ MIN ] = TEMP
11 end loop
Lines 2 to 7 find the minimum element among the elements A[I] to
A[A.length-1].
Lines 8 to 10 swaps elements A[I] and A[MIN].
4. Computational thinking, problem-solving and programming 153 / 192
Big O notation
You should use selection sort when you want to make fewer swaps.
You should use bubble sort when you hope that the array is almost
sorted, so you can finish faster.
You should use selection sort when you want that the smallest
elements appear in the beginning of the array very fast.
You should use bubble sort when you want that the largest elements
appear at the end of the array very fast.
However nowadays both algorithms are considered extremely
inefficient and are almost never used in practice.
4. Computational thinking, problem-solving and programming 158 / 192
Array Minimum
Algorithm Flowcharts
input or output
condition check
(if N > 0 then ... else)
calculation
4. Computational thinking, problem-solving and programming 163 / 192
For example assume that we have a printer named P. This printer could
be understood as an object and could have the following methods.
print() this method makes the printer print
hasInk() this method checks whether the printer has ink
hasPaper() this method checks whether the printer has paper
The parentheses after the method denote that it takes no arguments.
We can force the printer to print, with the following command: P.print().
We can check whether P has ink by writing P.hasInk() = true.
4. Computational thinking, problem-solving and programming 171 / 192
The methods of the printer P are called using the . and the name of the
method.
The following algorithm checks whether the printer has ink and paper
and then asks the printer to print.
1 if P . hasInk () = true AND P . hasPaper () = true then
2 P . print ()
3 end if
4. Computational thinking, problem-solving and programming 171 / 192
The methods of the printer P are called using the . and the name of the
method.
The following algorithm checks whether the printer has ink and paper
and then asks the printer to print.
1 if P . hasInk () = true AND P . hasPaper () = true then
2 P . print ()
3 end if
Since the methods P.hasInk() and P.hasPaper() return boolean values
(that is true or false) we could equivalently write the following
1 if P . hasInk () AND P . hasPaper () then
2 P . print ()
3 end if
The printer could also have an attribute color, which is accessed as
P.color.
4. Computational thinking, problem-solving and programming 172 / 192
Data Structure
A data structure is a way of organizing (i.e. storing) data (i.e.
information).
In a data structure, data are organized in a form such that several factors
are optimized: e.g. search time, insertion/deletion time, storage space,
etc.
Examples of data structures are arrays and collections
4. Computational thinking, problem-solving and programming 176 / 192
Usually dynamic data structures are used in order to save space and
optimize search algorithms.
Static data structures are typically used in order to have fast (random)
access of elements and simplify implementation.
4. Computational thinking, problem-solving and programming 177 / 192
Collection
The following code reads 20 integers from input and puts them in a
collection named C .
4. Computational thinking, problem-solving and programming 179 / 192
The following code reads 20 integers from input and puts them in a
collection named C .
1 loop I from 1 to 20
2 input X
3 C . addItem ( X )
4 end loop
4. Computational thinking, problem-solving and programming 180 / 192
Copy to Collection
Copy to Collection
Search in a Collection
Let B be a collection.
Write an algorithm that searches for element X in collection B.
4. Computational thinking, problem-solving and programming 182 / 192
Search in a Collection
Let B be a collection.
Write an algorithm that searches for element X in collection B.
1 found = false
2 B . resetNext ()
3 loop while B . hasNext ()
4 if X = B . getNext () then
5 found = true
6 end if
7 end loop
8 if found then
9 output X , " was found "
10 else
11 output X , " was not found "
12 end if
4. Computational thinking, problem-solving and programming 183 / 192
Search in a Collection
Search in a Collection
Search in a Collection
Search in a Collection
Example
Write a method count that takes a collection as input and returns the
number of elements in the collection.
4. Computational thinking, problem-solving and programming 186 / 192
Example
The of students who play football are stored in an array F. The names of
students who play basketball are stored in a collection named B.
1 Write a method S that takes as input a name N and an array A and
returns true if S appears in A and false otherwise.
2 Use the method from the previous subquestion to count the number
of students that play both football and basketball.
4. Computational thinking, problem-solving and programming 187 / 192
Example
Programming
Programming/Computer Language