SlideShare a Scribd company logo
1 of 20
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

BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptSeethaDinesh
 
Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
Linear Search
Linear SearchLinear Search
Linear SearchSWATHIR72
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation AhmedAlbutty
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data StructureTalha Shaikh
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear searchmontazur420
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)eShikshak
 
Red black tree
Red black treeRed black tree
Red black treeuos lahore
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresPriyanka Rana
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 

What's hot (20)

Insertion sort
Insertion sort Insertion sort
Insertion sort
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Linear Search
Linear SearchLinear Search
Linear Search
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Binary search
Binary searchBinary search
Binary search
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Red black tree
Red black treeRed black tree
Red black tree
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Binary search
Binary searchBinary search
Binary search
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 

Viewers also liked

Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary SearchReem Alattas
 
Data structure
Data structureData structure
Data structureM Ramya
 
Bec doms ppt on management information systems
Bec doms ppt on management information systemsBec doms ppt on management information systems
Bec doms ppt on management information systemsBabasab Patil
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...widespreadpromotion
 
Levels of management
Levels of managementLevels of management
Levels of managementSweetp999
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
Management Information System (MIS)
Management Information System (MIS)Management Information System (MIS)
Management Information System (MIS)Navneet Jingar
 

Viewers also liked (18)

Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
 
Linear and Bianry search
Linear and Bianry searchLinear and Bianry search
Linear and Bianry search
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary Search
 
Ch05 Black Jack
Ch05  Black  JackCh05  Black  Jack
Ch05 Black Jack
 
Data structure
Data structureData structure
Data structure
 
Bca cobol
Bca cobolBca cobol
Bca cobol
 
Binary search
Binary searchBinary search
Binary search
 
Bec doms ppt on management information systems
Bec doms ppt on management information systemsBec doms ppt on management information systems
Bec doms ppt on management information systems
 
Cobol basics 19-6-2010
Cobol basics 19-6-2010Cobol basics 19-6-2010
Cobol basics 19-6-2010
 
Binary search
Binary search Binary search
Binary search
 
Data Structures
Data StructuresData Structures
Data Structures
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 
Arrays
ArraysArrays
Arrays
 
Levels of management
Levels of managementLevels of management
Levels of management
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Management Information System (MIS)
Management Information System (MIS)Management Information System (MIS)
Management Information System (MIS)
 

Similar to Searching linear &amp; binary search

Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary searchJeoJoyA
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinaryPrashant Rai
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
seaching internal 2 ppt.pptx
seaching internal 2 ppt.pptxseaching internal 2 ppt.pptx
seaching internal 2 ppt.pptxSubhrasisBiswal1
 
Searching techniques
Searching techniquesSearching techniques
Searching techniquesER Punit Jain
 
Linear and Binary search .pptx
Linear and Binary search .pptxLinear and Binary search .pptx
Linear and Binary search .pptxp83629918
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search AlgorithmFazalRehman79
 
Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary SearchWinNie Sjr
 
COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6Vishal Patil
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3smruti sarangi
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 

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

search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
seaching internal 2 ppt.pptx
seaching internal 2 ppt.pptxseaching internal 2 ppt.pptx
seaching internal 2 ppt.pptx
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
searching
searchingsearching
searching
 
Linear and Binary search .pptx
Linear and Binary search .pptxLinear and Binary search .pptx
Linear and Binary search .pptx
 
4- searching.ppt
4- searching.ppt4- searching.ppt
4- searching.ppt
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary Search
 
COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6
 
Searching
SearchingSearching
Searching
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 

Recently uploaded

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 

Recently uploaded (20)

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 

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; }