Anda di halaman 1dari 3

MTES3113 Matematik Keputusan Jan 2018

1.2 Pengaturcaraan Komputer

Bahasa Pengaturcaraan mengandungi pernyataan yang melaksanakan tugasan secara berulang-ulang


(repetition). Dalam Bahasa BASIC pernyataan “for.......next” bertindak seperti berikut :

Contoh 1.1 :

a) FOR I = 1 TO 3 a) 1 b) 11
PRINT I 2 12
NEXT I
3 21
b) FOR I = 1 TO 2 22
FOR J = 1 TO 2
PRINT I,J
Keputusan dan hasil yang dicetak
NEXT J
NEXT I
adalah seperti di atas

Contoh 1.2

Tiket bas kadangkala dicetak dengan menggunakan nombor 4 digit bermula dari 0000 hingga 9999.
Untuk kanak-kanak nombor yang diperuntukkan adalah di mana nombor tersebut perlu ditambah digit
nya sehingga berjumlah 21. Berapa banyakkan jujukan atau susunan tiket dari nombor 0000 hingga
9999 ?
Algoritma berikut menunjukkan penyelesaian algoritma menggunakan Bahasa Pengaturcaraan Komputer
BASIC (***Cuba latihan ini sekurang – kurangnya 3 kitaran )
TOTAL =0 TOTAL =0 ; SUM = 0
FOR I = 1 TO 9 FOR I = 1 TO 9
FOR J = 0 TO 9 FOR J = 0 TO 9
FOR K = 0 TO 9 FOR K = 0 TO 9
FOR L = 0 TO 9 FOR L = 0 TO 9
SUM = I + J + K + L SUM = I + J + K + L
IF SUM = 21 THEN IF SUM = 21 THEN
TOTAL = TOTAL + 1 TOTAL = TOTAL + 1
NEXT L SUM = SUM + 1
NEXT K NEXT L
NEXT J SUM = SUM -9
NEXT I NEXT K
PRINT TOTAL SUM = SUM - 9
NEXT J
SUM = SUM - 9
NEXT I
PRINT TOTAL
MTES3113 Matematik Keputusan Jan 2018

Contoh 1.3
Algoritma berikut menunjukkan bagaimana mendapatkan nilai Faktor Sepunya Terbesar bagi dua nilai
intyeger (Highest Common Factor – HCF) seperti HCF bagi 24 dan 36 adalah 12.
1 Let A be the first integer and B be the second integer.
2 Divide B by A and round down to the nearest integer. Let Q
be the result.
3 Let R = B – (Q x A)
4 If R = 0 go to step 8
5 Let the new value of B be A.
6 Let the new value of A be R.
7 Go to step 2
8 Record the HCF as the value of A.
9 Stop

Contoh 1.4
Carta alir berikut menunjukkan algoritma bagi operasi yang melibatkan dua input , x dan y.

start

Read x and y
1

2 Let r = y and q
=0

NO
3 r<x Let r = r – x
4
Let q = q + 1
YES and q = 0

5 Print q and r

Stop

a) Laksanakan algoritma ini dengan input x = 3 dan y = 41, kirakan berapa kali arahan dalam
kotak nombor 4 dilaksanakan secara berulang ulang ?
Catatkan apakah nilai yang dicetak bagi q , nilai r dan bilangan ulangan yang berlaku untuk
kotak nombor 4.
b) apakah nilai akhir yang diperolehi oleh algoritma ini ?
MTES3113 Matematik Keputusan Jan 2018

2.3 Perbezaan antara pencarian linear dan pencarian binari

1. A linear search looks down a list, one item at a time, without jumping. In complexity terms this is an
O(n) search - the time taken to search the list gets bigger at the same rate as the list does.
2. A binary search is when you start with the middle of a sorted list, and see whether that's greater than or
less than the value you're looking for, which determines whether the value is in the first or second half of
the list. Jump to the half way through the sublist, and compare again etc. This is pretty much how humans
typically look up a word in a dictionary (although we use better heuristics, obviously - if you're looking for
"cat" you don't start off at "M"). In complexity terms this is an O(log n) search - the number of search
operations grows more slowly than the list does, because you're halving the "search space" with each
operation.
As an example, suppose you were looking for U in an A-Z list of letters (index 0-25; we're looking for the
value at index 20).

A linear search would ask:

list[0] == 'U'? No.


list[1] == 'U'? No.
list[2] == 'U'? No.
list[3] == 'U'? No.
list[4] == 'U'? No.
list[5] == 'U'? No.
... list[20] == 'U'? Yes. Finished.

The binary search would ask:

Compare list[12] ('M') with 'U': Smaller, look further on. (Range=13-25)
Compare list[19] ('T') with 'U': Smaller, look further on. (Range=20-25)
Compare list[22] ('W') with 'U': Bigger, look earlier. (Range=20-21)
Compare list[20] ('U') with 'U': Found it! Finished.

Comparing the two:

 Binary search requires the input data to be sorted; linear search doesn't
 Binary search requires an ordering comparison; linear search only requires equality comparisons
 Binary search has complexity O(log n); linear search has complexity O(n) as discussed earlier
 Binary search requires random access to the data; linear search only requires sequential access
(this can be very important - it means a linear search can stream data of arbitrary size)

Anda mungkin juga menyukai