SlideShare a Scribd company logo
1 of 30
Download to read offline
Algorithm and
Data Structure
Andi Nurkholis, S.Kom, M.Kom
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2020-2021
May 10, 2021
2
6 Searching
3
What is Searching?
Searching for data stored in different data structures
is a crucial part of pretty much every single
application.
There are many different algorithms available to
utilize when searching, and each have different
implementations and rely on different data
structures to get the job done.
4
Searching
Algorithm
1) Linear Search
2) Binary Search
Being able to choose a specific algorithm
for a given task is a key skill for
developers and can mean the difference
between a fast, reliable and stable
application and an application that
crumbles from a simple request.
5
6.2 Binary Search
6
Binary Search
Binary Search is a searching algorithm for finding an element's
position in a sorted array.
In this approach, the element is always searched in the middle of a
portion of an array.
Binary search can be implemented only on a sorted array of items.
If the elements are not sorted already, we need to sort them first.
Steps of Binary Search
1. Start with the middle element:
a. If the target value is equal to the middle element of
the array, then return the index of the middle
element.
b. If not, then compare the middle element with the
target value,
• If the target value is greater than the number in
the middle index, then pick the elements to the
right of the middle index, and start with Step 1.
• If the target value is less than the number in the
middle index, then pick the elements to the left of
the middle index, and start with Step 1.
7
Steps of Binary Search (cont.)
2. When a match is found, return the index of the element
matched.
3. If no match is found, then return -1
8
Example
9
data_1 = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21}
Find value 15 in array data_1
data_2 = {1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31}
Find value 4 in array data_2
data_3 = {1, 5, 9, 13, 17, 21, 25, 29, 33, 37}
Find value 33 in array data_3
Answer 1
10
Low
1 3 5 7 9 11 13 15 17 19 21
Value searched for 15
Value in middle-index array is 11
Is value 15 = 11?
FALSE
Is value 15 > 11?
TRUE
Then Algorithm is CONTINUE
If value searched greater than
value in middle-index array, then
searching continue to the right
High
Mid
Step 1
11
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [1] changes to the
previous middle-index [11]. That is
because searching continues to the right
What is the meaning? The next high-index
[11] that changes will also cause the next
middle-index [15] to change
Answer 1 (cont.)
12
1 3 5 7 9 11 13 15 17 19 21
Step 2
Value searched for 15
Value in middle-index array is 15
Is value 15 = 15?
TRUE
Then Algorithm is STOP
Low High
Mid
13
Final Result
Value searched for 15 is FOUND
in step 2
Answer 2
14
Step 1
1 4 7 10 13 16 19 22 25 28 31
Value searched for 4
Value in middle-index array is 16
Is value 4 = 16?
FALSE
Is value 4 > 11?
FALSE
Then Algorithm is CONTINUE
If value searched less than value in
middle-index array, then searching
continue to the left
Low High
Mid
15
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [31] changes to the
previous middle-index [16]. That is
because searching continues to the left
What is the meaning? The next high-index
[16] that changes will also cause the next
middle-index [7] to change
Answer 2 (cont.)
16
1 4 7 10 13 16 19 22 25 28 31
Step 2
Value searched for 4
Value in middle-index array is 7
Is value 4 = 7? FALSE
Is value 4 > 7? FALSE
Then Algorithm is CONTINUE
Low High
Mid
If value searched less than value in
middle-index array, then searching
continue to the left
17
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [16] changes to the
previous middle-index [7]. That is because
searching continues to the left
What is the meaning? The next high-index
[7] that changes will also cause the next
middle-index [4] to change
Answer 2 (cont.)
18
1 4 7 10 13 16 19 22 25 28 31
Step 3
Value searched for 4
Value in middle-index array is 13
Is value 4 = 13? FALSE
Is value 4 > 11? FALSE
Then Algorithm is STOP
Low High
Mid
If value searched less than value in
middle-index array, then searching
continue to the left
19
Final Result
Value searched for 4 is FOUND
in step 3
Answer 3
20
Step 1
1 5 9 13 17 21 25 29 33 37
Value searched for 33
Value in middle-index array is 17
Is value 33 = 17? FALSE
Is value 33 > 17? TRUE
Then Algorithm is CONTINUE
If value searched greater than
value in middle-index array, then
searching continue to the right
For an even-number array, done
N/2, then N-1. For this case, 10/2
= 5, then 5-1 = 4. Therefore,
middle-index array is 4-index.
Low High
Mid
21
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [1] changes to the
previous middle-index [17]. That is
because searching continues to the right
What is the meaning? The next high-index
[17] that changes will also cause the next
middle-index [25] to change
Answer 3 (cont.)
22
1 5 9 13 17 21 25 29 33 37
Step 2
Value searched for 33
Value in middle-index array is 25
Is value 33 = 25? FALSE
Is value 33 > 25? TRUE
Then Algorithm is CONTINUE
If value searched greater than
value in middle-index array, then
searching continue to the right
Low High
Mid
23
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [17] changes to the
previous middle-index [25]. That is
because searching continues to the right
What is the meaning? The next high-index
[25] that changes will also cause the next
middle-index [29] to change
Answer 3 (cont.)
24
1 5 9 13 17 21 25 29 33 37
Step 3
Value searched for 33
Value in middle-index array is 29
Is value 33 = 29? FALSE
Is value 33 > 29? TRUE
Then Algorithm is CONTINUE
If value searched greater than
value in middle-index array, then
searching continue to the right
Low High
Mid
25
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [25] changes to the
previous middle-index [29]. That is
because searching continues to the right
What is the meaning? The next high-index
[29] that changes will also cause the next
middle-index [33] to change
Answer 3 (cont.)
26
1 5 9 13 17 21 25 29 33 37
Step 4
Value searched for 33
Value in middle-index array is 33
Is value 33 = 33? TRUE
Then Algorithm is STOP
If value searched greater than
value in middle-index array, then
searching continue to the right
Low High
Mid
27
Final Result
Value searched for 33 is FOUND
in step 4
Advantage of Linear Search
1. It eliminates half of the list from further searching by using the
result of each comparison.
2. For large array of data, it works significantly better than linear
search.
28
Disadvantage of Linear Search
1. The algorithm requires the array to be sorted first
2. In implementing, the algorithm more difficult
than linear search
29
Thank You, Next …
Sorting
May 10, 2021
Andi Nurkholis, S.Kom, M.Kom
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2020-2021

More Related Content

What's hot

Interactive powerpoint - matrices; Budkie
Interactive powerpoint - matrices; BudkieInteractive powerpoint - matrices; Budkie
Interactive powerpoint - matrices; BudkieElyse Budkie
 
Basic relation add math
Basic relation add mathBasic relation add math
Basic relation add mathRoiamah Basri
 
relational algebra-(basics)
 relational algebra-(basics) relational algebra-(basics)
relational algebra-(basics)Nilt1234
 
Learning goals ngsss math course 3
Learning goals ngsss math course 3Learning goals ngsss math course 3
Learning goals ngsss math course 3Taleese
 
Solving linear equations in two
Solving linear equations in twoSolving linear equations in two
Solving linear equations in twomstf mstf
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) inventionjournals
 
Object oriented programming15 control structures relational operators
Object oriented programming15 control structures relational operatorsObject oriented programming15 control structures relational operators
Object oriented programming15 control structures relational operatorsVaibhav Khanna
 
Eigenvalues for HIV-1 dynamic model with two delays
Eigenvalues for HIV-1 dynamic model with two delaysEigenvalues for HIV-1 dynamic model with two delays
Eigenvalues for HIV-1 dynamic model with two delaysIOSR Journals
 
Generalized Linear Models
Generalized Linear ModelsGeneralized Linear Models
Generalized Linear ModelsAvinash Chamwad
 
Final generalized linear modeling by idrees waris iugc
Final generalized linear modeling by idrees waris iugcFinal generalized linear modeling by idrees waris iugc
Final generalized linear modeling by idrees waris iugcId'rees Waris
 
Real numbers and their properties
Real numbers and their propertiesReal numbers and their properties
Real numbers and their propertieselearningunit2013
 
(8) Lesson 3.2
(8) Lesson 3.2(8) Lesson 3.2
(8) Lesson 3.2wzuri
 

What's hot (18)

Outlier managment
Outlier managmentOutlier managment
Outlier managment
 
Interactive powerpoint - matrices; Budkie
Interactive powerpoint - matrices; BudkieInteractive powerpoint - matrices; Budkie
Interactive powerpoint - matrices; Budkie
 
Basic relation add math
Basic relation add mathBasic relation add math
Basic relation add math
 
relational algebra-(basics)
 relational algebra-(basics) relational algebra-(basics)
relational algebra-(basics)
 
Lesson 1 introduction
Lesson 1   introductionLesson 1   introduction
Lesson 1 introduction
 
Learning goals ngsss math course 3
Learning goals ngsss math course 3Learning goals ngsss math course 3
Learning goals ngsss math course 3
 
Solving linear equations in two
Solving linear equations in twoSolving linear equations in two
Solving linear equations in two
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI)
 
Object oriented programming15 control structures relational operators
Object oriented programming15 control structures relational operatorsObject oriented programming15 control structures relational operators
Object oriented programming15 control structures relational operators
 
11:00 ABE Math THUR 4/26
11:00 ABE Math THUR 4/2611:00 ABE Math THUR 4/26
11:00 ABE Math THUR 4/26
 
7.01
7.017.01
7.01
 
Rational number
Rational numberRational number
Rational number
 
Math matrix
Math matrixMath matrix
Math matrix
 
Eigenvalues for HIV-1 dynamic model with two delays
Eigenvalues for HIV-1 dynamic model with two delaysEigenvalues for HIV-1 dynamic model with two delays
Eigenvalues for HIV-1 dynamic model with two delays
 
Generalized Linear Models
Generalized Linear ModelsGeneralized Linear Models
Generalized Linear Models
 
Final generalized linear modeling by idrees waris iugc
Final generalized linear modeling by idrees waris iugcFinal generalized linear modeling by idrees waris iugc
Final generalized linear modeling by idrees waris iugc
 
Real numbers and their properties
Real numbers and their propertiesReal numbers and their properties
Real numbers and their properties
 
(8) Lesson 3.2
(8) Lesson 3.2(8) Lesson 3.2
(8) Lesson 3.2
 

Similar to Algorithm and Data Structure - Binary Search

linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
ODD EVEN BASED BINARY SEARCH
ODD EVEN BASED BINARY SEARCHODD EVEN BASED BINARY SEARCH
ODD EVEN BASED BINARY SEARCHIAEME Publication
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 
A presentation on the comparison on complexity between
A presentation on the comparison on complexity betweenA presentation on the comparison on complexity between
A presentation on the comparison on complexity betweenJubayer Hasan
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingnikshaikh786
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3smruti sarangi
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET Journal
 
Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary SearchWinNie Sjr
 

Similar to Algorithm and Data Structure - Binary Search (20)

linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
ODD EVEN BASED BINARY SEARCH
ODD EVEN BASED BINARY SEARCHODD EVEN BASED BINARY SEARCH
ODD EVEN BASED BINARY SEARCH
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
A presentation on the comparison on complexity between
A presentation on the comparison on complexity betweenA presentation on the comparison on complexity between
A presentation on the comparison on complexity between
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Search Algprithms
Search AlgprithmsSearch Algprithms
Search Algprithms
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 
Searching
SearchingSearching
Searching
 
Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary Search
 

More from AndiNurkholis1

Mobile Programming - 10 Firebase
Mobile Programming - 10 FirebaseMobile Programming - 10 Firebase
Mobile Programming - 10 FirebaseAndiNurkholis1
 
Mobile Programming - 9 Profile UI, Navigation Basic and Splash Screen
Mobile Programming - 9 Profile UI, Navigation Basic and Splash ScreenMobile Programming - 9 Profile UI, Navigation Basic and Splash Screen
Mobile Programming - 9 Profile UI, Navigation Basic and Splash ScreenAndiNurkholis1
 
Mobile Programming - 8 Progress Bar, Draggable Music Knob, Timer
Mobile Programming - 8 Progress Bar, Draggable Music Knob, TimerMobile Programming - 8 Progress Bar, Draggable Music Knob, Timer
Mobile Programming - 8 Progress Bar, Draggable Music Knob, TimerAndiNurkholis1
 
Mobile Programming - 7 Side Effects, Effect Handlers, and Simple Animations
Mobile Programming - 7 Side Effects, Effect Handlers, and Simple AnimationsMobile Programming - 7 Side Effects, Effect Handlers, and Simple Animations
Mobile Programming - 7 Side Effects, Effect Handlers, and Simple AnimationsAndiNurkholis1
 
Mobile Programming - 6 Textfields, Button, Showing Snackbars and Lists
Mobile Programming - 6 Textfields, Button, Showing Snackbars and ListsMobile Programming - 6 Textfields, Button, Showing Snackbars and Lists
Mobile Programming - 6 Textfields, Button, Showing Snackbars and ListsAndiNurkholis1
 
Mobile Programming - 5 Stylling Text and State
Mobile Programming - 5 Stylling Text and StateMobile Programming - 5 Stylling Text and State
Mobile Programming - 5 Stylling Text and StateAndiNurkholis1
 
Mobile Programming - 4 Modifiers and Image Card
Mobile Programming - 4 Modifiers and Image CardMobile Programming - 4 Modifiers and Image Card
Mobile Programming - 4 Modifiers and Image CardAndiNurkholis1
 
Mobile Programming - 3 Rows, Column and Basic Sizing
Mobile Programming - 3 Rows, Column and Basic SizingMobile Programming - 3 Rows, Column and Basic Sizing
Mobile Programming - 3 Rows, Column and Basic SizingAndiNurkholis1
 
Algoritma dan Struktur Data (Python) - Struktur Data
Algoritma dan Struktur Data (Python) - Struktur DataAlgoritma dan Struktur Data (Python) - Struktur Data
Algoritma dan Struktur Data (Python) - Struktur DataAndiNurkholis1
 
Mobile Programming - 2 Jetpack Compose
Mobile Programming - 2 Jetpack ComposeMobile Programming - 2 Jetpack Compose
Mobile Programming - 2 Jetpack ComposeAndiNurkholis1
 
Mobile Programming - 1 Introduction
Mobile Programming - 1 IntroductionMobile Programming - 1 Introduction
Mobile Programming - 1 IntroductionAndiNurkholis1
 
Algoritma dan Struktur Data (Python) - Perulangan
Algoritma dan Struktur Data (Python) - PerulanganAlgoritma dan Struktur Data (Python) - Perulangan
Algoritma dan Struktur Data (Python) - PerulanganAndiNurkholis1
 
Algoritma dan Struktur Data (Python) - Percabangan
Algoritma dan Struktur Data (Python) - PercabanganAlgoritma dan Struktur Data (Python) - Percabangan
Algoritma dan Struktur Data (Python) - PercabanganAndiNurkholis1
 
Algoritma dan Struktur Data (Python) - Struktur I/O
Algoritma dan Struktur Data (Python) - Struktur I/OAlgoritma dan Struktur Data (Python) - Struktur I/O
Algoritma dan Struktur Data (Python) - Struktur I/OAndiNurkholis1
 
Algoritma dan Struktur Data (Python) - Notasi Algoritmik
Algoritma dan Struktur Data (Python) - Notasi AlgoritmikAlgoritma dan Struktur Data (Python) - Notasi Algoritmik
Algoritma dan Struktur Data (Python) - Notasi AlgoritmikAndiNurkholis1
 
Algoritma dan Struktur Data (Python) - Pengantar Algoritma
Algoritma dan Struktur Data (Python) - Pengantar AlgoritmaAlgoritma dan Struktur Data (Python) - Pengantar Algoritma
Algoritma dan Struktur Data (Python) - Pengantar AlgoritmaAndiNurkholis1
 
Algorithm and Data Structure - Queue
Algorithm and Data Structure - QueueAlgorithm and Data Structure - Queue
Algorithm and Data Structure - QueueAndiNurkholis1
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - StackAndiNurkholis1
 
Algorithm and Data Structure - Modular Programming
Algorithm and Data Structure - Modular ProgrammingAlgorithm and Data Structure - Modular Programming
Algorithm and Data Structure - Modular ProgrammingAndiNurkholis1
 
Algorithm and Data Structure - Array and Struct
Algorithm and Data Structure - Array and StructAlgorithm and Data Structure - Array and Struct
Algorithm and Data Structure - Array and StructAndiNurkholis1
 

More from AndiNurkholis1 (20)

Mobile Programming - 10 Firebase
Mobile Programming - 10 FirebaseMobile Programming - 10 Firebase
Mobile Programming - 10 Firebase
 
Mobile Programming - 9 Profile UI, Navigation Basic and Splash Screen
Mobile Programming - 9 Profile UI, Navigation Basic and Splash ScreenMobile Programming - 9 Profile UI, Navigation Basic and Splash Screen
Mobile Programming - 9 Profile UI, Navigation Basic and Splash Screen
 
Mobile Programming - 8 Progress Bar, Draggable Music Knob, Timer
Mobile Programming - 8 Progress Bar, Draggable Music Knob, TimerMobile Programming - 8 Progress Bar, Draggable Music Knob, Timer
Mobile Programming - 8 Progress Bar, Draggable Music Knob, Timer
 
Mobile Programming - 7 Side Effects, Effect Handlers, and Simple Animations
Mobile Programming - 7 Side Effects, Effect Handlers, and Simple AnimationsMobile Programming - 7 Side Effects, Effect Handlers, and Simple Animations
Mobile Programming - 7 Side Effects, Effect Handlers, and Simple Animations
 
Mobile Programming - 6 Textfields, Button, Showing Snackbars and Lists
Mobile Programming - 6 Textfields, Button, Showing Snackbars and ListsMobile Programming - 6 Textfields, Button, Showing Snackbars and Lists
Mobile Programming - 6 Textfields, Button, Showing Snackbars and Lists
 
Mobile Programming - 5 Stylling Text and State
Mobile Programming - 5 Stylling Text and StateMobile Programming - 5 Stylling Text and State
Mobile Programming - 5 Stylling Text and State
 
Mobile Programming - 4 Modifiers and Image Card
Mobile Programming - 4 Modifiers and Image CardMobile Programming - 4 Modifiers and Image Card
Mobile Programming - 4 Modifiers and Image Card
 
Mobile Programming - 3 Rows, Column and Basic Sizing
Mobile Programming - 3 Rows, Column and Basic SizingMobile Programming - 3 Rows, Column and Basic Sizing
Mobile Programming - 3 Rows, Column and Basic Sizing
 
Algoritma dan Struktur Data (Python) - Struktur Data
Algoritma dan Struktur Data (Python) - Struktur DataAlgoritma dan Struktur Data (Python) - Struktur Data
Algoritma dan Struktur Data (Python) - Struktur Data
 
Mobile Programming - 2 Jetpack Compose
Mobile Programming - 2 Jetpack ComposeMobile Programming - 2 Jetpack Compose
Mobile Programming - 2 Jetpack Compose
 
Mobile Programming - 1 Introduction
Mobile Programming - 1 IntroductionMobile Programming - 1 Introduction
Mobile Programming - 1 Introduction
 
Algoritma dan Struktur Data (Python) - Perulangan
Algoritma dan Struktur Data (Python) - PerulanganAlgoritma dan Struktur Data (Python) - Perulangan
Algoritma dan Struktur Data (Python) - Perulangan
 
Algoritma dan Struktur Data (Python) - Percabangan
Algoritma dan Struktur Data (Python) - PercabanganAlgoritma dan Struktur Data (Python) - Percabangan
Algoritma dan Struktur Data (Python) - Percabangan
 
Algoritma dan Struktur Data (Python) - Struktur I/O
Algoritma dan Struktur Data (Python) - Struktur I/OAlgoritma dan Struktur Data (Python) - Struktur I/O
Algoritma dan Struktur Data (Python) - Struktur I/O
 
Algoritma dan Struktur Data (Python) - Notasi Algoritmik
Algoritma dan Struktur Data (Python) - Notasi AlgoritmikAlgoritma dan Struktur Data (Python) - Notasi Algoritmik
Algoritma dan Struktur Data (Python) - Notasi Algoritmik
 
Algoritma dan Struktur Data (Python) - Pengantar Algoritma
Algoritma dan Struktur Data (Python) - Pengantar AlgoritmaAlgoritma dan Struktur Data (Python) - Pengantar Algoritma
Algoritma dan Struktur Data (Python) - Pengantar Algoritma
 
Algorithm and Data Structure - Queue
Algorithm and Data Structure - QueueAlgorithm and Data Structure - Queue
Algorithm and Data Structure - Queue
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - Stack
 
Algorithm and Data Structure - Modular Programming
Algorithm and Data Structure - Modular ProgrammingAlgorithm and Data Structure - Modular Programming
Algorithm and Data Structure - Modular Programming
 
Algorithm and Data Structure - Array and Struct
Algorithm and Data Structure - Array and StructAlgorithm and Data Structure - Array and Struct
Algorithm and Data Structure - Array and Struct
 

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 

Algorithm and Data Structure - Binary Search

  • 1. Algorithm and Data Structure Andi Nurkholis, S.Kom, M.Kom Study Program of Informatics Faculty of Engineering and Computer Science SY. 2020-2021 May 10, 2021
  • 3. 3 What is Searching? Searching for data stored in different data structures is a crucial part of pretty much every single application. There are many different algorithms available to utilize when searching, and each have different implementations and rely on different data structures to get the job done.
  • 4. 4 Searching Algorithm 1) Linear Search 2) Binary Search Being able to choose a specific algorithm for a given task is a key skill for developers and can mean the difference between a fast, reliable and stable application and an application that crumbles from a simple request.
  • 6. 6 Binary Search Binary Search is a searching algorithm for finding an element's position in a sorted array. In this approach, the element is always searched in the middle of a portion of an array. Binary search can be implemented only on a sorted array of items. If the elements are not sorted already, we need to sort them first.
  • 7. Steps of Binary Search 1. Start with the middle element: a. If the target value is equal to the middle element of the array, then return the index of the middle element. b. If not, then compare the middle element with the target value, • If the target value is greater than the number in the middle index, then pick the elements to the right of the middle index, and start with Step 1. • If the target value is less than the number in the middle index, then pick the elements to the left of the middle index, and start with Step 1. 7
  • 8. Steps of Binary Search (cont.) 2. When a match is found, return the index of the element matched. 3. If no match is found, then return -1 8
  • 9. Example 9 data_1 = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21} Find value 15 in array data_1 data_2 = {1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31} Find value 4 in array data_2 data_3 = {1, 5, 9, 13, 17, 21, 25, 29, 33, 37} Find value 33 in array data_3
  • 10. Answer 1 10 Low 1 3 5 7 9 11 13 15 17 19 21 Value searched for 15 Value in middle-index array is 11 Is value 15 = 11? FALSE Is value 15 > 11? TRUE Then Algorithm is CONTINUE If value searched greater than value in middle-index array, then searching continue to the right High Mid Step 1
  • 11. 11 At each step, the middle-index changes according to the direction of the search At next step, low-index [1] changes to the previous middle-index [11]. That is because searching continues to the right What is the meaning? The next high-index [11] that changes will also cause the next middle-index [15] to change
  • 12. Answer 1 (cont.) 12 1 3 5 7 9 11 13 15 17 19 21 Step 2 Value searched for 15 Value in middle-index array is 15 Is value 15 = 15? TRUE Then Algorithm is STOP Low High Mid
  • 13. 13 Final Result Value searched for 15 is FOUND in step 2
  • 14. Answer 2 14 Step 1 1 4 7 10 13 16 19 22 25 28 31 Value searched for 4 Value in middle-index array is 16 Is value 4 = 16? FALSE Is value 4 > 11? FALSE Then Algorithm is CONTINUE If value searched less than value in middle-index array, then searching continue to the left Low High Mid
  • 15. 15 At each step, the middle-index changes according to the direction of the search At next step, low-index [31] changes to the previous middle-index [16]. That is because searching continues to the left What is the meaning? The next high-index [16] that changes will also cause the next middle-index [7] to change
  • 16. Answer 2 (cont.) 16 1 4 7 10 13 16 19 22 25 28 31 Step 2 Value searched for 4 Value in middle-index array is 7 Is value 4 = 7? FALSE Is value 4 > 7? FALSE Then Algorithm is CONTINUE Low High Mid If value searched less than value in middle-index array, then searching continue to the left
  • 17. 17 At each step, the middle-index changes according to the direction of the search At next step, low-index [16] changes to the previous middle-index [7]. That is because searching continues to the left What is the meaning? The next high-index [7] that changes will also cause the next middle-index [4] to change
  • 18. Answer 2 (cont.) 18 1 4 7 10 13 16 19 22 25 28 31 Step 3 Value searched for 4 Value in middle-index array is 13 Is value 4 = 13? FALSE Is value 4 > 11? FALSE Then Algorithm is STOP Low High Mid If value searched less than value in middle-index array, then searching continue to the left
  • 19. 19 Final Result Value searched for 4 is FOUND in step 3
  • 20. Answer 3 20 Step 1 1 5 9 13 17 21 25 29 33 37 Value searched for 33 Value in middle-index array is 17 Is value 33 = 17? FALSE Is value 33 > 17? TRUE Then Algorithm is CONTINUE If value searched greater than value in middle-index array, then searching continue to the right For an even-number array, done N/2, then N-1. For this case, 10/2 = 5, then 5-1 = 4. Therefore, middle-index array is 4-index. Low High Mid
  • 21. 21 At each step, the middle-index changes according to the direction of the search At next step, low-index [1] changes to the previous middle-index [17]. That is because searching continues to the right What is the meaning? The next high-index [17] that changes will also cause the next middle-index [25] to change
  • 22. Answer 3 (cont.) 22 1 5 9 13 17 21 25 29 33 37 Step 2 Value searched for 33 Value in middle-index array is 25 Is value 33 = 25? FALSE Is value 33 > 25? TRUE Then Algorithm is CONTINUE If value searched greater than value in middle-index array, then searching continue to the right Low High Mid
  • 23. 23 At each step, the middle-index changes according to the direction of the search At next step, low-index [17] changes to the previous middle-index [25]. That is because searching continues to the right What is the meaning? The next high-index [25] that changes will also cause the next middle-index [29] to change
  • 24. Answer 3 (cont.) 24 1 5 9 13 17 21 25 29 33 37 Step 3 Value searched for 33 Value in middle-index array is 29 Is value 33 = 29? FALSE Is value 33 > 29? TRUE Then Algorithm is CONTINUE If value searched greater than value in middle-index array, then searching continue to the right Low High Mid
  • 25. 25 At each step, the middle-index changes according to the direction of the search At next step, low-index [25] changes to the previous middle-index [29]. That is because searching continues to the right What is the meaning? The next high-index [29] that changes will also cause the next middle-index [33] to change
  • 26. Answer 3 (cont.) 26 1 5 9 13 17 21 25 29 33 37 Step 4 Value searched for 33 Value in middle-index array is 33 Is value 33 = 33? TRUE Then Algorithm is STOP If value searched greater than value in middle-index array, then searching continue to the right Low High Mid
  • 27. 27 Final Result Value searched for 33 is FOUND in step 4
  • 28. Advantage of Linear Search 1. It eliminates half of the list from further searching by using the result of each comparison. 2. For large array of data, it works significantly better than linear search. 28
  • 29. Disadvantage of Linear Search 1. The algorithm requires the array to be sorted first 2. In implementing, the algorithm more difficult than linear search 29
  • 30. Thank You, Next … Sorting May 10, 2021 Andi Nurkholis, S.Kom, M.Kom Study Program of Informatics Faculty of Engineering and Computer Science SY. 2020-2021