SlideShare a Scribd company logo
Linear search
Linear search is a very simple search algorithm. In this type of search, a search is made over one
by one. Every item is checked and if a match is found then that particular item is returned, otherwise the
search continues till the end of the data collection.
How Linear search works ?
Algorithm :
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Pseudocode :
procedure linear_search (list, value)]
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
Binary search
Binary search is fast search algorithm withcomplexity of Ο(log n).
How Binary search works ?
For a binary search to work, it is necessary for the target array to be sorted. We learn the
process of binary search with a example. The following is our sorted array and let us assume that we
need to search the location of value 31 using binary search.
10 14 19 25 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
First, we shall determine half of the array by using : mid = low + (high - low) / 2
Ans = 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
10 14 19 25 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
Now compare the value s at location 4, We find that the value at location 4 is 27, which is not a match.
As the value is greater than 27 and we have a sorted array, so we know that the target value must be in
the upper portion of the array.
10 14 19 25 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
We change the new mid value again. Our new mid is 7 now. We compare the value stored at location 7
with our target value 31.
10 14 19 25 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
The value at location 7 is not match, rather it is more than what we want. So, the value must be in the
lower part from this location.
calculate the mid again. This time it is 5.
10 14 19 25 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
compare the value at location 5 with our target value. it is a match already.
The target value 31 is stored at location 5.
Pseudocode
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure

More Related Content

What's hot

Writing Function Rules
Writing Function RulesWriting Function Rules
Writing Function Rules
Bitsy Griffin
 
2.1 Functions and Their Graphs
2.1 Functions and Their Graphs2.1 Functions and Their Graphs
2.1 Functions and Their Graphs
hisema01
 
My presentation
My presentationMy presentation
My presentation
Saifur13
 
ROOTS AND RADICALS - ELEMENTARY ALGEBRA
ROOTS AND RADICALS - ELEMENTARY ALGEBRAROOTS AND RADICALS - ELEMENTARY ALGEBRA
ROOTS AND RADICALS - ELEMENTARY ALGEBRA
Meutiah Nahrisyah
 
1.4 Functions
1.4 Functions1.4 Functions
1.4 Functions
nicksuf
 
Types of functions
Types of functionsTypes of functions
Types of functions
Maaz21
 
Power of Power Exponent Rule
Power of Power Exponent RulePower of Power Exponent Rule
Power of Power Exponent Rule
Passy World
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Rearranging Formulas
Rearranging FormulasRearranging Formulas
Rearranging Formulas
Passy World
 
Module 2 topic 1 notes
Module 2 topic 1 notesModule 2 topic 1 notes
Module 2 topic 1 notes
chrystal_brinson
 
Exponents Rules
Exponents RulesExponents Rules
Exponents Rules
BigPassy2
 
Function and graphs
Function and graphsFunction and graphs
Function and graphs
Rione Drevale
 
Logarithms
LogarithmsLogarithms
Logarithms
srobbins4
 
Lesson 1.3 Powerpoint
Lesson 1.3 PowerpointLesson 1.3 Powerpoint
Lesson 1.3 Powerpoint
Aaron
 
Types of functions, domain, range
Types of functions, domain, rangeTypes of functions, domain, range
Types of functions, domain, range
Richard Coleman
 
Radix sorting
Radix sortingRadix sorting
Radix sorting
Madhawa Gunasekara
 
Nikit
NikitNikit
Hashing
HashingHashing
Hashing
Ghaffar Khan
 
Lecture4
Lecture4Lecture4
Lecture4
Mohamed Awni
 
7th pre alg -l41
7th pre alg -l417th pre alg -l41
7th pre alg -l41
jdurst65
 

What's hot (20)

Writing Function Rules
Writing Function RulesWriting Function Rules
Writing Function Rules
 
2.1 Functions and Their Graphs
2.1 Functions and Their Graphs2.1 Functions and Their Graphs
2.1 Functions and Their Graphs
 
My presentation
My presentationMy presentation
My presentation
 
ROOTS AND RADICALS - ELEMENTARY ALGEBRA
ROOTS AND RADICALS - ELEMENTARY ALGEBRAROOTS AND RADICALS - ELEMENTARY ALGEBRA
ROOTS AND RADICALS - ELEMENTARY ALGEBRA
 
1.4 Functions
1.4 Functions1.4 Functions
1.4 Functions
 
Types of functions
Types of functionsTypes of functions
Types of functions
 
Power of Power Exponent Rule
Power of Power Exponent RulePower of Power Exponent Rule
Power of Power Exponent Rule
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Rearranging Formulas
Rearranging FormulasRearranging Formulas
Rearranging Formulas
 
Module 2 topic 1 notes
Module 2 topic 1 notesModule 2 topic 1 notes
Module 2 topic 1 notes
 
Exponents Rules
Exponents RulesExponents Rules
Exponents Rules
 
Function and graphs
Function and graphsFunction and graphs
Function and graphs
 
Logarithms
LogarithmsLogarithms
Logarithms
 
Lesson 1.3 Powerpoint
Lesson 1.3 PowerpointLesson 1.3 Powerpoint
Lesson 1.3 Powerpoint
 
Types of functions, domain, range
Types of functions, domain, rangeTypes of functions, domain, range
Types of functions, domain, range
 
Radix sorting
Radix sortingRadix sorting
Radix sorting
 
Nikit
NikitNikit
Nikit
 
Hashing
HashingHashing
Hashing
 
Lecture4
Lecture4Lecture4
Lecture4
 
7th pre alg -l41
7th pre alg -l417th pre alg -l41
7th pre alg -l41
 

Similar to Linear and Binary Search

Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
FazalRehman79
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
SwatiHans10
 
Searching
Searching Searching
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
maamir farooq
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
Revathiparamanathan
 
Linear and Binary search .pptx
Linear and Binary search .pptxLinear and Binary search .pptx
Linear and Binary search .pptx
p83629918
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
Interpolation search
Interpolation searchInterpolation search
Interpolation search
Usr11011
 
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 and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
nikunjandy
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
SpammemeLmao
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
guest2cb109
 

Similar to Linear and Binary Search (20)

Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Searching
Searching Searching
Searching
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
Linear and Binary search .pptx
Linear and Binary search .pptxLinear and Binary search .pptx
Linear and Binary search .pptx
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
Interpolation search
Interpolation searchInterpolation search
Interpolation search
 
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 and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 

More from WinNie Sjr

Pinterest
PinterestPinterest
Pinterest
WinNie Sjr
 
Graph
GraphGraph
Graph
WinNie Sjr
 
Pinterest
PinterestPinterest
Pinterest
WinNie Sjr
 
ข่าวพรบ.คอม
ข่าวพรบ.คอมข่าวพรบ.คอม
ข่าวพรบ.คอม
WinNie Sjr
 
พรบ คอมพิวเตอร์
พรบ คอมพิวเตอร์พรบ คอมพิวเตอร์
พรบ คอมพิวเตอร์
WinNie Sjr
 
อุปกรณ์เสริม
อุปกรณ์เสริมอุปกรณ์เสริม
อุปกรณ์เสริม
WinNie Sjr
 
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
WinNie Sjr
 
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
WinNie Sjr
 
คอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้นคอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้น
WinNie Sjr
 
คอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้นคอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้น
WinNie Sjr
 
It
ItIt
โครงงานรายงาน
โครงงานรายงานโครงงานรายงาน
โครงงานรายงาน
WinNie Sjr
 
Www (world wide web)
Www (world wide web)Www (world wide web)
Www (world wide web)
WinNie Sjr
 

More from WinNie Sjr (14)

Pinterest
PinterestPinterest
Pinterest
 
Graph
GraphGraph
Graph
 
Pinterest
PinterestPinterest
Pinterest
 
ข่าวพรบ.คอม
ข่าวพรบ.คอมข่าวพรบ.คอม
ข่าวพรบ.คอม
 
พรบ คอมพิวเตอร์
พรบ คอมพิวเตอร์พรบ คอมพิวเตอร์
พรบ คอมพิวเตอร์
 
อุปกรณ์เสริม
อุปกรณ์เสริมอุปกรณ์เสริม
อุปกรณ์เสริม
 
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
 
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
 
คอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้นคอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้น
 
คอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้นคอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้น
 
It
ItIt
It
 
Com
ComCom
Com
 
โครงงานรายงาน
โครงงานรายงานโครงงานรายงาน
โครงงานรายงาน
 
Www (world wide web)
Www (world wide web)Www (world wide web)
Www (world wide web)
 

Recently uploaded

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 

Linear and Binary Search

  • 1. Linear search Linear search is a very simple search algorithm. In this type of search, a search is made over one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection. How Linear search works ? Algorithm : Linear Search ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit Pseudocode : procedure linear_search (list, value)] for each item in the list if match item == value return the item's location end if end for end procedure
  • 2. Binary search Binary search is fast search algorithm withcomplexity of Ο(log n). How Binary search works ? For a binary search to work, it is necessary for the target array to be sorted. We learn the process of binary search with a example. The following is our sorted array and let us assume that we need to search the location of value 31 using binary search. 10 14 19 25 27 31 33 35 42 44 0 1 2 3 4 5 6 7 8 9 First, we shall determine half of the array by using : mid = low + (high - low) / 2 Ans = 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array. 10 14 19 25 27 31 33 35 42 44 0 1 2 3 4 5 6 7 8 9 Now compare the value s at location 4, We find that the value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array, so we know that the target value must be in the upper portion of the array. 10 14 19 25 27 31 33 35 42 44 0 1 2 3 4 5 6 7 8 9 We change the new mid value again. Our new mid is 7 now. We compare the value stored at location 7 with our target value 31. 10 14 19 25 27 31 33 35 42 44 0 1 2 3 4 5 6 7 8 9
  • 3. The value at location 7 is not match, rather it is more than what we want. So, the value must be in the lower part from this location. calculate the mid again. This time it is 5. 10 14 19 25 27 31 33 35 42 44 0 1 2 3 4 5 6 7 8 9 compare the value at location 5 with our target value. it is a match already. The target value 31 is stored at location 5. Pseudocode Procedure binary_search A ← sorted array n ← size of array x ← value to be searched Set lowerBound = 1 Set upperBound = n while x not found if upperBound < lowerBound EXIT: x does not exists. set midPoint = lowerBound + ( upperBound - lowerBound ) / 2 if A[midPoint] < x set lowerBound = midPoint + 1 if A[midPoint] > x set upperBound = midPoint - 1 if A[midPoint] = x EXIT: x found at location midPoint end while end procedure