Caie A2 Level Computer Science 9618 Practical v4
Caie A2 Level Computer Science 9618 Practical v4
ORG
CAIE A2 LEVEL
COMPUTER
SCIENCE
SUMMARIZED NOTES ON THE PRACTICAL SYLLABUS
CAIE A2 LEVEL COMPUTER SCIENCE
Python Code
def bubble_sort(arr):
n = len(arr)
# Traverse through all array elements
1.2. Binary Search for i in range(n):
# Last i elements are already sorted, so we don't need t
The necessary condition for a binary search is that the
for j in range(0, n-i-1):
list/array being searched must be ordered/sorted.
# Swap if the element found is greater than the next
if arr[j] > arr[j+1]:
How does It Work?
arr[j], arr[j+1] = arr[j+1], arr[j]
The middle item/index of the list is found In the worst case, it has a time complexity of O(n^2), where n
Item at the middle of the list is compared to item user
is the number of elements in the array.
inputs
if the item in the middle of the list is the same as the item
that item user inputs, a message saying “item found” is 1.4. Insertion Sort
output.
If the item is greater than what the user inputted, all the How Does It Work?
items at the index lower than the middle index are
The algorithm starts with the assumption that the first
discarded.
element in the array is already sorted.
If the item is lower than what the user inputted, all the
items at the index greater than the middle index are It then compares the next element with the sorted portion
discarded. of the array.
The above steps are repeated until the item searched for If the next element is smaller, it shifts the larger elements
is found to the right until it finds the correct position for the next
If one item is left in the list and it is not the item searched element and inserts it there.
The sorted portion of the array grows with each iteration
for, a message saying “item not found” is outputted.
until the entire array is sorted.
The binary search algorithm has a Big O notion of O(log n). The process is repeated until all elements are in their
The log is of base 2. correct positions.
WWW.ZNOTES.ORG
CAIE A2 LEVEL COMPUTER SCIENCE
def insertion_sort(arr):
for i in range(1, len(arr)): OUTPUT “Stack is full”
key = arr[i]
j=i-1 ELSE
2.2. Queues
PROCEDURE PopFromStack
Queue: an ADT where new elements are added at the end
IF TopOfStack = -1 of the queue, and elements leave from the start of the
queue
THEN FIFO: First In, First Out Data structure
Creating a Circular Queue:
OUTPUT “Stack is already empty”
PROCEDURE Initialise
ELSE
Front = 1
OUTPUT MyStack[ TopOfStack ] “is popped”
Rear = 6
TopOfStack ← TopOfStack – 1
NumberInQueue := 0
ENDIF
END PROCEDURE
ENDPROCEDURE
To add an Element to the Queue:
IF NumberInQueue == 6
PROCEDURE PushToStack
THEN Write (“Queue overflow”)
IF TopOfStack = MaxStackSize
ELSE
THEN
WWW.ZNOTES.ORG
CAIE A2 LEVEL COMPUTER SCIENCE
ENDIF NameList[50].Pointer ← 0
ENDPROCEDURE HeadPointer ← 0
FreePointer ← 1
The front of the queue is accessed through the pointer
Front.
To add an element to the queue, the pointers have to be A user-defined record type should first be created to
followed until the node containing the pointer of 0 is represent a node’s data and pointer:
reached → the end of the queue, and this pointer is then
changed to point to the new node.
In some implementations, two pointers are kept: 1 to the
front and 1 to the rear. This saves traversing the whole
queue when a new element is to be added.
To Remove an Item from the Queue
PROCEDURE DeQueue
IF NumberInQueue == 0
ELSE
NewItem = Q[Front]
NumberInQueue = NumberInQueue – 1
IF Front ==6
THEN Front = 1
ELSE
Front = Front + 1
ENDIF
ENDIF
END PROCEDURE
Items may only be removed from the front of the list and
added to the end of the list
WWW.ZNOTES.ORG
CAIE A2 LEVEL COMPUTER SCIENCE
WWW.ZNOTES.ORG
CAIE A2 LEVEL COMPUTER SCIENCE
3. Recursion
Part of the Computational Thinking and Problem-Solving
Chapter
WWW.ZNOTES.ORG
CAIE A2 LEVEL COMPUTER SCIENCE
Part of the Further Programming Chapter 4.3. Defining Classes and Methods
4.1. Key Terms & Definitions • To define a class in Python, use the class keyword followed
by the class name.
Objects: Instances of classes representing real-world • Inside the class, you can define attributes (variables) and
entities. methods (functions) that belong to the class.
Properties/Attributes: The data items/attributes and the Here's a simple example of defining a class called Person
data types // characteristics defined in a class. with attributes and methods as shown below:
Methods: the procedures/ functions / programmed
instructions in a class that act on the
properties/attributes.
Classes: Blueprint for creating objects with shared
attributes and methods.
Inheritance: It is a mechanism for creating a new class
based on an existing one, inheriting its attributes and
methods. Through inheritance, attributes contained in one
class (parent class) are made available to / reused by
another class (child class).
Polymorphism: Ability to use different classes through a
common interface. It allows the same method to take on
4.4. Get and Set Methods
different behaviours depending on which class is
These methods are used to access/change attributes set to
instantiated. These methods can be redefined for derived
be private in a class. These methods are decelerated inside
classes.
the class.
Containment (Aggregation): Combining multiple objects to
create a more complex object.
Encapsulation: Hiding the internal details of a class from
the outside.
Getters and Setters: Methods for accessing and modifying
object attributes. Get methods/Getters are used to access
attributes, while set methods/setters are used to modify
object attributes.
Instances: Individual objects created from a class.
4.2. Constructor
Constructors are functions that are used for initializing the 4.5. Inheritance
attributes/Properties of a class.
• Inheritance is a fundamental concept in OOP that allows you
A constructor in Object-Oriented Programming (OOP) is a to create a new class (a derived or child class) based on an
special method within a class that is automatically called existing class (a base or parent class).
when an object of that class is created. • The child class inherits the attributes and methods of the
Its primary purpose is to initialize the object's attributes or parent class and can also add new attributes and methods or
perform setup actions. override the ones inherited.
In Python, the constructor is typically named init and takes In the context of the Person class, Inheritance would involve
the self parameter, which refers to the created object. creating a child class, such as a Student or Employee, that
Inside the constructor, you initialize the object's attributes. inherits attributes and methods from the Person class. For
For example: example, a Student class could inherit the name and age
attributes and the get_name method from the Person class.
Polymorphism
WWW.ZNOTES.ORG
CAIE A2 LEVEL COMPUTER SCIENCE
• Polymorphism is the ability of different classes to be treated leading underscore). This indicates that __name it should not
as instances of a common base class. It allows objects of be accessed directly from outside the class. Instead, you
different classes to be used interchangeably if they provide a controlled interface through the get_name and
implement the same methods or interface. set_name methods, ensuring that name changes are
• Polymorphism promotes flexibility and extensibility in your validated and controlled.
code. Below is a brief code example illustrating how inheritance,
In the context of the Person class, Polymorphism could be polymorphism, and encapsulation could be applied to the
applied when you have different types of persons, such as Person class:
students, employees, and teachers, each having a get_name
method. You can call get_name on instances of these
different classes without knowing their specific type, as long
as they all have a get_name method.
Encapsulation
WWW.ZNOTES.ORG
CAIE A2 LEVEL
Computer Science