Sequential and Binary

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Algorithm and

Programming II

Anggina Primanita, M.IT


anggina.primanita@gmail.com

Todays coverage
Searching algorithm
Sequential Search
Binary Search

Searching Algorithm
Searching is an effort to look for one
or more data based on specific
criteria
Searching is done to:
ensure that such value exists
count the amount of data with such
value

Searching

Some searching algorithms..

Sequential search
Binary search
non-linear searching algorithms

Searching process is terminated if


one of these conditions is fulfilled

data with such value is found


there is no more data to be examined

SEQUENTIAL SEARCH

Sequential search: algorithm


Procedure Sequential_Search (input L : Larik, input N:integer, input X :
integer, input/output IX:integer)
Declaration
I : integer
found: integer
Description
I <-- 1
found <-- -1
While (IN) and (found=-1) do
if(L[I]=X)
found=I
endif
I <-- I + 1
Endwhile
If (found=-1) Then
IX <-- 0 { data not found}
Else
IX <-- I {data found}
Endif

Procedure Sequential_Search (input L : Larik, input


N:integer, input X : integer, input/output IX:integer)
Declaration
I : integer
Description
I <-- 1
While (IN) and (L[I] X) do
I <-- I + 1
Endwhile
If (I = N) Then
IX <-- 0 { data not found}
Else
IX <-- I {data found}
Endif

Sequential Search: Optimized

Procedure Sequential_Search_Sorted (input L : Larik, input


N:integer, input X : integer, input/output IX:integer)
Declaration
I : integer
Description
I <-- 1
While (IN) and (L[I]X) do
I <-- I + 1
Endwhile
If (I != N) and (L[I]=X) Then
IX <-- 1 { data found}
Else
IX <-- 0 {data not found}
Endif

Sequential Search:
Algorithm

BINARY SEARCH

The guessing game

A
B
A
B
A
B
A
B
A
B

:
:
:
:
:
:
:
:
:
:

Think an integer number between 1-10


ok!
is it 8?
no, its less than 8
is it.. 5?
its more than 5
so, 7 it is?
nope, its less than 7
ok, 6?
true!

GUESSING GAME: Algorithm


function guess (input max: integer, input min: integer) --> integer
Declaration
guess : character
guess_numb : integer
more_or_less : character
Description
guess <-- n
while (guess = n) do // while the guessed number is wrong
guess_numb <-- random(max, min)
read(more_or_less) // m if more, l if less, e if equal
if (more_or_less = m)
min <-- guess_numb+1
else if (more_or_less = l)
max <-- guess_numb-1
else if (more_or_less = e)
guess <-- y
endif
endwhile
return guess_numb

Binary search
According to Wiki:

the search algorithm finds the position of a specified


input value within an array sorted by key value. In each
step, the algorithm compares the search key value with
the key value of the middle element of the array. If the
keys match, then a matching element has been found,
and its index, or position, is returned. Otherwise, if the
search key is less than the middle elements key, then,
the algorithm repeats its action on the sub-array to the
left of the middle element or, if the search key is greater,
on the sub-array to the right. If the remaining array to be
searched is empty, then, the key cannot be found in the
array and a special not found indication is returned

binary search: how it


looks
is 12 available within array?
4

12

19

22

46

90

99

100

200

10

la

lb

la = 1
lb = 10
k = la+lb / 2 = 5

la = 1
lb = 4
k = la+lb / 2 = 2

12

19

la

lb

binary search: how it


looks
9

12

19

la

lb

la = 2
lb = 4
k = la+lb / 2 = 3
12 is within the array

binary search: how it


looks
is 99 available within array?
4

12

19

22

46

90

99

100

200

10

la

lb

la = 1
lb = 10
k = la+lb / 2 = 5
46

90

99

100

200

10

la
la = 6
lb = 10
k = la+lb / 2 = 8

lb

binary search: algorithm


Procedure Binary_Search(input L : Larik, input N:integer, input X : integer, input/output IX:integer)
Declaration
Ia, Ib : Integer
K: Integer
found : boolean
Description
Ia <-- 1
Ib <-- N
found=false
while (not found) and (Ia<=Ib) do
k <-- (Ia+Ib) div 2
If (L[k] <-- X) then
found <-- true
else
if (L[k] < X) then
Ia=k+1
else
Ib=K-1
endif
endif
endwhile
if found = true then
IX <-- k
else
IX <-- 0
endif

exercise
Look at this following array
-2

10

80

110

213

400

672

750

900

112
3

how many steps does it take to find


the value 110 using:
sequential search
binary search

Assignment
Write your interpretation of:
sequential search, and
binary search

on C/C++ language
Collect through e-learning
<nim>_<nama>.c/cpp
ex: 0900000000_abcdefghijkl.c/cpp

Semesters Project: Learning


the language
Presentation will be done on the 13 th and
14th week.
Class is divided into __ groups
Task:
Each group must determine a programming
language they intend to learn (no group can
have the same language)
Each group present:
hello world
selection and repetition
sorting and searching algorithms

on their respective programming language

You might also like