4.1 Computational Thinking and Problem-Solving: Algorithm
4.1 Computational Thinking and Problem-Solving: Algorithm
ALGORITHM
This is the simplest kind of searching. It is also called the serial sort or the sequential sort.
Searching starts with the first item and then moves to each item in turn until either a match is found
or the search reaches the end of the data set with no match found.
A criteria will allow a possible match to be found within the records / items stored. If no match is
found, then the process will return the appropriate message.
The Algorithm
1. Setup the search criteria
2. Examine first item in the data set
3. If there is a match, end the procedure and return the result with ‘match found’
4. If no match is found repeat with the next item
5. If the last item is reached and no match is found return ‘match not found’
Advantages
Linear search is fairly easy to code. For example the pseudo-code below shows the algorithm in
action.
Procedure serial_search ()
For I = 0 to 19
Check_for_match (I, list)
If match_found return ‘match found’
Next i
Return ‘no match found’
End
Good performance over small to medium lists. Computers are now very powerful and so
checking potentially every element in the list for a match may not be an issue with lists of
moderate length.
The list does not need to be in any order. Other algorithms only work because they assume
that the list is ordered in a certain way. Serial searching makes no assumption at all about the
list so it will work just as well with a randomly arrange list as an ordered list.
Not affected by insertions and deletions. Some algorithms assume the list is ordered in a
certain way. So if an item is inserted or deleted, the computer will need to re-order the list
before that algorithm can be applied. The overhead of doing this may actually mean that serial
searching performs better than other methods.
ALGORITHM 1
CAMBRIDGE – COMPUTING 9608 – GCE A2
Disadvantages
May be too slow for oversized lists. Computers take a finite amount of time to search each
item, so naturally, the longer the list, the longer it will take to search using the serial method.
The worst case being no match found and every item had to be checked.
This speed disadvantage is why other search methods have been developed.
BINARY SEARCH
ALGORITHM 2
CAMBRIDGE – COMPUTING 9608 – GCE A2
The worst case will be if the item you want to look for is at the end of the array.
In this case a linear search would take N iterations to retrieve this item.
Whereas, a binary search would take 2log2 (n) iterations to retrieve this item.
To watch an animation for searching, click the link below:
https://blog.penjee.com/wp-content/uploads/2015/04/binary-and-linear-search-animations.gif
INSERTION SORT
In this method we compare each number in turn with the numbers before it in the list. We then
insert the number into its correct position.
Consider the list
20 47 12 53 32 84 85 96 45 18
We start with the second number, 47, and compare it with the numbers preceding it. There is only
one and it is less than 47, so no change in the order is made. We now compare the third number,
12, with its predecessors. 12 is less than 20 so 12 is inserted before 20 in the list to give the list
12 20 47 53 32 84 85 96 45 18
This is continued until the last number is inserted in its correct position. Below, the blue numbers
are the ones before the one we are trying to insert in the correct position. The red number is the
one we are trying to insert.
ALGORITHM 3
CAMBRIDGE – COMPUTING 9608 – GCE A2
ALGORITHM
Simple to code
Very good performance with small lists (how small is small depends on the power of the
computer it is running on)
Very good when the list is almost sorted. In this case very few compares need to be done. The
worst case is when the list is in reverse order.
Sort-stable which means it keeps the relative positions of the elements intact
Very memory efficient – it only needs one extra storage location to make room for the moving
items.
Good with sequential data that is being read in one at a time e.g. tape, hard disk or data being
received sequentially.
DISADVANTAGE OF INSERTION SORT COMPARED TO ALTERNATIVES
Poor performance with large lists.
BUBBLE SORT
The Bubble Sort is a simple sorting algorithm that works by repeatedly steeping through the list to
be sorted, comparing each pair and swapping them if they are in the wrong order. The pass through
the list is repeated until no swaps are needed, which indicated what the list is sorted. The algorithm
gets its name from the way larger elements “bubble” to the top of the list. It is a very slow way of
sorting data and rarely used in the industry. There are much faster sorting algorithms out there such
as the Quick Sort or the Heap Sort.
STEP-BY-STEP EXAMPLE
Let us take the array of numbers “5 1 4 5 8”, and sort the array from lowest number to greatest
number using bubble sort algorithm. In each step, elements written in bold are being compared.
First Pass:
(5 1 4 2 8) (1 5 4 2 8) Here, algorithm compares the first two elements, and swaps them
since 5>1
(1 5 4 2 8) (1 4 5 2 8) It then compares the second and third items and swaps since 5>4
(1 4 5 2 8) (1 4 2 5 8) Swap since 5>2
(1 4 2 5 8) (1 4 2 5 8) Now, since these elements are already in order (8>5), algorithm
does not swap them.
ALGORITHM 4
CAMBRIDGE – COMPUTING 9608 – GCE A2
The algorithm has reached the end of the list of numbers and the largest number, 8, has bubbled to
the top. It now starts again.
Second Pass:
(1 4 2 5 8) (1 4 2 5 8) No swap needed
(1 4 2 5 8) (1 2 4 5 8) Swap since 4>2
(1 2 4 5 8) (1 2 4 5 8) No swap needed
(1 2 4 5 8) (1 2 4 5 8) No swap needed
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm
needs one whole pass without any swap to know it is sorted.
Third Pass:
(1 2 4 5 8) (1 2 4 5 8)
(1 2 4 5 8) (1 2 4 5 8)
(1 2 4 5 8) (1 2 4 5 8)
(1 2 4 5 8) (1 2 4 5 8)
Finally, the array is sorted, and the algorithm can terminate.
ALGORITHM
It is an important point to note that the number of iterations that each algorithm takes to sort the
data in either ascending or descending order may vary according to how the initial data is organized.
If the data is partially sorted, then the algorithms may take less iterations.
If the data is in descending order and you want to sort the data in ascending order, then the
algorithm will take maximum iterations to sort the data.
LINKED LIST
INSERTION
Consider the figure below, which shows a linked list and a free list. The linked list is created by
removing cells from the front of the free list and inserting them in the correct position in the linked
list.
ALGORITHM 5
CAMBRIDGE – COMPUTING 9608 – GCE A2
Now suppose we wish to insert an element between the second and third cells in the linked list. The
pointers have to be changed to those in Figure above.
The algorithm must check for an empty free list as there is then no way of adding new data. It must
also check to see if the new data is to be inserted at the front of the list. If neither of these is
needed, the algorithm must search the list to find the position for the new data.
ALGORITHM
ALGORITHM 6
CAMBRIDGE – COMPUTING 9608 – GCE A2
DELETION
Suppose we wish to delete the third cell in the Linked list shown above. The result is shown below.
ALGORITHM
AMENDMENT
Amendments can be done by searching the list to find the cell to be amended.
The algorithm is
SEARCHING
Assuming that the data in a linked list is in ascending order of some key value, the following
algorithm explains how to find where to insert a cell containing new data and keep the list in
ascending order. It assumes that the list is not empty and the data is not to be inserted at the head
of the list.
ALGORITHM 7
CAMBRIDGE – COMPUTING 9608 – GCE A2
Note: A number of methods have been shown here to describe algorithms associated with linked
lists. Any method is acceptable provided it explains the method. An algorithm does not have to be
in pseudo code, indeed, the sensible way of explaining these types of algorithm is often by diagram.
BINARY TREE
The TREE is a general data structure that describes the relationship between data items or ‘nodes’.
The parent node of a binary tree has only two child nodes.
ALGORITHM 8
CAMBRIDGE – COMPUTING 9608 – GCE A2
ALGORITHM 9
CAMBRIDGE – COMPUTING 9608 – GCE A2
ALGORITHM 10
CAMBRIDGE – COMPUTING 9608 – GCE A2
DELETION
Data is a tree serves two purposes (one is to be the data itself) the other is to act as a reference for
the creation of the sub-tree below it. If Joh is deleted there no way of knowing which direction to
take at that node to find the details of the data beneath it.
Solution 1 is to store Joh’s sub-tree in temporary storage and then rewrite it to the tree after Joh is
deleted. (The effect is that one member of the sub-tree will take over from Joh as the root of that
sub-tree).
Solution 2 is to mark data Joh as deleted so that the data no longer exists but it can maintain its
action as an index for that part of the tree so that the sub-tree can be correctly negotiated.
RETRIEVAL
The algorithm for this is:
STACKS
INSERTION
The figure below shows a stack and its head pointer. Remember, a stack is a Last-In-First-Out (LIFO)
data structure. If we are to insert an item into a stack we must first check that the stack is not full.
Having done this we shall increment the pointer and then insert the new data item into the cell
pointed to by the stack pointer. This method assumes that the cells are numbered from 1 upwards
and that, when the stack is empty, the pointer is zero.
ALGORITHM 11
CAMBRIDGE – COMPUTING 9608 – GCE A2
ALGORITHM
1. Check to see if stack is full.
2. If the stack is full report an error and stop.
3. Increment the stack pointer.
4. Insert new data item into cell pointed to by the stack pointer and stop.
DELETION (EQUIVALENT TO RETRIEVAL)
When an item is deleted from a stack, the item’s value is copied and the stack pointer is moved
down one cell. The data itself is not deleted. This time, we must check that the stack is not empty
before trying to delete an item.
The algorithm for deletion is:
1. Check to see if stack is empty.
2. If the stack is empty report an error and stop.
3. Copy data item in cell pointed to by the stack pointer.
4. Decrement the stack pointer and stop.
There are the only two operations you can perform on a stack.
QUEUE
The figure below shows a queue and its head and tail pointers. Remember, a queue is a First-In-
First-Out (FIFO) data structure. If we are to insert an item into a queue we must first check that the
stack is not full. Having done this we shall increment the pointer and then insert the new data item
into the cell pointed to by the head pointer. This method assumes that the cells are numbered from
1 upwards and that, when the queue is empty, the two pointers point to the same cell.
ALGORITHM 12
CAMBRIDGE – COMPUTING 9608 – GCE A2
A Hash Table or a Hash Map is a data structure that associates keys with values. The primary
operation it supports efficiently is a lookup: given a key (e.g. a person’s name), find the
corresponding value (e.g. that person’s telephone number). It works by transforming the key using a
hash function into a hash, a number that the hash table uses to locate the desired value.
INSERTION
In a hash table, each slot either contains a key or NIL (if the slot is empty) and if the slot is empty,
then you can insert your data item in that slot. If the hash table is searched until the end, and no
empty slots are found, then the algorithm will return ‘hash table full’.
Hash_insert (T, k)
I := 0
Repeat j := h(k, i)
If T[j] = NIL
Then T[j] := k
Else i := i + 1
Until i = m
Error “hash table overflow”
ALGORITHM 13
CAMBRIDGE – COMPUTING 9608 – GCE A2
SEARCHING
Hash_search (T, k)
i := 0
repeat j :- h (k. i)
if T[j] = k
then return j
i := i + 1
until T[j] = NIL or i = m
return NIL
For different searching and sorting algorithms, the logic behind their functioning differs. Some
algorithms can return the answer in less number of iterations than compared to other algorithms.
If we compare the Binary search to the Linear search, it should be quite obvious by now that the
Binary search takes fewer iterations to return the value being searched for. However, as mentioned
before, the Linear search is very easy to implement which means that it does not take up a lot of
computer memory for its functioning unlike the Binary search. This is the compromise that you have
to make for stronger algorithms. Those which take fewer iterations (take less time to return answer)
are more likely to consume more computer memory and vice versa.
ALGORITHM 14