10. 2 ARRAYS and Algorithm (Searching & SOrtin
10. 2 ARRAYS and Algorithm (Searching & SOrtin
Syllabus Content:
10.2 Arrays
use the technical terms associated with arrays including upper and lower bound
select a suitable data structure (1D or 2D array) to use for a given task
use pseudocode for 1D and 2D arrays (pseudocode will use square brackets to contain the
array subscript, for example a 1D array as A[1:n] and a 2D array as C[1:m, 1:n])
write program code using 1D and 2D arrays
write algorithms/program code to process array data including:
o Sort using a bubble sort
o Search using a linear search
An array is a special variable that has one name, but can store multiple values. Each value is stored in an
element pointed to by an index.
The first element in the array has index value 0, the second has index 1, etc
PYTHON Code:
name = []
marks = []
for count in range(5):
name.append (str(input("Enter name: ")))
marks.append (int(input("Enter marks ")))
print("Name ", name, " scored ", marks)
OUTPUT screen
num = [1, 2, 3]
num.append(4)
num.extend([5, 6])
print(num) # will print this in output [1, 2, 3, 4, 5, 6]
Python Code
row, col = 3, 4
table = [[0 for x in range(row)] for y in range(col)]
# Creates a list containing 5 lists, each of 8 items, all set to 0
For row = 0 To 2
For column = 0 To 3
Console.WriteLine("Row = " & row & "column = " & column & “has Value”)
Console.WriteLine(matrix(row, column))
Next
Next
Console.ReadKey()
End Sub
Multi-Dimensional Arrays:
A multi-dimensional array can be thought of as a table, each element has a row and
column index.
Following example declares a two-dimensional array called matrix and would be
declared by
The pseudocode linear search algorithm and identifier table to find if an item is in the
1D array(myList) is given below.
DECLARE count, num As Integer
DECLARE found As Boolean = False
//Creating array to search item (Free notes @ www.majidtahir.com)
DECLARE Mylist() As Integer = {4, 2, 8, 17, 9, 3, 7, 12, 34, 21}
OUTPUT ("Please enter any integer to be checked in List")
INPUT num
For count = 1 To 10
If num = Mylist(count) Then
found = True
End If
Next count
Bubble Sort:
Bubble sort is a sorting algorithm that compares each value in the list with the next value, and
swaps the value if not in order. Algorithm is repeated many times in loop to arrange all values in
order. We call it Bubble Sort because small values rise to the top slowly like bubbles rise in
water. After every sorting from start to end of list, biggest value goes at the bottom.
When we have completed the first pass through the entire array, the largest value is in the
correct position at the end of the array. The other values may or may not be in the correct order.
We need to work through the array again and again. After each pass through the array the next
largest value will be in its correct position, as shown in Figure below.
In effect we perform a loop within a loop, a nested loop. This method is known as a bubble
sort. The name comes from the fact that smaller values slowly rise to the top, like bubbles in a
liquid.
upperBound LENGTH(MyList)
lowerBound 0
top upperBound
REPEAT
FOR index = lowerBound TO (top – 1)
Swap FALSE
IF MyList [count] > MyList [count + l]
THEN Temp MyList[count]
MyList[count] MyList[count + 1]
MyList[count + 1] Temp
swaps TRUE
END IF
NEXT
top top – 1
UNTIL (NOT swap) OR (top = 0)
Refrences:
Computer Science by David Watson & Helen Williams
Visual Basic Console Cook Book
Computer Science AS and A level by Sylvia Langfield and Dave Duddell
https://www.sitesbay.com/javascript/javascript-looping-statement
http://wiki.jikexueyuan.com/project/lua/if-else-if-statement.html