Topic 4.2.1 - Describe the characteristics of standard algorithms on linear arrays
Topic 4.2.1 - Describe the characteristics of standard algorithms on linear arrays
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
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
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