SlideShare a Scribd company logo
1 of 3
Download to read offline
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
 
1.4 Functions
1.4 Functions1.4 Functions
1.4 Functions
nicksuf
 
Power of Power Exponent Rule
Power of Power Exponent RulePower of Power Exponent Rule
Power of Power Exponent Rule
Passy World
 
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

Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 
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...
 
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
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 

More from WinNie Sjr

อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
WinNie Sjr
 
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
WinNie Sjr
 
คอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้นคอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้น
WinNie Sjr
 
คอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้นคอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้น
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

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

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