You are on page 1of 27

Computer and Information Technology for (HKCEE) Module A2

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

© Longman Hong Kong Education Page2


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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;

© Longman Hong Kong Education Page3


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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;

© Longman Hong Kong Education Page4


Computer and Information
Technology for (HKCEE)
Module A2: Part C

8.3 Swapping
Swapping
exchanging the content of two elements
Cup_A := ‘coffee’;
Cup_B := ‘tea’;
Spare_Cup := ‘’;

Spare_Cup := Cup_A;

© Longman Hong Kong Education Page5


Computer and Information
Technology for (HKCEE)
Module A2: Part C

8.3 Swapping
Swapping
exchanging the content of two elements

Cup_A := Cup_B;

Cup_B := Spare_Cup;

© Longman Hong Kong Education Page6


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page7


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page8


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

Compare [2] and [3]


Step 2: 55 25 40 60
and swap

Compare [3] and [4]


Step 3: 55 40 25 60
and swap

© Longman Hong Kong Education Page9


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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)

Compare [2] and [3]


Step 2: 55 40 60 25
and swap

Compare [3] and [4]


Step 3: 55 60 40 25
(no swap)

© Longman Hong Kong Education Page10


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page11


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page12


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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]

Find the smallest of first


Pass 2: 60 55 40 25 three elements and swap it
with arr[3]
Find the smallest of first two
Pass 3: 60 55 40 25 elements and swap it with
arr[2]
sorted array
© Longman Hong Kong Education Page13
Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page14


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page15


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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]

Compare arr[2] and


Step 2: 25 55 40 60 target, stop if
target = arr[2]

© Longman Hong Kong Education Page18


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[3] and
Step 3: 25 55 40 60 target, stop if
target = arr[3]

Compare arr[4] and


Step 4: 25 55 40 60 target, stop if
target = arr[4]

unsuccessful search

© Longman Hong Kong Education Page19


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page21


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page22


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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

© Longman Hong Kong Education Page23


Computer and Information
Technology for (HKCEE)
Module A2: Part C

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.

© Longman Hong Kong Education Page25


Computer and Information
Technology for (HKCEE)
Module A2: Part C

8.6 Merging Two Sorted Arrays


Merging
to combine two sorted arrays together

initial situation

after merging

© Longman Hong Kong Education Page26


Computer and Information Technology for (HKCEE) Module A2

END

You might also like