Top Banner
Adam Mukharil Bachtiar English Class Informatics Engineering 2011 Algorithms and Programming Searching
26

Algorithm and Programming (Searching)

Mar 20, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Algorithm and Programming (Searching)

Adam Mukharil BachtiarEnglish Class

Informatics Engineering 2011

Algorithms and Programming

Searching

Page 2: Algorithm and Programming (Searching)

Steps of the Day

Let’s Start

1Definition of Searching 2Sequential Search

3Binary

Search

Page 3: Algorithm and Programming (Searching)

Definition of Searching

All About Searching

1

Page 4: Algorithm and Programming (Searching)

Wha

t is S

earc

hing

Process that search the value in group of data.

This process can produce FOUND or NOT FOUND

value.

Page 5: Algorithm and Programming (Searching)

Algo

rithm

s of S

ortin

g • Sequential search / Linear search

• Binary search

Page 6: Algorithm and Programming (Searching)

2 Sequential Search

Definition and Structures of Sequential Search

Page 7: Algorithm and Programming (Searching)

Wha

t is S

eque

ntial

Sea

rch

• Trace group of data one by one.

• Start the process from the first data.

• If the data was found in group then stop

the searching but if not, search until the

last data in grup.

Page 8: Algorithm and Programming (Searching)

Met

hods

in S

eque

ntial

Sea

rch

• Without boolean

Without sentinel

Use sentinel

• Use boolean

Page 9: Algorithm and Programming (Searching)

Ilustration of Seq. Search Without Sentinel

Given an array to be processed:

Number

Data that want to be sought : 9• Number[1] = 9? • Number[2] = 9? • Number[3] = 9?

Result: 9 is found in number[3]

5 1 9 4 2[1] [2] [3] [4] [5]

i i + 1i i + 1

i (STOP SEARCH)

Page 10: Algorithm and Programming (Searching)

Sequential Search Without Sentinel

12345678910111213141516171819

Procedure SeqSearchTanpaSentinel (Input nama_array:tipe_array){I.S. : elemen array [1..maks_array] sudah terdefinisi}{F.S. : menampilkan hasil pencarian (ditemukan/tidak)}Kamus:

i : integerdata_cari : tipedata

Algoritma:input(data_cari)i 1while(nama_array [i] ≠ data_cari) and (i < maks_array) do

i i + 1endwhileif (nama_array[i] = data_cari)then

output(data_cari,’ ditemukan pada indeks ke-’,i)else

output(data_cari,’ tidak ditemukan’)endif

EndProcedure

Page 11: Algorithm and Programming (Searching)

Sequ

entia

l Sea

rch

Use

Sen

tinel

• Place the data that want to be sought in

sentinel.

• Sentinel is additional index that was placed in

max array + 1.

• If the data is found in sentinel that means the

result is data is not found and vice versa.

Page 12: Algorithm and Programming (Searching)

Ilustration of Seq. Search Use Sentinel

Data that was sought: 9

Number

Result: Data was found in Number[3]

Data that was sought: 10

Number

Result: Data was not found

5 1 9 4 2 9[1] [2] [3] [4] [5] [6]

sentinel

5 1 9 4 2 10[1] [2] [3] [4] [5] [6]

sentinel

Page 13: Algorithm and Programming (Searching)

Sequential Search Use Sentinel

1234567891011121314151617181920

Procedure SeqSearchSentinel (Input nama_array:tipe_array){I.S. : elemen array [1..maks_array] sudah terdefinisi}{F.S. : menampilkan hasil pencarian (ditemukan/tidak)}Kamus:

i : integerdata_cari : tipedata

Algoritma:input(data_cari)i 1nama_array(maks_array + 1) data_cariwhile (nama_array [i] ≠ data_cari) do

i i + 1endwhileif (i < maks_array+1)then

output(data_cari,’ ditemukan pada indeks ke-’,i)else

output(data_cari,’ tidak ditemukan’)endif

EndProcedure

Page 14: Algorithm and Programming (Searching)

Sequ

entia

l Sea

rch

Use

Boo

lean

• Its searching process is similar with another

sequential search method.

• Involves one boolean variable.

Page 15: Algorithm and Programming (Searching)

Ilustration of Seq. Search Use Boolean

Given an array to be processed:

Number

Data that want to be sought : 9• Number[1] = 9? • Number[2] = 9? • Number[3] = 9?

Result: 9 is found in number[3]

5 1 9 4 2[1] [2] [3] [4] [5]

FOUND FALSEFOUND FALSE

FOUND TRUE (STOP SEARCH)

Page 16: Algorithm and Programming (Searching)

Sequential Search Use Sentinel1234567891011121314151617181920212223242526

Procedure SeqSearchBoolean (Input nama_array:tipe_array){I.S. : elemen array [1..maks_array] sudah terdefinisi}{F.S. : menampilkan data yg dicari ditemukan atau tidak ditemukan}Kamus:

i : integerketemu : booleandata_cari : tipedata

Algoritma:input(data_cari)i 1ketemu falsewhile (not ketemu) and (i ≤ maks_array) do

if(nama_var_array(i) = data_cari)then ketemu trueelse i i + 1endif

endwhileif (ketemu)then

output(data_cari,’ ditemukan pada indeks ke-’,i)else

output(data_cari,’ tidak ditemukan’)endif

EndProcedure

Page 17: Algorithm and Programming (Searching)

3 Binary Search

Definition and Structures of Binary Search

Page 18: Algorithm and Programming (Searching)

Wha

t is B

inar

y Se

arch

• Searching algorithm that divide group of data into

two parts (left and right).

• First, check data in the middle. If same with the data

that was sought then data is found. If not then

continue searching process to left or right (based on

condition).

• Group of data must be sorted before the searching

process.

Page 19: Algorithm and Programming (Searching)

Data that was sought: 7

Number

Result: ?

Case

Exa

mpl

e of

Bin

ary

Sear

ch

3 7 12 15 29[1] [2] [3] [4] [5]

Page 20: Algorithm and Programming (Searching)

Case Example of Binary Search

Data that was sought: 7

Number

Result: ?

3 7 12 15 29[1] [2] [3] [4] [5]

Page 21: Algorithm and Programming (Searching)

Case Example of Binary Search

Step 1 : Divide array into 2 parts. Count the middle position (k) of array to start searchingk = (Ia + Ib) div 2 = (1 + 5) div 2 = 3

la : lower bound (for index) lb : upper bound (for index)

3 7 12 15 29[1] [2] [3] [4] [5]

Ia k Ib

Left Side Right Side

Page 22: Algorithm and Programming (Searching)

Case Example of Binary Search

Step 2 :

• check data in k. If it’s same with data that was sought then

stop search and data is found.

• If it’s not then check whether data was bigger or smaller than

data in k.

• If it’s bigger one then continue searching to right side and la

= k+1. if it’s smaller one then continue searching to the left

side and lb = k-1 (data wa sorted in ascending way).

3 71 2

Ia Ib

Page 23: Algorithm and Programming (Searching)

Case Example of Binary Search

Step 3 : repeat step 1 until step 2 until data is found or until

la>lb then stop searching.

Result : 7 is found in Number[2] and in the third looping.

3 71 2

Ia Ibk

Left Side Right Side

Page 24: Algorithm and Programming (Searching)

Binary Search1234567891011121314151617181920212223242526

Procedure BinarySearch (Input nama_array : tipe_array){I.S. : elemen array yang terurut secara ascending sudah terdefinisi}{F.S. : menampilkan data yg dicari ditemukan atau tidak ditemukan}Kamus:

Ia, Ib, k : integerketemu : booleandata_cari : tipedata

Algoritma:input(data_cari)Ia 1Ib maks_arrayketemu falsewhile (not ketemu) and (Ia ≤ Ib) do

k (Ia + Ib) div 2if (nama_var_array[k] = data_cari) then ketemu true else if (nama_var_array[k] < data_cari) then Ia k + 1 else Ib k – 1 endifendif

endwhile

Page 25: Algorithm and Programming (Searching)

Binary Search

27282930313233

if (ketemu) then

output(data_cari,’ ditemukan pada indeks ke-’,k) else

output(data_cari,’ tidak ditemukan’)endif

EndProcedure

Page 26: Algorithm and Programming (Searching)

Contact Person:Adam Mukharil Bachtiar

Informatics Engineering UNIKOMJalan Dipati Ukur Nomor. 112-114 Bandung 40132

Email: [email protected]: http://adfbipotter.wordpress.com

Copyright © Adam Mukharil Bachtiar 2011

GRACIASTHANK YOU