unit II_2_i.pptx

H
Searching methods
• is an operation or a technique that helps finds the place of a
given element or value in the list.
• are designed to check for an element or retrieve an element
from any data structure where it is stored.
• is said to be successful or unsuccessful depending upon
whether the element that is being searched is found or not.
• standard techniques
• Sequential Search
• Interval Search
Linear Search
• simplest method for searching
• defined as a sequential search algorithm
• starts at one end and goes through each element of a list until
the desired element is found
• otherwise the search continues till the end of the data set
Linear Search
• Given an array arr[] of N elements, the task is to write a
function to search a given element x in arr[].
• Iterate from 0 to N-1 and compare the value of every index with
x if they match return index.
Steps for Linear Search
• Start from the leftmost element of arr[] and one by one compare
x with each element of arr[]
• If x matches with an element, return the index.
• If x doesn’t match with any of the elements, return -1.
Algorithm for Linear Search
• algorithm Seqnl_Search(list, item)
• Pre: list != ;
• Post: return the index of the item if found, otherwise: 1
• index <- fi
• while index < list.Cnt and list[index] != item //cnt: counter variable
• index <- index + 1
• end while
• if index < list.Cnt and list[index] = item
• return index
• end if
• return: 1
Linear Search implementation in C
Binary search
• is a very fast and efficient searching technique.
• requires the list to be in sorted order.
Binary search
• execution needs to compare the element to be searched with the
present element at the center of the list.
• If it matches, then the search is successful otherwise the list is
divided into two halves:
• one from the 0th element to the middle element which is the
center element (first half)
• another from the center element to the last element (which is the
2nd half) where all values are greater than the center element.
• proceeds from either of the two halves depending upon whether the
target element is greater or smaller than the central element.
• If the element is smaller than the central element, then searching is
done in the first half, otherwise searching is done in the second half.
Algorithm for Binary Search
• algorithm Binary_Search(list, item)
• Set L to 0 and R to n: 1
• if L > R, then Binary_Search terminates as unsuccessful
• else
• Set m (the position in the mid element) to the floor of (L + R) / 2
• if Am < T, set L to m + 1 and go to step 3
• if Am > T, set R to m: 1 and go to step 3
• Now, Am = T,
• the search is done; return (m)
Binary Search implementation in C
Linear Search and Binary Search
Linear Search and Binary Search
1 de 12

Más contenido relacionado

Similar a unit II_2_i.pptx

SearchingSearching
SearchingAshim Lamichhane
18.2K vistas35 diapositivas
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
18 vistas87 diapositivas
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx21BD1A058RSahithi
3 vistas26 diapositivas

Similar a unit II_2_i.pptx(20)

SearchingSearching
Searching
Ashim Lamichhane18.2K vistas
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman3.9K vistas
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs718 vistas
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
21BD1A058RSahithi3 vistas
cs702 ppt.pptcs702 ppt.ppt
cs702 ppt.ppt
JavedIqbal39817114 vistas
programming in Cprogramming in C
programming in C
ADITHYAM197 vistas
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
Sujan5279087 vistas
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptx
bhuvansaachi183 vistas
Searching SortingSearching Sorting
Searching Sorting
guest2cb1091.3K vistas
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university112 vistas
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
technologygyan42 vistas
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786149 vistas
searchingsearching
searching
A. S. M. Shafi30 vistas
Searching Searching
Searching
CGC Technical campus,Mohali531 vistas
Searching techniquesSearching techniques
Searching techniques
ER Punit Jain304 vistas
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith555 vistas

Más de HODElex

Unit 4_Network Layer_Part II.pptxUnit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxHODElex
7 vistas102 diapositivas
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.pptHODElex
10 vistas62 diapositivas
I & E Tutorials.pdfI & E Tutorials.pdf
I & E Tutorials.pdfHODElex
38 vistas6 diapositivas
wireless lan presentation.pptwireless lan presentation.ppt
wireless lan presentation.pptHODElex
78 vistas93 diapositivas

Más de HODElex(7)

Último(20)

cloud computing-virtualization.pptxcloud computing-virtualization.pptx
cloud computing-virtualization.pptx
RajaulKarim2072 vistas
PlumbingPlumbing
Plumbing
Iwiss Tools Co.,Ltd9 vistas
SICTECH CORPORATE PRESENTATIONSICTECH CORPORATE PRESENTATION
SICTECH CORPORATE PRESENTATION
SiCtechInduction15 vistas
What is Whirling Hygrometer.pdfWhat is Whirling Hygrometer.pdf
What is Whirling Hygrometer.pdf
IIT KHARAGPUR 10 vistas
Pointers.pptxPointers.pptx
Pointers.pptx
Ananthi Palanisamy58 vistas
Paper 3.pdfPaper 3.pdf
Paper 3.pdf
Javad Kadkhodapour6 vistas
SEMI CONDUCTORSSEMI CONDUCTORS
SEMI CONDUCTORS
pavaniaalla200515 vistas
Sanitary Landfill- SWM.pptxSanitary Landfill- SWM.pptx
Sanitary Landfill- SWM.pptx
Vinod Nejkar5 vistas
Object Oriented Programming with JAVAObject Oriented Programming with JAVA
Object Oriented Programming with JAVA
Demian Antony D'Mello49 vistas
String.pptxString.pptx
String.pptx
Ananthi Palanisamy45 vistas
Stone Masonry and Brick Masonry.pdfStone Masonry and Brick Masonry.pdf
Stone Masonry and Brick Masonry.pdf
Mohammed Abdullah Laskar17 vistas
Electrical CrimpingElectrical Crimping
Electrical Crimping
Iwiss Tools Co.,Ltd18 vistas
SNMPxSNMPx
SNMPx
Amatullahbutt10 vistas
Codes and Conventions.pptxCodes and Conventions.pptx
Codes and Conventions.pptx
IsabellaGraceAnkers5 vistas

unit II_2_i.pptx

  • 1. Searching methods • is an operation or a technique that helps finds the place of a given element or value in the list. • are designed to check for an element or retrieve an element from any data structure where it is stored. • is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. • standard techniques • Sequential Search • Interval Search
  • 2. Linear Search • simplest method for searching • defined as a sequential search algorithm • starts at one end and goes through each element of a list until the desired element is found • otherwise the search continues till the end of the data set
  • 3. Linear Search • Given an array arr[] of N elements, the task is to write a function to search a given element x in arr[]. • Iterate from 0 to N-1 and compare the value of every index with x if they match return index.
  • 4. Steps for Linear Search • Start from the leftmost element of arr[] and one by one compare x with each element of arr[] • If x matches with an element, return the index. • If x doesn’t match with any of the elements, return -1.
  • 5. Algorithm for Linear Search • algorithm Seqnl_Search(list, item) • Pre: list != ; • Post: return the index of the item if found, otherwise: 1 • index <- fi • while index < list.Cnt and list[index] != item //cnt: counter variable • index <- index + 1 • end while • if index < list.Cnt and list[index] = item • return index • end if • return: 1
  • 7. Binary search • is a very fast and efficient searching technique. • requires the list to be in sorted order.
  • 8. Binary search • execution needs to compare the element to be searched with the present element at the center of the list. • If it matches, then the search is successful otherwise the list is divided into two halves: • one from the 0th element to the middle element which is the center element (first half) • another from the center element to the last element (which is the 2nd half) where all values are greater than the center element. • proceeds from either of the two halves depending upon whether the target element is greater or smaller than the central element. • If the element is smaller than the central element, then searching is done in the first half, otherwise searching is done in the second half.
  • 9. Algorithm for Binary Search • algorithm Binary_Search(list, item) • Set L to 0 and R to n: 1 • if L > R, then Binary_Search terminates as unsuccessful • else • Set m (the position in the mid element) to the floor of (L + R) / 2 • if Am < T, set L to m + 1 and go to step 3 • if Am > T, set R to m: 1 and go to step 3 • Now, Am = T, • the search is done; return (m)
  • 11. Linear Search and Binary Search
  • 12. Linear Search and Binary Search