0% found this document useful (0 votes)
5 views

Topic 4.2.1 - Describe the characteristics of standard algorithms on linear arrays

The document covers computational thinking, problem-solving, and programming concepts in IB Computer Science, focusing on standard algorithms and data structures like linear arrays, parallel arrays, and arrays of objects. It details characteristics of variables, one-dimensional and two-dimensional arrays, and introduces key algorithms such as sequential search, binary search, bubble sort, and selection sort. Pseudocode examples are provided for practical understanding of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Topic 4.2.1 - Describe the characteristics of standard algorithms on linear arrays

The document covers computational thinking, problem-solving, and programming concepts in IB Computer Science, focusing on standard algorithms and data structures like linear arrays, parallel arrays, and arrays of objects. It details characteristics of variables, one-dimensional and two-dimensional arrays, and introduces key algorithms such as sequential search, binary search, bubble sort, and selection sort. Pseudocode examples are provided for practical understanding of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Topic 4

Computational thinking, problem-


solving and programming

IB Computer Science
Topic 4.2

Connecting computational
thinking and program design
IB Computer Science
Topic 4.2: 4.2.1 - 4.2.9

Connecting computational
thinking and program design
IB Computer Science
Topic 4: 4.2.1

Describe the characteristics of


standard algorithms
on linear arrays.
Topic 4: 4.2.1
Variables
In computer science, a variable acts as a storage location that
can be used to store a value.
Each variable has a name (or identifier) used to refer to the
stored value.
The value of the variable can be changed during program
execution.
Each variable can store a particular type of data like strings,
reals, boolean and integers.
Topic 4: 4.2.1
One dimensional arrays or linear arrays
An array can hold multiple data elements of the same type
(integers, strings, boo/ean etc.). An array has
● a name,
● a size that cannot be changed during program execution (in
most cases is a static data structure) and
● a data type that describes the type of data that it can store.
A one-dimensional array is a type of linear array.
Topic 4: 4.2.1
One dimensional arrays or linear arrays
In computer science, a variable acts as a storage location that
can be used to store a value.
Each variable has a name (or identifier) used to refer to the
stored value.
The value of the variable can be changed during program
execution.
Each variable can store a particular type of data like strings,
reals, boolean and integers.
Topic 4: 4.2.1
One dimensional arrays or linear arrays
This is an array size 10 (it starts with value 0):
Topic 4: 4.2.1
One dimensional arrays or linear arrays
Build a Pseudocode program that will fill the array with values
from 1 to 10 and then print the values in this format: “Array
position X contains the value Y”

Hint: what’s the first value of the array’s index? What’s the first
number in the array?
To create an array use: new Array ()
Topic 4: 4.2.1
One dimensional arrays or linear arrays
A = new Array ()
N=0
loop N from 0 to 9
A[N] = N+1
end loop
loop N from 0 to 9
output " Array position ", N, “ contains the value", A[N]
end loop
Topic 4: 4.2.1
Parallel arrays
Parallel arrays are extremely useful when a programmer wants
to store different properties of an entity (fields of a record). All
elements of an array should be of the same data type. So, if a
programmer wants to store different data types of an entity
(e.g. student) parallel arrays offer a convenient solution. The
data are organized as a table. Each row represents a particular
student and all columns are of the same data type.
Topic 4: 4.2.1
Example To list all the students:
NAMES = ["May", "Eri", "Elen", "Rit", "Rato", ????????
"More", "Epi","Ent","Ronal", “Bib”]
GRADES = [99, 55, 77, 45, 89, 98, 76, 45, 33, 75]
MIN = GRADES[0] To find the worst grade:
MAX = GRADES[0] ????????
M=0
BEST = “0”
WORST = “0” To find the best grade:
????????
Topic 4: 4.2.1
Example To list all the students:
loop M from 0 to9
NAMES = ["May", "Eri", "Elen", "Rit", "Rato", output "No", M+1, " Student “, NAMES[M],
"More", "Epi","Ent","Ronal", “Bib”] “Mark ", GRADES[M]
GRADES = [99, 55, 77, 45, 89, 98, 76, 45, 33, 75] end loop
MIN = GRADES[0]
MAX = GRADES[0]
M=0 To find the best grade: To find the worst grade:
BEST = “0” loop M from 0 to 9 loop M from 0 to 9
WORST = “0” if MAX <= GRADES [M] then if MIN >= GRADES[M] then
MAX = GRADES[M] MIN = GRADES[M]
BEST = NAMES[M] WORST = NAMES[M]
end if end if
end loop end loop
Topic 4: 4.2.1
Arrays of objects
An array of objects is an array of reference variables. Each
reference variable is an element of the array and it’s a reference
to an object.
Topic 4: 4.2.1
Arrays of objects
Suppose a programmer wants to create an array of vehicle
objects named A.
Each vehicle object has the following data fields: Colour, Type
and Engine. The programmer wants to construct an array of
vehicle objects with the following objects:
Vehicle 1 [Colour:"red” - Type:”car”- Engine:2000]
Vehicle 2 [Colour:”green” - Type:”bus”- Engine:4000]
Vehicle 3 [Colour:"blue" - Type:”motorcycle” - Engine:800]
Topic 4: 4.2.1
Arrays of objects
Pseudocode:
Vehicle1= new vehicle (setColour ="red", setType=”car, setEngine =2000)
Vehicle2= new vehicle (setColour =”green”, setType=”bus, setEngine =4000)
Vehicle3= new vehicle (setColour ="blue", setType="motorcycle, setEngine =800)
Topic 4: 4.2.1
Arrays of objects
Vehicle1= new vehicle (setColour ="red", setType=”car, setEngine =2000)
Vehicle2= new vehicle (setColour =”green”, setType=”bus, setEngine =4000)
Vehicle3= new vehicle (setColour ="blue", setType="motorcycle, setEngine =800)
Use of an array of objects vehicle to find the location of an object that has data field
type = “bus”
A = [Vehicle1, Vehicle2, Vehicle3]
loop I from 0 to2
if A[I].getType = “bus” then
output "bus found at array position" , I
end if
end loop
Topic 4: 4.2.1
Two dimensional arrays
In two dimensional arrays data come in the form of a data table.
A typical example is a table that depicts the average monthly
temperature for 10 cities.
Topic 4: 4.2.1
This program will use the array
TEMP which is a 2D ARRAY. It
will print the contents of the
array: 12 months 5 cities
Topic 4: 4.2.1
The four key standard algorithms
• Sequential search
• Binary search
• Bubble sort
• Selection sort
Topic 4: 4.2.1
Sequential search
● It’s an algorithm to find an item in a list.
● It is considered to be the simplest search algorithm
● It starts at the first element and compares each element to
the one it’s looking for until it finds it (brute force strategy)
● It is usually used with collections (which are unsorted lists of
items) and text/csv file reading.
Topic 4: 4.2.1
Sequential search
Topic 4: 4.2.1
Sequential search

https://www.youtube.com/watch?v=CX2CYIJLwfg
Topic 4: 4.2.1
Sequential search - Pseudocode
NAMES = (“Bob”,”Betty”,”Kim”,”Lucy”,”Dave”)
output "These names start with D"
loop while NAMES.hasNext()
NAME = NAMES.getNext()
if firstLetter(NAME) = "D" then
output NAME
end if
end loop
Topic 4: 4.2.1
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 (divide
and conquer strategy)
● 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) and it is very efficient for large arrays.
Topic 4: 4.2.1
Binary search

https://www.youtube.com/watch?v=D5SrAga1pno
Topic 4: 4.2.1
Binary search
Topic 4: 4.2.1
Binary search - Pseudocode
Topic 4: 4.2.1
Binary search vs Linear search
Topic 4: 4.2.1
Bubble sort
Bubble sort is a simple sorting algorithm that repeatedly steps
through the array to be sorted. It compares adjacent items
(pairs of adjacent array elements) and exchanges them if they
are not in the correct order (ascending or descending). The
algorithm makes multiple passes until no swaps are necessary
and the elements of the array are sorted. The algorithm is very
slow and impractical for most cases.
Topic 4: 4.2.1
Bubble sort

https://www.youtube.com/watch?v=8Kp-8OGwphY
Topic 4: 4.2.1
Bubble sort
Topic 4: 4.2.1
Bubble sort - Pseudocode

Swapping value method


with a Temp variable
Topic 4: 4.2.1
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.
Topic 4: 4.2.1
Selection sort

https://www.youtube.com/watch?v=f8hXR_Hvybo
Topic 4: 4.2.1
Selection sort
Topic 4: 4.2.1
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

You might also like