Algorithm Design and Data Structures
Algorithm Design and Data Structures
ALGORITHMS
- A set of instructions describing the steps followed in performing a specific task, for
example, calculating change.
- They are a sequence of instructions for solving a problem.
- Algorithms can be illustrated using the following:
Descriptions, Flowcharts, Pseudocodes, Structure diagrams
Pseudocodes:
- These are English-like statements, closer to programming language that indicates steps
followed in performing a specific task.
- They are means of expressing algorithms without worrying about the syntax of the
programming language.
- There are no strict rules on how pseudocode statements should be written.
- Indentations are very important in writing pseudocodes since they clearly indicate the
extent of loops and conditional statements.
- They are however independent of any programming language.
- An example is as follows:
Enter centigrade temperature, C
If C = 0, then stop.
Set F to 32 + (9C/5)
Print C and F
End
Control Structures/Programming Constructs/building blocks of a structured program
- A number of control structures are used in designing Pseudocodes.
- These includes: simple sequence, selection and iteration.
NB: GO TO statements (also called spaghetti programming) must be avoided as the programs
will be difficult to follow, difficult to debug, and difficult to maintain.
17
ALGORITHM DESIGN AND DATA STRUCTURES
A Selection construct allows a program to choose between different paths based on whether a
condition is True or False. It is just like making a decision in real life.
The IF...THEN...ELSE Statement
This is the most common selection tool.
Scenario: Think of a grading system for a Form 5 test.
Logic: IF a student gets 50% or more, THEN they pass. ELSE, they fail.
Real-World Zimbabwe Example: EcoCash
When you dial *151# to use EcoCash, the system uses selection:
IF your PIN is correct, THEN show the menu.
ELSE, show an error message saying "Invalid PIN."
� Example:
IF mark >= 50 THEN
DISPLAY "Pass"
ELSE
DISPLAY "Fail"
ENDIF
Cascaded/Nested If Statements
This is whereby if statements are found inside other if statements (nested Ifs) as shown below:
Start
Enter “First Number”, A
Enter “Second Number”, B
Enter “Third Number”, C
If A>B Then
If B>C Then
Print “A is the biggest Number”
End If
End If
End.
CASE Statement: This is an alternative to the IF...THEN...ELSE statement and is shorter. For
example:
START
18
ALGORITHM DESIGN AND DATA STRUCTURES
A group of statements between the looping structures is called the loop body and is the one that
is repeatedly executed.
The For...Next loop is appropriate when the number of repetitions is known well in advance, e.g.
five times. An example of a program that uses the For...Next loop is as follows:
Sum, Average = 0
FOR I = 1 to 5 DO
Enter Number
19
ALGORITHM DESIGN AND DATA STRUCTURES
b. Repeat...Until Structure: This is a looping structure that repeatedly executes the loop body
when the condition set is FALSE until it becomes TRUE. The number of repetitions may not be
known in advance and the loop body is executed at least once. The syntax is as follows:
Repeat
Statement 1
Statement 2 loop body
................
Until {Condition}
For example
Sum, Average, Count = 0
Repeat
Enter Number (999 to end)
Sum = Sum + Number
Count = count + 1
Until Number = 999
Average = Sum / count
Print Sum, count, Average
End
In the above program:
- Count records the number of times the loop body executes.
- 999 is used to stop further data entry through the keyboard and thereby ending the loop.
Such a value that stops further data entry through the keyboard thereby terminating a loop
is called a Rogue value or sentinel.
20
ALGORITHM DESIGN AND DATA STRUCTURES
- The condition here is {Number = 999}. The loop exits when the number 999 is entered.
If 999 is part of the number to be entered in this program, then the user has to split it into
two numbers, that is 999 = 990 + 9, therefore can be entered separately as 990 and 9.
- A flag is also used to control the loop. In this case 999 is also a flag.
NB. As for the Repeat...Until loop, the condition is tested after the loop body has been run at
least once, even when the condition is true from start. This is rather misleading.
b. While ... Do Structure
A looping structure in which the loop body is repeatedly executed when the condition set is
TRUE until it becomes FALSE. It is used when the number of repetitions is not known in
advance. The condition set is tested first before execution of the loop body. Therefore the loop
body may not be executed at all if the condition set is FALSE from start. The syntax of the
WHILE…ENDWHILE structure is as follows:
WHILE {condition}
Statement 1
Statement 2 loop body
................
ENDWHILE
An example of the program is as follows:
Sum, Count, Average = 0
WHILE Count < 6 DO
Enter Number
Sum = Sum + number
Count = count + 1
ENDWHILE
Average = Sum/count
Display sum, count, average
END
The word WEND can be used to replace the word ENDWHILE in some structures and
therefore is acceptable. The word Do, after the condition is optional.
21
ALGORITHM DESIGN AND DATA STRUCTURES
SORTING ALGORITHMS
Bubble sort
Sorting whereby the array is scanned from right to left multiple times (passes) until all
elements are in their correct positions
Compare each element with the adjacent, and if it is greater than the next element then swap
the elements.
For k = 0 to N – 1 {counts the number of passes / scans made to the array}
For i = 0 to N – 2 {use N – 2 instead of N – 1 so that at the end of the array you will be able to
compare Array[i] and Array[i+1]}
If Array[i] > Array[i+1] Then {compare adjacent elements}
Swap(Array[i], Array[i+1]) {swapping element, i.e Array[i] = [i+1]}
End if
Next i
Next k
Example
Arrange the following numbers in ascending order using the bubble sort algorithm
17 18 2 11 0
[0] [1] [2] [3] [4]
22
ALGORITHM DESIGN AND DATA STRUCTURES
23
ALGORITHM DESIGN AND DATA STRUCTURES
Next j
Next pass
Examine the following table. (Note that each pass represents the status of the array after the
completion of the inner for loop, except for pass 0, which represents the array as it was passed to
the function for sorting)
8 6 10 3 1 2 5 4 } pass 0
6 8 3 1 2 5 4 10 } pass 1
6 3 1 2 5 4 8 10 } pass 2
3 1 2 5 4 6 8 10 } pass 3
1 2 3 5 4 6 8 10 } pass 4
1 2 3 4 5 6 8 10 } pass 5
1 2 3 4 5 6 8 10 } pass 6
1 2 3 4 5 6 8 10 } pass 7
The above tabulated data clearly depicts how bubble sort works. Note that each pass results in
one number being bubbled to the end of the list.
24
ALGORITHM DESIGN AND DATA STRUCTURES
25
ALGORITHM DESIGN AND DATA STRUCTURES
The bubble sort standard algorithm can be modified or optimised to improve its efficiency as
shown below. The test data is 6 9 7 3
Identifier Explanation
Data[0…3] One-dimension array to store 4 numbers
MaxIndex The last index number of array elements
n The number of elements to compare in each pass
Pass Control variable for outer loop
j Control variable for inner loop
Temp Variable for temporary storage while swapping array elements
n = MaxIndex – 1
For Pass = 0 to MaxIndex – 1
For j = 0 to n
If Data(j) > Data(j+1)
Temp = Data(j)
Data(j) = Data(j + 1)
Data(j + 1) = Temp
End if
Next j
n = n – 1 // this restricts the algorithm to deal only with array elements not in
// correct positions in the inner loop
Next pass
Note: the effect of the statement n = n – 1 in the algorithm. The statement restricts the algorithm
from performing comparison on array elements which are already in the correct position. The
inner loop control variable c, iterates from 1 to 3 in the first pass, pass 1. In pass 2, the inner loop
terminates when c = 1. The idea here is to avoid CPU overheads and increase throughput.
26
ALGORITHM DESIGN AND DATA STRUCTURES
6 9 7 3
The trace table
Pass j Temp Data(j) Data(j+1) Swapping process
Temp=Data(j) Data(j)=Data(j+1) Data(j+1)=Temp
1 0 6 9
1 9 7 9 7 9
2 9 3 9 3 9
Status of the array after pass 1, note the shaded numbers
6 7 3 9 Moving on to pass 2
2 0 6 7
1 7 3 7 3 7
Status of the array after pass 2, note the shaded numbers
6 3 7 9 Moving on to pass 3
3 0 6 3 6 3 6
Status of the array after pass 3, note the shaded numbers
3 6 7 9
Quick Sort
7 2 1 6 8 5 3 4 5
27
ALGORITHM DESIGN AND DATA STRUCTURES
First select a pivot (select any one of the elements and move it to the end of the array) say 5
Now to rearrange the elements we need to make swaps so that elements less than the pivot are to
the left and those greater are to the right. - Swap element from left which is greater than the pivot
with element from the right which is smaller than the pivot, repeat the process until element from
left is greater than the pivot, then swap the element with the pivot
7 2 1 6 8 3 4 5
4 2 1 6 8 3 7 5
4 2 1 3 8 6 7 5
4 2 1 3 5 6 7 8
Sub list 1 – elements < pivot
28
ALGORITHM DESIGN AND DATA STRUCTURES
4 1 3 2
1 4 3 2
1 2 3 4
Now we have a sorted sub list, do the same for the other sub list
Then will have a sorted array
1 2 3 4 5 6 7 8
Another example
The elements below need to be sorted in ascending order
(28) 49 14 51 40 13 19
Let 28 be reference number denoted by brackets since it is first number in the list
We set two pointers: one at each end of the list
(28) 49 14 51 40 13 19
29
ALGORITHM DESIGN AND DATA STRUCTURES
19 13 14 51 40 (28) 49
The two pointers are now at the same position and the pivot, 28, has been placed at its rightful
position, where elements to the right of it are greater while those on the left are smaller. We now
create two sub list or array that exclude the pivot (28), the left sub-list and the right sub-list as
shown below
19 13 14 (28) 40 51 49
19 13 (14)
The two pointers are now at the same position and the pivot
40 51 49
Let 49 be reference number denoted by brackets
We set two pointers: one at each end of the list
40 51 (49)
The two pointers are now at the same position and the pivot
Then will have a sorted array
13 14 19 28 40 49 51
31
ALGORITHM DESIGN AND DATA STRUCTURES
Insertion
Items are copied from unsorted array to a new sorted array
Each element is inserted into the right place so that the output array is always sorted
Considerably faster than bubble sort but slower than quick sort
Searching Algorithms
a) Linear search
- searching data items one by one until item is found or end of list reached
- this involves comparing each item in the list with the item sought for match, if match
then item found
Start at the beginning of list
Repeat
Test next item for a match
Until item found or end of list reached
b) Binary Search
- Search through an ordered array and is much faster than linear search.
- Divides the array into 3 parts: middle item, lower part and upper part
- The middle item is compared with the item sought for match, if they match them item
found.
- If middle item is less than item sought (middle < itemsought), then the lower half of
the array is discarded, it will be of no interest.
- Therefore, the number of items search is reduced by half, repeat the process until the
last item is examined, with either the upper half or lower half of the items searched
being discarded at each pass.
BinarySearch (Low, Top, ItemSought)
Low = 0
Top = n – 1 {n = number of items in the array}
While low <=Upper
Mid = (Low + Top)/2
If A[Mid] = ItemSought Then {compare middle with ItemSought, if equal them
search found}
32
ALGORITHM DESIGN AND DATA STRUCTURES
Mid = (6+9)/2 = 7,5 = 8 {round off to get the whole number part}
19 21 27 29
[6] [7] [8] [9]
-
- Compare the item at index 8 with ItemSought (27 and 19)
- Mid (27) is greater than ItemSought (19), therefore discard the upper half,
33
ALGORITHM DESIGN AND DATA STRUCTURES
19 21
[6] [7]
Mid = (6+7)/2 = 6,5 = 7 {round off to get the whole number part}
19 21
[6] [7]
Mid = (6+6)/2 = 6
- Compare the item at index 6 with ItemSought (19 and 19), Mid = ItemSought
therefore item found
Trace Table
Low Top Mid Found
0 9 5 False
6 9 8 False
6 7 7 False
6 6 6 True
34
ALGORITHM DESIGN AND DATA STRUCTURES
DATA STRUCTURES
A data structure is a collection of different data items that are stored together as a single unit and
the operations allowable on them. Such data structures includes arrays, trees, linked lists, stacks
and queues. Data structures can be static or dynamic.
Static data structure
Are those which do not change in size while the program is running, e.g arrays and fixed
length records.
Most arrays are static, i.e., once you declare them, they cannot change in size.
It main advantage is that amount of storage is known and therefore is easier to program
Advantages of Static Data Structures
Easy to check for overflow
Memory allocation is fixed and therefore there is no problem with adding and removing
data items.
Compiler can allocate space during compilation
Easy to program as there is no need to check for data structure size at any given
time/point
An array allows random access and is faster to access elements than in other data
structures
Disadvantages of Static Data Structures
Programmer has to estimate maximum amount of space needed, which may be difficult
Can waste a lot of space if some space is left with no data entered into it.
Adding, removing and modifying elements is not directly possible. If done, it requires a
lot of resources like memory.
DYNAMIC DATA STRUCTURES
Can increase and decrease in size while the program is running, e.g. binary trees, linked
lists, etc.
they uses the space needed at any time, no limitations at all, unless computer memory is
full
its size changes as data is added & removed (size is not fixed)
Advantages of Dynamic Data Structures
Only uses the space that is needed at any time
35
ALGORITHM DESIGN AND DATA STRUCTURES
Makes efficient use of the memory, no spaces lie idle at any given point
Storage no-longer required can be returned to the system for other uses.
Does not allow overflow
There is effective use of resources as resources are allocated at run-time, as they are
required.
Disadvantages of Dynamic Data Structures
Complex and more difficult to program as software needs to keep track of its size and
data item locations at all times
Can be slow to implement searches
A linked list only allows serial access
Static data structure
Array
An array is a static data structure, which stores and implements a set of items of the same data
type using the same identifier name, in contiguous memory location.
Declaration of arrays using VB 6.0
Arrays are declared by stating the following parameters: array name, array size, dimensions and
the data type. The size of the array is the maximum number of elements that it can store. For
example, the following declares an array that reserves five locations in memory and labels these
as ‘Names’:
Dim Names(4) As String
This can also be declared as
Dim Names(0 to 4 ) As String
One may also declare the array as
Dim Names(1 to 5) As String
The last option modifies the starting value and the ending value of the indices, but the number of
elements still remains the same.
By default, this implies that the array stores at most 5 elements. On running, the computer creates
5 contiguous memory locations under the name “Names”. Thus Names will contain 5 partitions.
Each memory partition will be accessed using an index, which is the address of the memory space
in the array.
36
ALGORITHM DESIGN AND DATA STRUCTURES
The five individual locations are Names (0), Names (1), Names (2), Names (3) and Names (4).
Each data item is called an element of the array. To reference a particular element one must use
the appropriate index.
NB: However, most programming languages differ with Microsoft Visual basic in handling
arrays, especially on the amount of memory allocated. For example, using Java, the
following declaration:
Int [4 ]Names;
This array declaration creates exactly 4 memory spaces for the array Names. The indices of
the array range from 0 to 3 which are
Names[0], Names[1], Names[2] and Names[3]
Initialising an array in Visual Basic 6.0
The procedure of initializing an array in the computer memory is as follows:
- Size of array is calculated
- Location of array is decided according to data type and size
- Locations are reserved for the array
- Size of array is stored in a table
- Lower bound of the array is stored in a table
- Upper bound of array is stored in a table
- Data type is stored in a table
- Address of first element is stored in a table
Inserting data into an array
One may use the assignment statement or use looping structures. For example, the following
statement assigns data to the 4th element:
Names(3) = “Manyeruke”
Arrays simplify the processing of similar data. An algorithm for getting four names from the user
and storing them in the array Names is shown below:
Dim Names(4) As String
For i=0 to 4
37
ALGORITHM DESIGN AND DATA STRUCTURES
Input Value
Names(i)=Value
Next i
DYNAMIC DATA STRUCTURE
Binary Trees
A binary tree is a data structure, consisting of a root node and zero, one or two sub-tree which
are organised in a hierarchical way. Each node is a parent of at most two nodes.
-
The data items are held in nodes.
The possible routes are called paths/branches. They are lines connecting the nodes.
Each node has two possible paths.
The nodes are arranged in layers.
The first node is called the root, or root node. Each tree has only one root node.
However, each branch can have its branch root.
Node created by another one is called child node (children)
Each child node has only one parent node
Each parent node has at most two children
The last node is called the leaf node/terminal node (has no children)
Nodes that share common parent are called siblings
For example, given the following numbers: 20, 30, 5, 2, 7, 6, 17, 58, 41
Placing them in the binary tree is as follows:
38
ALGORITHM DESIGN AND DATA STRUCTURES
39
ALGORITHM DESIGN AND DATA STRUCTURES
40
ALGORITHM DESIGN AND DATA STRUCTURES
B. In-Order Traversal
The order of traversal is:
- Traverse the Left sub-tree
- Visit the Node
- Traverse the Right sub-tree.
This is generally given as LNR
For the diagram above, the pre-order traversal will be as follows:
2, 5, 6, 7, 17, 20, 30, 41, 58.
NB: In-order traversal prints items in ascending order or in alphabetical order if they are
alphabetic items.
In-order traversal algorithm:
1. For the current node, check if there is left-sb-tree. If it exists, go to the root node of this
sub-tree and then go to 2. If it doesn’t exist, go to 3.
2. Repeat 1
3. Print the current node
4. For the current node. Check whether it has a right sub-tree. If it has, go to 5. Else go to 6
5. Repeat 1
6. End
41
ALGORITHM DESIGN AND DATA STRUCTURES
C. Post-Order Traversal
The order of traversal is:
- Traverse the Left sub-tree
- Traverse the Right sub-tree.
- Visit the Node
42
ALGORITHM DESIGN AND DATA STRUCTURES
4. If new data item is less than value at current node go left else go right.
5. Current node = node reached (null if no node).
6. if node reached is null, create new node and enter data.
This can also be written as:
Repeat
Compare new value with root value
If new value > root value then
Follow right sub-tree
Else
Follow left sub-tree
Endif
Until no sub-tree
Insert new value as root of new sub-tree.
43
ALGORITHM DESIGN AND DATA STRUCTURES
The values in the list are read back into the tree (element which was originally on the left
will replace the deleted element (becomes root of that branch))
Searching item from Binary Tree
The algorithm is as follows:
Enter item to search(this item)
Start at root node
Repeat
If wanted item = this item Then
Found = True
Display Item
Else
If wanted Item >this item Then
Follow right pointer
Else
Follow left pointer
EndIf
EndIf
Until (Found=True or Null pointer is encountered)
Binary tree maintain the order of elements. However, if one element is deleted, the order is
affected.
In some cases, each node (data) may be assigned pointers (right and left pointer). This is as
illustrated below:
44