Anda di halaman 1dari 59

KONSEP ALGORITMA Dosen Pengajar:

Widha Kusumaningdyah, PhD


Memahami konsep dan sejarah algoritma
TUJUAN Memahami evolusi Bahasa pemrograman

PEMBELAJARAN Memahami kriteria kualitas pemrograman


SEJARAH PERADABAN MANUSIA
Periode Pra Mekanik
Periode Mekanik
Periode Elektro Mekanik
Periode Elektronik
PERIODE PRA-MEKANIK
oManusia Saling bertukar Informasi sejak oSalah satu penemuan terpenting adalah
5000 tahun yang lalu ditemukannya angka modern
oKomunikasi menggunakan Simbol dan
Gambar

ANGKA MODERN ROMAN ABACUS

PAPYROUS HIEROGLIPH
PERIODE MEKANIK
Masa ini dimulai pada zaman PASCALINE
pertengahan
Kemunculan periode ini ditandai dengan
adanya alat untuk
menyimpan,mengolah,dan merekam
informasi
Penemuan terpenting pada periode ini
adalah Penemuan pascaline oleh Blaise
Pascal di tahun 1600
PENEMUAN PENTING ERA MEKANIK

MESIN CETAK GUTENBERG MESIN ANALITIK


PERIODE ELEKTRO MEKANIK
Kemunculan Periode ini ditandai dengan
digunakannya tenaga listrik sebagai
sumber energi utama
Pada masa ini sudah memungkinkan
untuk melakukan komunikasi jarak jauh
PENEMUAN ERA ELEKTRO MEKANIK

Telephone Radio

https://www.youtube.com/watch?v=ixFK0LycQVg&t=80s
PERIODE ELEKTRONIK
Dimulai pada tahun 1957 yaitu ketika
Jean Hoerni menemukan transistor
Planar
Alat ini mampu mengintegrasikan sirkuit
yang ditemukannya setelahnya
KOMPUTER PERTAMA DI DUNIA

Manchester Mark 1
 Dibuat tahun 1948
Tingginya 2,4 meter
Lebarnya 17 meter
ALGORITMA
11
DEFINISI ALGORITMA
Algoritma adalah urutan langkah logis tertentu untuk memecahkan suatu masalah
 Urutan langkah logis, yang berarti algoritma harus mengikuti suatu urutan tertentu, tidak boleh
melompat-lompat

Alur pemikiran dalam menyelesaikan suatu pekerjaan yang dituangkan secara


tertulis.
 Alur pikiran, sehingga algoritma seseorang dapat juga berbeda dari algoritma orang lain
 Tertulis, yang artinya dapat berupa kalimat, gambar, atau tabel tertentu

12
DEFINISI ALGORITMA
Deretan langkah-langkah komputasi yang mentransformasikan data masukan
menjadi keluaran
Deretan instruksi yang jelas untuk memecahkan masalah, yaitu untuk memperoleh
keluaran yang diinginkan dari suatu masukan dalam jumlah waktu yang terbatas
Prosedur komputasi yang terdefinisi dengan baik yang menggunakan beberapa nilai
sebagai masukan dan menghasilkan beberapa nilai yang disebut keluaran.

13
SEJARAH ALGORITMA
Asal kata algoritma berasal dari nama Abu Ja’far
Mohammed Ibn Musa al-Khowarizmi
 Ilmuan Persia yang menulis kitab al jabar wal-muqabala (the book of
restoration and reduction)
 Tahun 825 M

Perubahan kata
 Algorism  proses menghitung dengan angka Arab
 Algorist  orang yang menggunakan angka Arab
 Algorithm  metode perhitungan secara umum

Tahun 1950  euclid’s algorithm


 Langkah-langkah menemukan pembagi bersama terbesar (common
greatest divisor = gcd)

14
WHY WE MUST STUDY ALGORITHMS ?

15
CIRI ALGORITMA (DONALD E. KNUTH)
Finiteness, algoritma harus berhenti setelah mengerjakan sejumlah langkah terbatas
Definiteness, setiap langkah harus didefinisikan secara tepat dan tidak berarti-dua
(ambiguous)
Algoritma memiliki nol atau lebih masukan (input)
Algoritma memiliki nol atau lebih keluaran (output)
Effectiveness, algoritma harus efektif, setiap langkah harus sederhana sehingga
dapat dikerjakan dalam sejumlah waktu yang masuk akal

16
ALGORITHM -- EXAMPLES

A COOKING ASSEMBLY THE RULES OF VCR DESCRIPTION OF


RECIPE INSTRUCTIONS HOW TO PLAY A INSTRUCTIONS A MARTIAL ARTS
FOR A MODEL GAME TECHNIQUE

DIRECTIONS FOR A KNITTING A CAR REPAIR


DRIVING FROM A PATTERN MANUAL
TO B
17
ALMOND AND HONEY SLICE
1/2 quantity Shortcrust Pastry Preheat Preheat oven for 200° C

185 g unsalted butter Line Line a 30 cm ´ 20 cm baking tray with baking paper, and
then with pastry

100 g castor sugar Bake Bake blind for 20 minutes, then remove weights and foil

5 tablespoons honey Turn Turn oven up to 220° C.

50 ml cream Bring Bring remaining ingredients to a boil, stirring.

50 ml brandy or any other liqueur or


Spread Spread evenly over pastry.
spirit
From: Stephanie Alexander, The Cook’s Bake until topping is bubbling and has caramelised evenly,
300 g flaked almonds Bake about 15 minutes.
Companion, Viking/Penguin,
Ringwood, Victoria, 1996, p. 349. Cool Cool before cutting into fingers or squares.

18
ALMOND AND HONEY SLICE
1/2 quantity Shortcrust Pastry Preheat Preheat oven for 200° C

185 g unsalted butter are given in


Instructions
Line Line a 30 cm ´ 20 cm baking tray with baking paper, and
then with pastry

100 g castor sugar in which they


the order Bake Bake blind for 20 minutes, then remove weights and foil

are performed
5 tablespoons honey Turn Turn oven up to 220° C.
(“executed”)
50 ml cream Bring Bring remaining ingredients to a boil, stirring.

50 ml brandy or any other liqueur or


Spread Spread evenly over pastry.
spirit
From: Stephanie Alexander, The Cook’s Bake until topping is bubbling and has caramelised evenly,
300 g flaked almonds Bake about 15 minutes.
Companion, Viking/Penguin,
Ringwood, Victoria, 1996, p. 349. Cool Cool before cutting into fingers or squares.

19
CORRECT ALGORITHM?
Dalgona Coffe CARA MEMBUAT
BAHAN-BAHAN •Siapkan wadah dan whisk untuk mengocok
1 porsi •Dalam wadah tersebut, tuang 2 sachet nescaffe
classic, beri 2 sdm gula pasir, dan 2 sdm air.
Bahan utama :
•Kocok menggunakan whisk hingga tercampur rata
2 sachet Nescaffe Classic
•Kocok terus hingga mengembang dan berbentuk
2 sdm gula pasir (boleh ditambah jika suka manis) foam. (sekitar 10 menit)
2 sdm air •Siapkan gelas. Beri es batu.
Bahan tambahan : •Tuang susu full cream ke dalam gelas tersebut. (saya
1/2 kotak atau sekitar 125ml)
5 sdm Es batu
•Lalu, beri adonan kopi yang tadi sudah dibuat
125 ml susu full cream (saya greenfields)
•Sajikan bersama topping kesukaan. So simple ye
Topping (susu bubuk, messes, creamer, dll) maakk ngopi ala caffe😊😊😊
Sumber: Cookpad App
Reka ( Bundanya Rere )
@rekadst MUARA BUNGO, JAMBI / Semarang, JAWA TENGAH
CORRECT ALGORITHM ???...

21
CORRECT ALGORITHM

22
COMPONENTS OF AN ALGORITHM
A. Values
B. Variables
C. Instructions
1. Simple and unambiguous
2. Sequences
3. Functions
4. Selections
5. Repetitions
D. Documentation
23
A. VALUES
Represent quantities, amounts or measurements
May be numerical or alphabetical
(or other things)
Often have a unit related to their purpose
Example:
 Recipe ingredients

24
B. VARIABLES
Are containers for values – places to store values
Example:
Variable Values

This jar
10 cookies
can contain 50 grams of sugar
3 slices of cake
etc.
25
RESTRICTIONS ON VARIABLES
Variables may be restricted to contain a specific type of value

26
C. INSTRUCTIONS
Some action that is
simple
unambiguous
that the system knows about...
...and should be able to actually do

27
INSTRUCTIONS – EXAMPLES
Take off your shoes
Count to 10
Directions to perform
Cut along dotted line
specific actions on values
Knit 1
and variables
Purl 2
Pull rip-cord firmly
Shift 10 grams of arsenic

28
INSTRUCTIONS - APPLICATION
Some instructions can only be applied to a specific type of values or variables
Examples:

29
INSTRUCTIONS – SIMPLE AND
UNAMBIGUOUS
When writing an algorithm, make each instruction simple and unambiguous
Example:

Cut chicken into pieces and Cut chicken into pieces.


brown the pieces on all Heat olive oil in a casserole
sides in a casserole dish dish.
in hot olive oil. Brown the chicken pieces in
the casserole dish.

30
INSTRUCTIONS - SEQUENCES
A “sequence” of simple
When writing an algorithm, make each instruction simple and unambiguous
instructions
Example:

Cut chicken into pieces and Cut chicken into pieces.


brown the pieces on all Heat olive oil in a casserole
sides in a casserole dish dish.
in hot olive oil. Brown the chicken pieces in
the casserole dish.

31
INSTRUCTIONS - SEQUENCES
A series of instructions
...to be carried out one after the other...
...without hesitation or question
Example:
How to cook a Gourmet MealTM

32
INSTRUCTIONS - SEQUENCES
suatu struktur program dimana setiap
baris program akan dikerjakan secara
urut dari atas ke bawah sesuai dengan
urutan penulisannya.
instruksi baris program 2 akan
dikerjakan jika instruksi baris program 1
telah selesai dikerjakan.

33
INSTRUCTIONS - FUNCTION
A named sequence of instructions
So that you can
 Refer to it collectively (by name)
 ...instead of individually (by each instruction in the sequence)
Example:
 Print_sentence
def Print_sentence():
print (“heyyow world”)

 Drive_To_Uni

34
INSTRUCTIONS – EXAMPLE OF
FUNCTION
def Drive_To_Uni (): etc… etc….
{ 1. find car keys 52. find parking space
53. pull into parking space
2. disable car alarm
54. turn off engine
3. open car door
55. remove keys from ignition
4. get in car 56. open car door
5. shut car door 57. get out
6. put keys in ignition 58. shut car door
7. start car 59. lock car door
60. enable alarm
8. back car out of driveway
}
9. drive to end of street
...etc...etc...etc
35
INSTRUCTIONS - SELECTION
An instruction that decides which of two possible sequences is
executed
The decision is based on a single true/false condition
Examples:
 Car repair
 Reciprocals

36
INSTRUCTIONS – EXAMPLE OF SELECTION
if (motor turns)
then:
{ CheckFuel
CheckSparkPlugs
CheckCarburettor }
else
{ CheckDistributor
CheckIgnitionCoil }

37
SELECTION EXAMPLE – CAR REPAIR
if (motor turns)
then
{ Should be a
CheckFuel true or false
CheckSparkPlugs
condition
CheckCarburettor
}
else
{
CheckDistributor
CheckIgnitionCoil
}

38
SELECTION EXAMPLE – CAR REPAIR
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
Sequence if
CheckCarburettor the condition
} is true
else
{
CheckDistributor
CheckIgnitionCoil
}

39
SELECTION EXAMPLE – CAR REPAIR
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor Sequence if the
}
else
condition is
{ false
CheckDistributor
CheckIgnitionCoil
}

40
STRUKTUR PEMILIHAN (SELECTION),
Menghasilkan nilai benar (true) atau nilai
salah (false).
IF sederhana
 IF <syarat> THEN
<instruksi>

Pada bentuk IF sederhana ini, intruksi akan


dikerjakan jika syarat yangdiuji benilai
benar (true).
Jika syarat yang diuji benilai salah (false)
maka tidak ada instruksi yang dikerjakan.

41
IF BERSARANG (NESTED IF)
IF <syarat1> THEN
<instruksi1>
ELSE IF <syarat2> THEN
<instruksi2>
ELSE IF <syarat3> THEN
<instruksi3>
ELSE IF <syaratm> THEN
<instruksim>
ELSE
<Instruksin>

42
INSTRUCTIONS – REPETITION
Repeat an instruction...
...while (or maybe until) some true or false condition
occurs
Test the condition each time before repeating the
instruction
Also known as iteration or loop
Example:
Algorithm for getting a date
43
INSTRUCTIONS – EXAMPLE OF REPETITION
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
add 1 to begging count
ListenToReply ( )
}
}
44
REPETITION – EXAMPLE (CONT)
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )
start begging count at zero
while ( reply is "No" and begging count < 100 )
{
Say("Oh please!")
add 1 to begging count
Condition is tested
ListenToReply ( ) before sequence
}
}

45
REPETITION – EXAMPLE (CONT)
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
add 1 to begging count Sequence may not
ListenToReply ( ) get executed at all
}
}
46
REPETITION – EXAMPLE (CONT)
function AskOnDate ( name, time, location )
{
Ensure initial
Phone(name) values of variables
used in the
Say("Hey", name, "it's your lucky day!")
conditions
Say("Wanna come to", location, "at", time, "?") are set
ListenToReply ( )
start begging count at zero
correctly
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
add 1 to begging count
ListenToReply ( )
}
}

47
REPETITION – EXAMPLE (CONT)
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
add 1 to begging count Ensure the variables
ListenToReply ( )
used in the conditions
} are updated in each
} repetition
48
REPETITION – EXAMPLE (CONT)
• What if we don’t increment the begging count?
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
} Infinite loop
}
49
STRUKTUR FOR PADA REPETITION
Struktur for
Mengulang satu baris instruksi atau satu
blok instruksi sampai jumlah perulangan
yang disyaratkan terpenuhi.
Ciri utama terdapat nilai awal dan nilai
akhir yang menunjukkan banyaknya
pengulangan yang akan dilakukan.

50
STRUKTUR WHILE PADA REPETITION
Untuk mengulang satu baris instruksi
atau satu blok baris instruksi selama
syarat yang diberikan masih terpenuhi.
Syarat akan diuji didepan.
Jika syarat yang diuji bernilai benar
maka instruksi1, instruksi2, instruksi3
akan dikerjakan, setelah mengerjakan
instruksi1, instruksi2, instruksi3 maka
syarat akan diuji lagi.
pengulangan akan berhenti jika syarat
yang diuji bernilai salah.
51
D. DOCUMENTATION
Records what the algorithm does
Describes how it does it
Explains the purpose of each component of the algorithm
Notes restrictions or expectations
Example:
 Getting a date (again)

52
DOCUMENTATION - EXAMPLE
Think of something romantic to do
decide on time and location
Work through address book to look for a person
initialise booking to “unsuccessful”
until (successfully booked)
{
get next Name in little black book
AskOnDate(Name, Time, Location)
DetermineBookingSuccess
}
Assumes that I will find someone in the book before it runs out
SighWithRelief
53
FROM ALGORITHMS TO
PROGRAMS
FROM ALGORITHMS TO PROGRAMS
Problem Algorithm: A sequence of instructions
describing how to do a task (or process)

Program

55
THE SOFTWARE DEVELOPMENT PROCESS
Define the problem clearly
Analyse the problem thoroughly
Design an algorithm carefully
Code the algorithm efficiently
Test the code thoroughly
Document the system lucidly

56
TOP-DOWN ALGORITHM DESIGN
Write down what you must do
Break that into 3-7 smaller steps
Break each step into 3-7 smaller steps
Keeping subdividing until each individual step is easy
enough to do
Example:
Learning
57
TOP-DOWN DESIGN - EXAMPLE
Read lecture notes
Read
Read textbook
Make notes
Prepare Prepare questions

Read exercise
Attend lecture
Design algorithm
Learn Listen and think
Study Complete practice
Code solution
Test and document

Record insights
Record answers to
Reinforce questions
Revise notes

58
CARA PENYAJIAN ALGORITMA

Uraian Deskriptif

• Menggunakan bahasa yang bisa di gunakan sehari-hari

Menggunakan flow chart (diagram alir)

Menggunakan pseudo-code

• Ditulis dalam kode-kode yang disepakati yang mempunyai arti

59

Anda mungkin juga menyukai