SlideShare a Scribd company logo
DATA STRUCTURE
LINEAR SEARCH
M.RAMYA
III-MCA
HCC
Trichy
Searching: Finding the location of item or
printing some message when item is not
found.
SEARCH
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
DATA[LOC] =ITEM
LOC=LOC+1
DATA[LOC] =ITEM, LOC=LOC+1
DATA[LOC] =ITEM
LOC=LOC+1
LOC=1
LOC=2
LOC=3
A B C D E F G
DATA[LOC] =ITEM,
LOC=LOC+1
S
E
A
R
C
H
I
N
G
A B C D E F G
DATA[LOC] =ITEM,
LOC=LOC+1
A B C D E F G
DATA[LOC]
=ITEM,
LOC=LOC+1
LOC=4
LOC=5
LOC=6
Here the item is found
The item ‘G’ is located
So the loop executes until this condition
A B C D E F G
DATA[LOC] =ITEM
LOC=7
• 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.
COMPLEXITY
• Worst Case: The maximum value to search.
• Complexity is measured by f(n).f(n)=n+1
• The average case is measured by probability.
• Here,
• pk – probability that ITEM appears in DATA[K].
• q – probability that ITEM does not appears in
DATA[K].
• Algorithm uses k comparisons when ITEM
appears in DATA[K].
• f(n)=1 . p1 + 2 . p2 + … +n . pn + (n+1) . q
• Suppose, q is very small and ITEm appears in
equal probability, then
q=o and pi=1/n
f(n)=1 . 1/n + 2 . 1/n + … +n . 1/n + (n+1).0
=(1+2+…+n) .1/n
=n(n+1)/2.(1/n)
f(n)=(n+1)/2

More Related Content

PPTX
LINEAR SEARCH
PPTX
Searching linear & binary search
PPSX
Linear and binary search
PPTX
Rahat & juhith
PPT
Searching algorithm
PPTX
Searching techniques
PDF
Binary search algorithm
PPTX
Binary search
LINEAR SEARCH
Searching linear & binary search
Linear and binary search
Rahat & juhith
Searching algorithm
Searching techniques
Binary search algorithm
Binary search

What's hot (20)

PPTX
Sorting and searching arrays binary search algorithm
PPT
SEARCHING AND SORTING ALGORITHMS
PPTX
Binary search Algorithm
PPTX
Dsa – data structure and algorithms searching
PDF
linear search and binary search
PDF
Python list functions
PPT
Searching Sorting
PDF
Self-balancing Trees and Skip Lists
PPTX
Binary search
PPTX
Sequential & binary, linear search
PPT
Searching algorithms
PDF
PPT
L10 sorting-searching
PPTX
Data structure unit I part B
PDF
Ocw chp6 2searchbinary
PPTX
PDF
searching
PPTX
366 it elective 4 (analysis of algoritm)
PPT
Chapter 14
PPT
Binary Search
Sorting and searching arrays binary search algorithm
SEARCHING AND SORTING ALGORITHMS
Binary search Algorithm
Dsa – data structure and algorithms searching
linear search and binary search
Python list functions
Searching Sorting
Self-balancing Trees and Skip Lists
Binary search
Sequential & binary, linear search
Searching algorithms
L10 sorting-searching
Data structure unit I part B
Ocw chp6 2searchbinary
searching
366 it elective 4 (analysis of algoritm)
Chapter 14
Binary Search
Ad

Viewers also liked (10)

PPTX
Binary search
PPTX
Linear Search Presentation
PPT
Linear and Bianry search
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
PDF
Linear search algorithm
PPTX
Linear Search Data Structure
PDF
Sorting Algorithms
PPT
Data Structures - Searching & sorting
PPT
Chapter 11 - Sorting and Searching
Binary search
Linear Search Presentation
Linear and Bianry search
Binary Search - Design & Analysis of Algorithms
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
Linear search algorithm
Linear Search Data Structure
Sorting Algorithms
Data Structures - Searching & sorting
Chapter 11 - Sorting and Searching
Ad

Similar to Data structure (20)

PPT
PPTX
Linear and binary search
PPT
search_sort.ppt
PPT
COMPUTER PROGRAMMING UNIT 1 Lecture 6
PPTX
searching in data structure.pptx
PPT
arrays1.ppt python programme arrays insertion
PPTX
seaching internal 2 ppt.pptx
PPT
Array operations
PPT
ds 2-Arrays and its types and operations
PPSX
Algorithm and Programming (Searching)
PPTX
Chapter 2. data structure and algorithm
PPTX
Unit 2 - Quick Sort.pptx
PPT
4- searching.ppt
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
PPT
Data structure lecture 4
PPTX
Data structure using c module 3
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
PPT
search_sort Search sortSearch sortSearch sortSearch sort
PPTX
Data structures
PPTX
Linear and binary search
search_sort.ppt
COMPUTER PROGRAMMING UNIT 1 Lecture 6
searching in data structure.pptx
arrays1.ppt python programme arrays insertion
seaching internal 2 ppt.pptx
Array operations
ds 2-Arrays and its types and operations
Algorithm and Programming (Searching)
Chapter 2. data structure and algorithm
Unit 2 - Quick Sort.pptx
4- searching.ppt
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Data structure lecture 4
Data structure using c module 3
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
search_sort Search sortSearch sortSearch sortSearch sort
Data structures

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
master seminar digital applications in india
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
GDM (1) (1).pptx small presentation for students
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
O5-L3 Freight Transport Ops (International) V1.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Types and Its function , kingdom of life
Computing-Curriculum for Schools in Ghana
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
master seminar digital applications in india
STATICS OF THE RIGID BODIES Hibbelers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Final Presentation General Medicine 03-08-2024.pptx
GDM (1) (1).pptx small presentation for students

Data structure

  • 2. Searching: Finding the location of item or printing some message when item is not found. SEARCH 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 DATA[LOC] =ITEM LOC=LOC+1 DATA[LOC] =ITEM, LOC=LOC+1 DATA[LOC] =ITEM LOC=LOC+1 LOC=1 LOC=2 LOC=3
  • 11. A B C D E F G DATA[LOC] =ITEM, LOC=LOC+1 S E A R C H I N G A B C D E F G DATA[LOC] =ITEM, LOC=LOC+1 A B C D E F G DATA[LOC] =ITEM, LOC=LOC+1 LOC=4 LOC=5 LOC=6
  • 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 DATA[LOC] =ITEM LOC=7
  • 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. COMPLEXITY • Worst Case: The maximum value to search. • Complexity is measured by f(n).f(n)=n+1 • The average case is measured by probability. • Here, • pk – probability that ITEM appears in DATA[K]. • q – probability that ITEM does not appears in DATA[K]. • Algorithm uses k comparisons when ITEM appears in DATA[K].
  • 15. • f(n)=1 . p1 + 2 . p2 + … +n . pn + (n+1) . q • Suppose, q is very small and ITEm appears in equal probability, then q=o and pi=1/n f(n)=1 . 1/n + 2 . 1/n + … +n . 1/n + (n+1).0 =(1+2+…+n) .1/n =n(n+1)/2.(1/n) f(n)=(n+1)/2