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

PPTX
Application of derivatives
PPT
Piecewise part 2 worked
PPTX
Binary search
PPT
Linear and Bianry search
PPTX
Binary search2
PPTX
Greatest integer
PPT
Gr10 step function ppt
PPTX
Application of derivatives
Piecewise part 2 worked
Binary search
Linear and Bianry search
Binary search2
Greatest integer
Gr10 step function ppt

What's hot (20)

PPT
Writing Function Rules
PPT
2.1 Functions and Their Graphs
PPTX
My presentation
PPTX
ROOTS AND RADICALS - ELEMENTARY ALGEBRA
PPTX
1.4 Functions
PDF
Types of functions
PPTX
Power of Power Exponent Rule
PPT
Chapter 11 - Sorting and Searching
PPTX
Rearranging Formulas
PPT
Module 2 topic 1 notes
PPTX
Exponents Rules
PPTX
Function and graphs
PPT
Logarithms
PPT
Lesson 1.3 Powerpoint
DOCX
Types of functions, domain, range
PPTX
Radix sorting
PPT
Hashing
PDF
Lecture4
PDF
7th pre alg -l41
Writing Function Rules
2.1 Functions and Their Graphs
My presentation
ROOTS AND RADICALS - ELEMENTARY ALGEBRA
1.4 Functions
Types of functions
Power of Power Exponent Rule
Chapter 11 - Sorting and Searching
Rearranging Formulas
Module 2 topic 1 notes
Exponents Rules
Function and graphs
Logarithms
Lesson 1.3 Powerpoint
Types of functions, domain, range
Radix sorting
Hashing
Lecture4
7th pre alg -l41
Ad

Similar to Linear and Binary Search (20)

PPTX
Binary search Algorithm
PDF
advanced searching and sorting.pdf
PPT
search_sort.ppt
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPTX
Lecture_Oct26.pptx
PPTX
All Searching and Sorting Techniques in Data Structures
PPTX
Searching and Sorting algorithms and working
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
PDF
Binary search algorithm
DOCX
UNIT V.docx
PPTX
Linear and Binary search .pptx
PPTX
Dsa – data structure and algorithms searching
PDF
UNIT IV -Data Structures.pdf
PPTX
Interpolation search
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
Algorithm & data structures lec4&5
DOCX
MODULE 5-Searching and-sorting
PPT
Data structure and problem solving ch03.ppt
Binary search Algorithm
advanced searching and sorting.pdf
search_sort.ppt
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Lecture_Oct26.pptx
All Searching and Sorting Techniques in Data Structures
Searching and Sorting algorithms and working
Chapter #3 (Searchinmmmg ALgorithm).pptx
Binary search algorithm
UNIT V.docx
Linear and Binary search .pptx
Dsa – data structure and algorithms searching
UNIT IV -Data Structures.pdf
Interpolation search
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Searching and Sorting Algorithms in Data Structures
Algorithm & data structures lec4&5
MODULE 5-Searching and-sorting
Data structure and problem solving ch03.ppt
Ad

More from WinNie Sjr (14)

PDF
Pinterest
PDF
Graph
DOCX
Pinterest
PDF
ข่าวพรบ.คอม
PDF
พรบ คอมพิวเตอร์
PDF
อุปกรณ์เสริม
PDF
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
PDF
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
PDF
คอมพิวเตอร์เบื้องต้น
PPTX
คอมพิวเตอร์เบื้องต้น
PDF
PDF
PDF
โครงงานรายงาน
PDF
Www (world wide web)
Pinterest
Graph
Pinterest
ข่าวพรบ.คอม
พรบ คอมพิวเตอร์
อุปกรณ์เสริม
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
อุปกรณ์เชื่อมต่อคอมพิวเตอร์
คอมพิวเตอร์เบื้องต้น
คอมพิวเตอร์เบื้องต้น
โครงงานรายงาน
Www (world wide web)

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Insiders guide to clinical Medicine.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
Pharmacology of Heart Failure /Pharmacotherapy of CHF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
TR - Agricultural Crops Production NC III.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
102 student loan defaulters named and shamed – Is someone you know on the list?
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial diseases, their pathogenesis and prophylaxis
Insiders guide to clinical Medicine.pdf
Computing-Curriculum for Schools in Ghana
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
human mycosis Human fungal infections are called human mycosis..pptx

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