SlideShare a Scribd company logo
LINEAR SEARCH
Presented By
Nikunj Patel
Parth Patel
DATA
STRUCTURE
Searching: Finding the location of
item or printing some message when
item is not found.
SEARC
H
LINEAR BINARY
 Linear search: Traversing data sequentially to locate
item is called linear search.
 Ex: Searching an item for operation in array.
 Binary search: Data in array which is sorted in
increasing numerical order or alphabetically.
 Ex: Searching name in telephone directory,
searching words in dictionary.
LINEAR SEARCH
 It test whether the ITEM in DATA is present or
not.
 It test the data in sequential manner.
 It searches the data one by one fully and returns
the ITEM as the result.
 Otherwise, it returns the value 0.
 We see this by ALGORITHM.
Alg:LINEAR(DATA,N,ITEM,LOC)
 Items explanation:
1. DATA --Linear array
2. N --Number of elements
3. ITEM --Elements to find
4. LOC --Location of the item
STEPS:
1. [Insert ITEM at the end] Set DATA[N+1]:=ITEM.
2. [Initialize counter] Set LOC:=1.
3. [Search for ITEM]
Repeat while DATA[LOC]= ITEM:
Set LOC:=LOC+1.
[End if loop]
4. [Successful?]If LOC:=N+1, then ;
Set LOC:=0
5. Exit
EXECUTION WITH EXAMPLE
PARTICULARS:
1. DATA [6] =
2. ITEM=G
Cell
name
A B C D E F
Loc 1 2 3 4 5 6
 To find the item we are first inserting the item to the
end of the list.
 Step 1: DATA[N+1]=ITEM.
Exp:
N=6
DATA[6]=F
DATA[6+1]=G
 So the item is added at LOC[7]
A B C D E F G
1 2 3 4 5 6 7
 Step 2:
Initializing the counter to start the search.
Therefore, LOC=1.
It starts the search from LOC=1{i.e. from
DATA[1]=A}
 Step 3:
WHILE loop is executed till DATA[LOC]=ITEM
From the step 2, LOC=1
A B C D E F G
A B C D E F G
A B C D E F G
A B C D E F G
S
E
A
R
C
H
I
N
G
A B C D E F G
S
E
A
R
C
H
I
N
G
A B C D E F G
A B C D E F G
Here the item is found
 The item ‘G’ is located
 So the loop executes until this condition
A B C D E F G
 STEP 4:
Originally the location is 6. We added the item at
the end.
So the item is located in 7.
LOC=N+1
We reached the condition then
LOC=0
 STEP 5:
Searching is finished and the algorithm exits.
Binary Search
• If the array is sorted, then we can apply the binary
search technique.
number
• The basic idea is straightforward. First search the
value in the middle position. If X is less than this
value, then search the middle of the left half next. If
X is greater than this value, then search the middle
of the right half next. Continue in this manner.
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
Sequence of Successful Search - 1
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )low high mid
0 8#1
highlow



 

2
highlow
mid
38 < 44 low = mid+1 = 5
mid
4
Sequence of Successful Search - 2
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )low high mid
0 8#1



 

2
highlow
mid
44 < 77high = mid-1=5
4
mid
6
highlow
5 8#2
Sequence of Successful Search - 3
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )low high mid
0 8#1



 

2
highlow
mid
44 == 44
4
5 8#2 6
highlow
5 5#3
mid
5
Successful Search!!
Sequence of Unsuccessful Search
- 4
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )low high mid
0 8#1



 

2
highlow
mid
4
5 8#2 6
5 5#3 5
high low
6 5#4
Unsuccessful Search
low > highno more elements to search
Binary Search Routine
public int binarySearch (int[] number, int searchValue) {
int low = 0,
high = number.length - 1,
mid = (low + high) / 2;
while (low <= high && number[mid] != searchValue) {
if (number[mid] < searchValue) {
low = mid + 1;
} else { //number[mid] > searchValue
high = mid - 1;
}
mid = (low + high) / 2; //integer division will
truncate
}
if (low > high) {
mid = NOT_FOUND;
}
return mid;
}
Searching linear &amp; binary search

More Related Content

What's hot (20)

PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PPTX
Searching Techniques and Analysis
AkashBorse2
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPT
b+ tree
bitistu
 
PPSX
Linear and binary search
Arjunsinh Jadeja
 
PPTX
Linear search-and-binary-search
International Islamic University
 
PPT
Binary tree
Vanitha Chandru
 
PPTX
Dijkstra
jagdeeparora86
 
PPT
1.4 expression tree
Krish_ver2
 
PPT
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
PPT
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
PPT
Best for b trees
DineshRaaja
 
PDF
Expression trees
Salman Vadsarya
 
PPTX
Binary Search Tree
Abhishek L.R
 
PPTX
AVL Tree Data Structure
Afaq Mansoor Khan
 
PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPT
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 
PPTX
Automata Theory - Turing machine
Akila Krishnamoorthy
 
Searching & Sorting Algorithms
Rahul Jamwal
 
Searching Techniques and Analysis
AkashBorse2
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
b+ tree
bitistu
 
Linear and binary search
Arjunsinh Jadeja
 
Linear search-and-binary-search
International Islamic University
 
Binary tree
Vanitha Chandru
 
Dijkstra
jagdeeparora86
 
1.4 expression tree
Krish_ver2
 
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Best for b trees
DineshRaaja
 
Expression trees
Salman Vadsarya
 
Binary Search Tree
Abhishek L.R
 
AVL Tree Data Structure
Afaq Mansoor Khan
 
Binary search tree(bst)
Hossain Md Shakhawat
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 
Automata Theory - Turing machine
Akila Krishnamoorthy
 

Viewers also liked (17)

PPT
Linear and Bianry search
Daffodil International University
 
PPT
Linear Search & Binary Search
Reem Alattas
 
PPTX
Sequential & binary, linear search
montazur420
 
PPT
Data Structures - Searching & sorting
Kaushal Shah
 
PPT
Ch05 Black Jack
leminhvuong
 
PPTX
Data structure
M Ramya
 
PDF
Bca cobol
blancanus
 
PPTX
Binary search
ronit gaikwad
 
PPTX
Linear Search Presentation
Markajul Hasnain Alif
 
PPT
Cobol basics 19-6-2010
SivaprasanthRentala1975
 
PPTX
Binary search
Raghu nath
 
PPTX
Data Structures
Nitesh Bichwani
 
PPTX
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
widespreadpromotion
 
PPT
Arrays
archikabhatia
 
PPTX
Levels of management
Sweetp999
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PPT
Management Information System (MIS)
Navneet Jingar
 
Linear and Bianry search
Daffodil International University
 
Linear Search & Binary Search
Reem Alattas
 
Sequential & binary, linear search
montazur420
 
Data Structures - Searching & sorting
Kaushal Shah
 
Ch05 Black Jack
leminhvuong
 
Data structure
M Ramya
 
Bca cobol
blancanus
 
Binary search
ronit gaikwad
 
Linear Search Presentation
Markajul Hasnain Alif
 
Cobol basics 19-6-2010
SivaprasanthRentala1975
 
Binary search
Raghu nath
 
Data Structures
Nitesh Bichwani
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
widespreadpromotion
 
Levels of management
Sweetp999
 
Sorting Algorithms
Mohammed Hussein
 
Management Information System (MIS)
Navneet Jingar
 
Ad

Similar to Searching linear &amp; binary search (20)

PPT
search_sort.ppt
SwatiHans10
 
PPTX
Linear and binary search
JeoJoyA
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PDF
Ocw chp6 2searchbinary
Prashant Rai
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
sorting and searching.pptx
ParagAhir1
 
PPTX
seaching internal 2 ppt.pptx
SubhrasisBiswal1
 
PPTX
Searching and Sorting algorithms and working
RitikaLohiya2
 
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
PPTX
Chapter 2. data structure and algorithm
SolomonEndalu
 
PPTX
Searching techniques
ER Punit Jain
 
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
PPTX
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
PDF
searching
A. S. M. Shafi
 
PPTX
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
PPTX
Linear and Binary search .pptx
p83629918
 
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
PPT
4- searching.ppt
zabihniazai1
 
PPTX
Binary search Algorithm
FazalRehman79
 
search_sort.ppt
SwatiHans10
 
Linear and binary search
JeoJoyA
 
searching in data structure.pptx
chouguleamruta24
 
Ocw chp6 2searchbinary
Prashant Rai
 
sorting and searching.pptx
ParagAhir1
 
seaching internal 2 ppt.pptx
SubhrasisBiswal1
 
Searching and Sorting algorithms and working
RitikaLohiya2
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
Chapter 2. data structure and algorithm
SolomonEndalu
 
Searching techniques
ER Punit Jain
 
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
searching
A. S. M. Shafi
 
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
Linear and Binary search .pptx
p83629918
 
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
4- searching.ppt
zabihniazai1
 
Binary search Algorithm
FazalRehman79
 
Ad

Recently uploaded (20)

PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
PPTX
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PPTX
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PPTX
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 

Searching linear &amp; binary search

  • 1. LINEAR SEARCH Presented By Nikunj Patel Parth Patel DATA STRUCTURE
  • 2. Searching: Finding the location of item or printing some message when item is not found. SEARC H LINEAR BINARY
  • 3.  Linear search: Traversing data sequentially to locate item is called linear search.  Ex: Searching an item for operation in array.  Binary search: Data in array which is sorted in increasing numerical order or alphabetically.  Ex: Searching name in telephone directory, searching words in dictionary.
  • 4. LINEAR SEARCH  It test whether the ITEM in DATA is present or not.  It test the data in sequential manner.  It searches the data one by one fully and returns the ITEM as the result.  Otherwise, it returns the value 0.  We see this by ALGORITHM.
  • 5. Alg:LINEAR(DATA,N,ITEM,LOC)  Items explanation: 1. DATA --Linear array 2. N --Number of elements 3. ITEM --Elements to find 4. LOC --Location of the item
  • 6. STEPS: 1. [Insert ITEM at the end] Set DATA[N+1]:=ITEM. 2. [Initialize counter] Set LOC:=1. 3. [Search for ITEM] Repeat while DATA[LOC]= ITEM: Set LOC:=LOC+1. [End if loop] 4. [Successful?]If LOC:=N+1, then ; Set LOC:=0 5. Exit
  • 7. EXECUTION WITH EXAMPLE PARTICULARS: 1. DATA [6] = 2. ITEM=G Cell name A B C D E F Loc 1 2 3 4 5 6
  • 8.  To find the item we are first inserting the item to the end of the list.  Step 1: DATA[N+1]=ITEM. Exp: N=6 DATA[6]=F DATA[6+1]=G  So the item is added at LOC[7] A B C D E F G 1 2 3 4 5 6 7
  • 9.  Step 2: Initializing the counter to start the search. Therefore, LOC=1. It starts the search from LOC=1{i.e. from DATA[1]=A}  Step 3: WHILE loop is executed till DATA[LOC]=ITEM From the step 2, LOC=1
  • 10. A B C D E F G A B C D E F G A B C D E F G A B C D E F G S E A R C H I N G
  • 11. A B C D E F G S E A R C H I N G A B C D E F G A B C D E F G
  • 12. Here the item is found  The item ‘G’ is located  So the loop executes until this condition A B C D E F G
  • 13.  STEP 4: Originally the location is 6. We added the item at the end. So the item is located in 7. LOC=N+1 We reached the condition then LOC=0  STEP 5: Searching is finished and the algorithm exits.
  • 14. Binary Search • If the array is sorted, then we can apply the binary search technique. number • The basic idea is straightforward. First search the value in the middle position. If X is less than this value, then search the middle of the left half next. If X is greater than this value, then search the middle of the right half next. Continue in this manner. 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90
  • 15. Sequence of Successful Search - 1 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 44 )low high mid 0 8#1 highlow       2 highlow mid 38 < 44 low = mid+1 = 5 mid 4
  • 16. Sequence of Successful Search - 2 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 44 )low high mid 0 8#1       2 highlow mid 44 < 77high = mid-1=5 4 mid 6 highlow 5 8#2
  • 17. Sequence of Successful Search - 3 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 44 )low high mid 0 8#1       2 highlow mid 44 == 44 4 5 8#2 6 highlow 5 5#3 mid 5 Successful Search!!
  • 18. Sequence of Unsuccessful Search - 4 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 45 )low high mid 0 8#1       2 highlow mid 4 5 8#2 6 5 5#3 5 high low 6 5#4 Unsuccessful Search low > highno more elements to search
  • 19. Binary Search Routine public int binarySearch (int[] number, int searchValue) { int low = 0, high = number.length - 1, mid = (low + high) / 2; while (low <= high && number[mid] != searchValue) { if (number[mid] < searchValue) { low = mid + 1; } else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; //integer division will truncate } if (low > high) { mid = NOT_FOUND; } return mid; }