IBDP Computer Science Computational Thinking Notes
IBDP Computer Science Computational Thinking Notes
Professor. A. Lawson
Sources: Online Materials, thanks for all
Topic.4:
Problem-solving and programming
1
IB Computer Science
Topic 4 - Overview
Thinking procedurally
4.1.1 Identify the procedure appropriate to solving a problem
4.1.2 Evaluate whether the order in which activities are undertaken will result in the
required outcome
4.1.3 Explain the role of sub-procedures in solving a problem
Thinking logically
4.1.4 Identify when decision-making is required in a specified situation
4.1.5 Identify the decisions required for the solution to a specified problem
4.1.6 Identify the condition associated with a given decision in a specified problem
4.1.7 Explain the relationship between the decisions and conditions of a system
4.1.8 Deduce logical rules for real-world situations
Thinking ahead
4.1.9 Identify the inputs and outputs required in a solution
4.1.10 Identify pre-planning in a suggested problem and solution
4.1.11 Explain the need for pre-conditions when executing an algorithm
4.1.12 Outline the pre- and post-conditions to a specified problem
4.1.13 Identify exceptions that need to be considered in a specified problem solution
Thinking concurrently
4.1.14 Identify the parts of a solution that could be implemented concurrently
4.1.15 Describe how concurrent processing can be used to solve a problem
4.1.16 Evaluate the decision to use concurrent processing in solving a problem
Thinking abstractly
4.1.17 Identify examples of abstraction
4.1.18 Explain why abstraction is required in the derivation of computational solutions
for a specified situation
4.1.19 Construct an abstraction from a specified situation
4.1.20 Distinguish between a real-world entity and its abstraction
2
IB Computer Science
Topic 4.1.1
Identify the procedure appropriate to solving a problem
• Evaluate whether the order in which activities are
undertaken will result in the required outcome.
• Explain the role of sub-procedures in solving a problem.
IB Computer Science
Topic 4.1.2
Evaluate whether the order in
which activities are undertaken
will result in the required outcome
3
Lets Practice by doing some
Java problems
Username:
lawsonsclass
Password:
codechef021
https://www.codechef.com/problems/school http://codingbat.com/java
IB Computer Science
Topic 4.1.3 – 4.1.8
SUB-Procedures
4
Explain the role of sub-procedures
in solving a problem
5
Logic algorithm mathematical games to practice
https://www.mathplayground.com/logicgames.html
7
IB Computer Science
Topic 4.1.20
Distinguish between a
real-world entity and its
abstraction
IB Computer Science
Topic 4.2.1
Describe the characteristics of standard algorithms on linear arrays
The four key standard algorithms:
• Sequential search
• Binary search
• Bubble sort
• Selection sort
IB Computer Science
Topic 4.2.1
Sequential search
• Linear search or sequential search is an algorithm to find an
item in a list.
• It starts at the first element and compares each element to
the one it’s looking for until it finds it.
• Commonly used with collections (which are unsorted lists
of items) and text/csv file reading.
Binary search
https://www.youtube.com/watch?v=D5SrAga1pno
IB Computer Science
Topic 4.2.1
Binary search
• Binary search, also known as half-interval search, is a search algorithm that
finds the position of a target value within a sorted array.
• It works by comparing the target value to the middle element of the array;
• If they are unequal, the lower or upper half of the array is eliminated
depending on the result and the search is repeated in the remaining sub-array
until it is successful.
• It only applies to SORTED arrays (where there are usually no duplicate values,
or duplicates do not matter)
Bubble sort
• Bubble sort is a simple sorting algorithm that
repeatedly steps through the list to be sorted,
compares each pair of adjacent items and swaps
them if they are in the wrong order.
• The pass through the list is repeated until no
swaps are needed, which indicates that the list is
sorted.
• The algorithm, which is a comparison sort, is
named for the way smaller elements "bubble" to
the top of the list.
• Although the algorithm is simple, it is too slow
and impractical for most problem
Selection sort
• Selection sort is a sorting algorithm and it is inefficient on large lists
• Selection sort is noted for its simplicity, and it has performance advantages over
more complicated algorithms in certain situations, particularly where memory is
limited.
• The algorithm divides the input list into two parts: the sublist of items already
sorted, which is built up from left to right at the front (left) of the list, and the sublist
of items remaining to be sorted that occupy the rest of the list.
• Initially, the sorted sublist is empty and the unsorted sublist is the entire input list.
• The algorithm proceeds by finding the smallest (or largest, depending on sorting
order) element in the unsorted sublist, exchanging (swapping) it with the leftmost
unsorted element (putting it in sorted order), and moving the sublist boundaries
one element to the right.
IB Computer Science
Topic 4.2.1
Selection sort
Selection sort (Pseudocode)
A - an array containing the
list of numbers numItems -
the number of numbers in
the list
for i = 0 to numItems - 1 for j
= i+1 to numItems if A[i] >
A[j] // Swap the entries
Temp = A[i] A[i] = A[j] A[j] =
Temp end if end loop
end loop
IB Computer Science
Topic 4.2.2
Outline the standard operations of collections
Collections?
• As far as the IB is concerned, collections are UNORDERED lists
usually of UNKNOWN length or size.
• In practice we usually program collections using LinkedLists in Java.
• This means that we must remember there are a few things that
LinkedLists CAN do that collections CANNOT.
Standard collection operations
.addItem( data ) = add data item to the collection .resetNext() =
start at the beginning .hasNext() → tells whether there is another
item in the list .getNext() → retrieves a data item from the collection
.isEmpty() → check whether collection is empty
IB Computer Science
Topic 4.2.2
Collections (Pseudocode)
NAMES = new Collection()
NAME = ""
loop while NAME <> "quit"
input NAME
if NAME <> "quit" then
if NAMES.contains(NAME) then
output NAME , " returned" Task: This program inputs NAMES of students
NAMES.remove(NAME) who are leaving school early - for example to visit
else the doctor. The names are collected in a
Collection list. When a student returns, tying the
output NAME , " is leaving" same name again removes that name from the
NAMES.addItem(NAME) list. At the end of the day, the secretary types
end if "quit" to end the program and see a list of all
end if students who left but did not return.
end loop
output "The following students left and did not return"
NAMES.resetNext()
loop while NAMES.hasNext()
output NAMES.getNext()
end loop
IB Computer Science
Topic 4.2.3
Discuss an algorithm to solve a specific problem
Basic comparisons
• A binary search is faster - O( log N ), but can only be performed in a SORTED list
• A sequential search is slower - O(N) but can be performed whether the list is sorted or not
• A bubble sort can "quit early" if no swaps are made in a pass. But it makes lots of swaps.
• A selection sort must always perform N passes it cannot "quit early". But it makes fewer
swaps maximum of N swaps
• Both bubble and selection sort are O(n^2) = equally complex
Analyse an algorithm
presented as a flow chart
Analyse an algorithm
presented in pseudocode
Questions in exam pertaining to topic may include the following: variables, calculations,
simple and nested loops, simple conditionals and multiple or nested conditionals.
IB Computer Science
Topic 4.2.6
Construct pseudocode to
represent an algorithm
IB Computer Science
Topic 4.2.7
Suggest suitable algorithms to A Tale of Four Algorithms
solve a specific problem Article by: Virginia Eubanks
• For example:
If PRICES and NAMES and
INVENTORY are parallel arrays, write
an algorithm that finds all the items
where INVENTORY is below 10 items,
and adds 20% to the PRICES of those
items.
Definition: Complexity
• Complexity of an algorithm is a measure of the
amount of time and/or space required by an
algorithm for an input of a given size (n).
• Time for an algorithm to run t(n) is characterised
by the size of the input.
• We usually try and estimate the WORST CASE, and
sometimes the BEST CASE, and very rarely the
AVERAGE CASE.
What do we measure?
• In analysing an algorithm, rather than a piece of
code, we will try and predict the number of times
the principle activity of that algorithm is performed.
• For example, if we are analysing a sorting
algorithm we might count the number of
comparisons performed.
IB Computer Science
Topic 4.2.8
Best vs Worst vs Average case
• Worst Case
– is the maximum run time, over all inputs of size n,
ignoring effects (a) through (d) above. That is, we
only consider the "number of times the principle
activity of that algorithm is performed".
• Best Case
– In this case we look at specific instances of input of
size n. For example, we might get best behaviour
from a sorting algorithm if the input to it is already
sorted.
• Average Case
– Arguably, average case is the most useful measure.
It might be the case that worst case behaviour is
pathological and extremely rare, and that we are
more concerned about how the algorithm runs in
the general case. Unfortunately this is typically a
very difficult thing to measure.
IB Computer Science
Topic 4.3.1
State the fundamental
operations of a computer
All CPUs have sets of instructions, also
called the fundamental operations,
that enable commands to be
processed.
Fundamental vs Compound
An example of fundamental instructions: Examples of compound/complex
instructions:
LOAD register 34AB39 Find the biggest number in an array
ADD 29 Sort the names alphabetically
STORE result
COMPARE result to register 4
IB Computer Science
Topic 4.3.3
Explain the essential features of a
computer language
IB Computer Science
Topic 4.3.4
Explain the need for
higher level languages
Low level vs High level language
IB Computer Science
Topic 4.3.5
Outline the need for a translation process from a
higher level language to machine executable code
IB Computer Science
Topic 4.3.5
Video: Interpreter vs Compiler
https://www.youtube.com/watch?v=_C5AHaS1mOA#t=44
Key terms
• Compiler - If the translator translates a high level language into a lower level language
(done in a batch)
• Interpreter - the translator translates a high level language into an intermediate code
which will be immediately executed by the CPU (done line by line)
• Assembler - the translator translates assembly language to machine code (mnemonics to
binary)
Levels of language
Variable
• Variables are storage location for data in a program.
• They are a way of naming a memory location for later usage (to put a
value into/retrieve a value).
• Each variable has a name and a data type that is determined at its
creation (and cannot be changed).
Constant
• A constant is an identifier with an associated value which cannot be
altered by the program during normal execute -the value is constant.
• This is contrasted with a variable, which is an identifier with a value
that can be changed during normal execution – the value is variable.
Operator
• A character/set of characters that represents an action
• Types: – Boolean operators (AND, OR, &&, ||) for working out
true/false situations – Arithmetic operators (+, -, ++, --, /, %, div, mod)
for doing simple mathematical calculations – Assignment operators ,
which assign a specified value to another value and ( = ) – Relational
operators , which compare two values (<, >, >=, <=, ==, !=, .equals() )
Operator
Symbols:
IB Computer Science
Topic 4.3.6
Object
• In Object-oriented programming (OOP), an object is an instance of a
class.
• Objects are an abstraction: they hold both data (states), and ways to
manipulate the data (behaviours).
IB Computer Science
Topic 4.3.7
Define the operators:
=, ≠, <, <=, >, >=, mod, div
Arithmetic Operators
• +: used for adding two numbers (also used for String
concatenation)
• -: used for subtractions
• *: used for multiplication
• /: used for division
•% or mod: returns the remainder of a division calculation
• div: returns the numbers of times X divides into Y without a
remainder
IB Computer Science
Topic 4.3.7
Unary operators
• ++: Increment operator; increments a value by 1
• i++ is the same as i = i + 1
• --: Decrement operator; decrements a value by 1
• i-- is the same as i = i - 1
IB Computer Science
Topic 4.3.8
Analyse the use of For example, identify and justify the use of a
constant as opposed to a variable in a given
variables, constants and situation. These will depend on the
operators in algorithms algorithm presented in the exam and are
impossible to predict.
• Useful for group of items when you don’t know how many items you’ll be
needing/using (contrast to arrays where the size is set in stone at creation)
• Because the collection is only as big as you need it to be, it is an efficient use
of RAM (memory)
• Can be of any data type (primitive or even your own object)
IB Computer Science
Topic 4.3.11
Construct algorithms using the
access methods of a collection
Important: only use the methods below
A collection is like a linked-list, but the order of elements is not guaranteed so
you can’t use .get(x) or .size() etc.
IB Computer Science
Topic 4.3.12
Discuss the need for sub-
programmes and collections
within programmed
solutions
IB Computer Science
Topic 4.3.12
Advantages to modular design
• Modular programming is an important and
beneficial approach to programming problems.
They make program development easier, and they
can also help with future development projects.
• Key benefits/advantages:
– Usefulness of reusable code
– Eases program organization, both for the
individual programmer, team members
– Makes future maintenance easier – you only
have fix/update a module, not the whole program
Manageable tasks
• Breaking down a programming project into
modules makes it more manageable.
• These individual modules are easier to design,
implement and test.
• Then you can use these modules to construct the
overall program.
Distributed development
• Modular programming allows distributed
development.
• By breaking down the problem into multiple
tasks, different developers can work in parallel.
• And this will shorten the development time.
IB Computer Science
Topic 4.3.12
Code reusability
• A program module can be reused in programs.
• This is a convenient feature because it reduces
redundant code.
• Modules can also be reused in future projects.
• It is much easier to reuse a module than
recreate program logic from scratch.
Program readability
• Modular programming leads to more
readable programs.
• Modules can be implemented as user-defined
functions.
• A program that has plenty of functions is
straightforward.
• But a program with no functions can be very
long and hard to follow.
IB Computer Science
Topic 4.3.13
Construct algorithms:
using predefined sub-programmes, one-
dimensional arrays and/or collections
Averaging an array
Factors
Copying a
collection into an
array in reverse