Professional Documents
Culture Documents
8.1Counting
8.2 Accumulating
8.3 Swapping
8.4 Sorting
8.5 Searching
8.6 Merging Two Sorted Arrays
Computer and Information
Technology for (HKCEE)
Module A2: Part C
8.1 Counting
Counting
count the occurrence of a specific value or variable
in a loop
keep track of the number of times the loop has
been executed
8.1 Counting
Counting
count := 0; {initialise counter}
read(letter); {read the first character}
WHILE letter <> ‘.’ DO
BEGIN
count := count +1; {increment counter}
read(letter) {get the next character}
END;
8.2 Accumulating
Accumulating
sum up a specific value or variable
sum :=0 {initialise sum}
count := 1;
WHILE letter <= 10 DO
BEGIN
read(num); {input a value}
sum := sum + num; {add the value to sum}
count := count +1
END;
8.3 Swapping
Swapping
exchanging the content of two elements
Cup_A := ‘coffee’;
Cup_B := ‘tea’;
Spare_Cup := ‘’;
Spare_Cup := Cup_A;
8.3 Swapping
Swapping
exchanging the content of two elements
Cup_A := Cup_B;
Cup_B := Spare_Cup;
8.4 Sorting
Sorting
transform a list into an equivalent list
the elements are arranged in ascending or
descending order
Two operations of sorting
compare two items and decide which one is in
front of the other
swap the positions of the two items if necessary
8.4 Sorting
Bubble sort
each pair of adjacent elements in the list is
compared
the elements are swapped if they are not in the
desired order
repeat until the whole list is sorted
8.4 Sorting
Bubble sort: in descending order
arr Operations
Stage 1
[1] [2] [3] [4]
Compare [1] and [2]
Step 1: 25 55 40 60
and swap
8.4 Sorting
Bubble sort: in descending order
arr Operations
Stage 2
[1] [2] [3] [4]
Compare [1] and [2]
Step 1: 55 40 60 25
(no swap)
8.4 Sorting
Bubble sort: in descending order
arr Operations
Stage 3
[1] [2] [3] [4]
Compare [1] and [2]
Step 1: 55 60 40 25
and swap
Sorted array:
60 55 40 25
8.4 Sorting
Selection Sort
based on compare-and-swap
Procedures:
1. Find the smallest / largest item in the entire list of
items
2. Move the item to the beginning of the list.
3. Look at the items that are left, again find the one with
the largest / smallest value
4. Repeat 1 to 3
8.4 Sorting
Selection sort: in descending order
arr Operations
[1] [2] [3] [4]
Find the smallest of first
Pass 1: 25 55 40 60 four elements and swap it
with arr[4]
8.4 Sorting
Cost of sorting is measured by
the number of array elements that must be
compared to each other
the number of swaps of two array elements
Types Comparisons Swaps (worse case)
for N elements for N elements
N(N − 1) N(N − 1)
Bubble sort
2 2
N(N − 1)
Selection sort N −1
2
8.5 Searching
Searching
to look for a particular value or item in a data
structure
Search key
a data element of the same type as the list
elements
Linear Search
to proceed sequentially to examine elements until
the search key value is found
8.5 Searching
Linear search of an ordered array
Target = 45
arr Operations
[1] [2] [3] [4]
Compare arr[1] and
Step 1: 60 55 40 25 target, stop if
target >= arr[1]
Compare arr[2] and
Step 2: 60 55 40 25 target, stop if
target >= arr[2]
Compare arr[3] and
Step 3: 60 55 40 25 target, stop if
target >= arr[3]
unsuccessful search
© Longman Hong Kong Education Page16
Computer and Information
Technology for (HKCEE)
Module A2: Part C
8.5 Searching
Linear search of an ordered array
Target = 40
arr Operations
[1] [2] [3] [4]
Compare arr[1] and
Step 1: 60 55 40 25 target, stop if
target >= arr[1]
Compare arr[2] and
Step 2: 60 55 40 25 target, stop if
target >= arr[2]
Compare arr[3] and
Step 3: 60 55 40 25 target, stop if
target >= arr[3]
successful search
© Longman Hong Kong Education Page17
Computer and Information
Technology for (HKCEE)
Module A2: Part C
8.5 Searching
Linear search of an unordered array
Target = 45
arr Operations
[1] [2] [3] [4]
Compare arr[1] and
Step 1: 25 55 40 60 target, stop if
target = arr[1]
8.5 Searching
Linear search of an unordered array
Target = 45
arr Operations
[1] [2] [3] [4]
Compare arr[3] and
Step 3: 25 55 40 60 target, stop if
target = arr[3]
unsuccessful search
8.5 Searching
Linear search of an unordered array
Target = 40
arr Operations
[1] [2] [3] [4]
Compare arr[1] and
Step 1: 25 55 40 60 target, stop if
target = arr[1]
Compare arr[2] and
Step 2: 25 55 40 60 target, stop if
target = arr[2]
Compare arr[3] and
Step 3: 25 55 40 60 target, stop if
target = arr[3]
successful search
© Longman Hong Kong Education Page20
Computer and Information
Technology for (HKCEE)
Module A2: Part C
8.5 Searching
Binary search
repeat halving the list
compare the middle value in the sorted list with
the desired value
8.5 Searching
Binary search
Target = 17 Operations
arr
[1] [2] [3] [4] [5] [6] Set first to 1
Set last to 6
Step 1: 10 14 20 23 27 34 Compute mid = 3
target < arr[3]
first mid last
Set last to 2
Step 2: 10 14 20 23 27 34 Compute mid = 1
first last target > arr[1]
mid
8.5 Searching
Binary search
Target = 17 Operations
arr
[1] [2] [3] [4] [5] [6] Set first to 2
Compute mid = 2
Step 3: 10 14 20 23 27 34 target > arr[2]
first
mid
last unsuccessful search
Set first to 3
Step 2: 10 14 20 23 27 34 Since last < first
last first terminate
8.5 Searching
Binary search
Target = 27 Operations
arr
[1] [2] [3] [4] [5] [6] Set first to 1
Set last to 6
Step 1: 10 14 20 23 27 34 Compute mid = 3
target > arr[3]
first mid last
Set first to 4
Step 2: 10 14 20 23 27 34 Compute mid = 5
first mid last target = arr[5]
successful search
© Longman Hong Kong Education Page24
Computer and Information
Technology for (HKCEE)
Module A2: Part C
8.5 Searching
Relative efficiency of searching
Difference between sequential search and binary
is large when the number of elements is colossal.
initial situation
after merging
END