SlideShare a Scribd company logo
Searching 
algorithm
Searching Algorithms 
• Necessary components to search a list of data 
– Array containing the list 
– Length of the list 
– Item for which you are searching 
• After search completed 
– If item found, report “success,” return location in array 
– If item not found, report “not found” or “failure”
Searching Algorithms (Cont’d) 
• Suppose that you want to determine whether 27 is in the list 
• First compare 27 with list[0]; that is, compare 27 with 35 
• Because list[0] ≠ 27, you then compare 27 with list[1] 
• Because list[1] ≠ 27, you compare 27 with the next element in 
the list 
• Because list[2] = 27, the search stops 
• This search is successful! 
Figure 1: Array list with seven (07) elements
Searching Algorithms (Cont’d) 
Let’s now search for 10 
The search starts at the first element in the list; that is, at 
list[0] 
Proceeding as before, we see that this time the search item, 
which is 10, is compared with every item in the list 
Eventually, no more data is left in the list to compare with 
the search item; this is an unsuccessful search
Sequential Search Algorithm 
The previous could be further reduced to: 
public static int linSearch(int[] list, int listLength, int key) { 
int loc; 
boolean found = false; 
for(int loc = 0; loc < listLength; loc++) { 
if(list[loc] == key) { 
found = true; 
break; 
} 
} 
if(found) 
return loc; 
else 
return -1; 
}
Sequential Search Algorithm (Cont’d) 
public static int linSearch(int[] list, int listLength, int key) { 
int loc; 
for(int loc = 0; loc < listLength; loc++) { 
if(list[loc] == key) 
return loc; 
} 
return -1; 
}
Sequential Search Algorithm (Cont’d) 
• Using a while (or a for) loop, the definition of the method 
seqSearch can also be written without the break statement as: 
public static int linSearch(int[] list, int listLength, int key) { 
int loc = 0; 
boolean found = false; 
while(loc < listLength && !found) { 
if(list[loc] == key) 
found = true; 
else 
loc++ 
} 
if(found) 
return loc; 
else 
return -1; 
}
Performance of the Sequential Search 
• Suppose that the first element in the array list contains the 
variable key, then we have performed one comparison to find 
the key. 
• Suppose that the second element in the array list contains the 
variable key, then we have performed two comparisons to find 
the key. 
• Carry on the same analysis till the key is contained in the last 
element of the array list. In this case, we have performed N 
comparisons (N is the size of the array list) to find the key. 
• Finally if the key is NOT in the array list, then we would have 
performed N comparisons and the key is NOT found and we 
would return -1.
Performance of the Sequential Search (Cont’d) 
• Therefore, the best case is: 1 
• And, the worst case is: N 
• The average case is: 
1 + 2 + 3 + …..+ N + N 
N+1 
Best case 
Average Number of 
Comparisons 
Worst case and key found at the end of 
the array list! 
Worst case and key is NOT found! 
= 
Number of possible cases
Binary Search Algorithm 
Can only be performed on a sorted list !!! 
Uses divide and conquer technique to search list
Binary Search Algorithm (Cont’d) 
Search item is compared with middle element of list 
If search item < middle element of list, search is restricted 
to first half of the list 
If search item > middle element of list, search second half 
of the list 
If search item = middle element, search is complete
Binary Search Algorithm (Cont’d) 
• Determine whether 75 is in the list 
Figure 2: Array list with twelve (12) elements 
Figure 3: Search list, list[0] … list[11]
Binary Search Algorithm (Cont’d) 
Figure 4: Search list, list[6] … list[11]
Binary Search Algorithm (Cont’d) 
public static int binarySearch(int[] list, int listLength, int key) { 
int first = 0, last = listLength - 1; 
int mid; 
boolean found = false; 
while (first <= last && !found) { 
mid = (first + last) / 2; 
if (list[mid] == key) 
found = true; 
else 
if(list[mid] > key) 
last = mid - 1; 
else 
first = mid + 1; 
} 
if (found) 
return mid; 
else 
return –1; 
} //end binarySearch
Binary Search Algorithm (Cont’d) 
Figure 5: Sorted list for binary search 
key = 89 
key = 34
Binary Search Algorithm (Cont’d) 
key = 22 
Figure 6: Sorted list for binary search
Indexed Search 
• Indexes: Data structures to organize records to 
optimize certain kinds of retrieval operations. 
o Speed up searches for a subset of records, based on values in 
certain (“search key”) fields 
o Updates are much faster than in sorted files.
Alternatives for Data Entry k* in 
Index 
Data Entry : Records stored in index file 
Given search key value k, provide for efficient retrieval of 
all data entries k* with value k. 
In a data entry k* , alternatives include that we can store: 
 alternative 1: Full data record with key value k, or 
 alternative 2: <k, rid of data record with search key value k>, or 
 alternative 3: <k, list of rids of data records with search key k> 
Choice of above 3 alternative data entries is orthogonal to indexing 
technique used to locate data entries. 
 Example indexing techniques: B+ trees, hash-based structures, etc.
Alternatives for Data Entries 
Alternative 1: Full data record with key value k 
Index structure is file organization for data records (instead 
of a Heap file or sorted file). 
At most one index on a given collection of data records 
can use Alternative 1. 
Otherwise, data records are duplicated, leading to 
redundant storage and potential inconsistency. 
If data records are very large, this implies size of auxiliary 
information in index is also large.
Alternatives for Data Entries 
Alternatives 2 (<k, rid>) and 3 (<k, list-of-rids>): 
Data entries typically much smaller than data records. 
Comparison: 
Both better than Alternative 1 with large data records, 
especially if search keys are small. 
Alternative 3 more compact than Alternative 2, 
but leads to variable sized data entries even if search keys 
are of fixed length.
Index Classification 
Clustered vs. unclustered index : 
If order of data records is the same as, or `close to’, 
order of data entries, then called clustered index.
Index Clustered vs Unclustered 
Observation 1: 
Alternative 1 implies clustered. True ? 
Observation 2: 
In practice, clustered also implies Alternative 1 (since 
sorted files are rare). 
Observation 3: 
A file can be clustered on at most one search key. 
Observation 4: 
Cost of retrieving data records through index varies 
greatly based on whether index is clustered or not !!
Index Clustered vs Unclustered 
Observation 1: 
Alternative 1 implies clustered. True ? 
Observation 2: 
In practice, clustered also implies Alternative 1 (since 
sorted files are rare). 
Observation 3: 
A file can be clustered on at most one search key. 
Observation 4: 
Cost of retrieving data records through index varies 
greatly based on whether index is clustered or not !!
Clustered vs. Unclustered Index 
Index entries 
CLUSTERED direct search for 
UNCLUSTERED 
data entries 
Data entries 
(Index File) 
(Data file) 
Data Records 
Data entries 
Data Records 
Suppose Alternative (2) is used for data entries.
Clustered vs. Unclustered Index 
Use Alternative (2) for data entries 
Data records are stored in Heap file. 
To build clustered index, first sort the Heap file 
Overflow pages may be needed for inserts. 
Thus, order of data recs is close to (not identical to) 
sort order. 
Index entries 
CLUSTERED direct search for 
UNCLUSTERED 
data entries 
Data entries 
(Index File) 
(Data file) 
Data Records 
Data entries 
Data Records
Summary of Index Search 
Many alternative file organizations exist, each appropriate in 
some situation. 
If selection queries are frequent, sorting the file or building an 
index is important. 
 Hash-based indexes only good for equality search. 
 Sorted files and tree-based indexes best for range search; also 
good for equality search. 
 Files rarely kept sorted in practice; B+ tree index is better. 
Index is a collection of data entries plus a way to quickly find 
entries with given key values.
Summary of Index Search 
 Data entries can be : 
 actual data records, 
 <key, rid> pairs, or 
 <key, rid-list> pairs. 
 Can have several indexes on a given file of data 
records, each with a different search key. 
 Indexes can be classified as clustered vs. unclustered, 
 Differences have important consequences for 
utility/performance of query processing
THANK YOU….. !!!
Ad

More Related Content

What's hot (20)

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
Talha Shaikh
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and Analysis
AkashBorse2
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
irdginfo
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Binary search
Binary searchBinary search
Binary search
AparnaKumari31
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
Monalisa Patel
 
Selection sort
Selection sortSelection sort
Selection sort
stella D
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
Talha Shaikh
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and Analysis
AkashBorse2
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
irdginfo
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith
 
Selection sort
Selection sortSelection sort
Selection sort
stella D
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 

Viewers also liked (20)

Sdd HSC Summary
Sdd HSC SummarySdd HSC Summary
Sdd HSC Summary
mary_ramsay
 
Meta Languages Railroad Diagrams
Meta Languages Railroad DiagramsMeta Languages Railroad Diagrams
Meta Languages Railroad Diagrams
Kelly Bauer
 
Desk Chekcing Algorithms
Desk Chekcing AlgorithmsDesk Chekcing Algorithms
Desk Chekcing Algorithms
Kelly Bauer
 
Meta Languages Bnf Ebnf Student Version
Meta Languages Bnf Ebnf Student VersionMeta Languages Bnf Ebnf Student Version
Meta Languages Bnf Ebnf Student Version
Kelly Bauer
 
Exam feedback term 1 2011
Exam feedback term 1 2011Exam feedback term 1 2011
Exam feedback term 1 2011
Kelly Bauer
 
Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta Language
Kelly Bauer
 
How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...
American Astronautical Society
 
History of Google Local from 2004-2011
History of Google Local from 2004-2011 History of Google Local from 2004-2011
History of Google Local from 2004-2011
Mike Blumenthal
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
Google
GoogleGoogle
Google
vibhabehl
 
A history of science (volume 1)
A history of science (volume 1) A history of science (volume 1)
A history of science (volume 1)
Dipoceanov Esrever
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"
John Lynch
 
CSS 3, Style and Beyond
CSS 3, Style and BeyondCSS 3, Style and Beyond
CSS 3, Style and Beyond
Samsung Open Source Group
 
Introduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_aIntroduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_a
Shahi Raz Akhtar
 
Algorithms - Aaron Bloomfield
Algorithms - Aaron BloomfieldAlgorithms - Aaron Bloomfield
Algorithms - Aaron Bloomfield
Reggie Niccolo Santos
 
Google at a glance 1998 - 2008
Google at a glance 1998 - 2008Google at a glance 1998 - 2008
Google at a glance 1998 - 2008
Andreas Jaffke
 
The Scientific Revolution
The Scientific RevolutionThe Scientific Revolution
The Scientific Revolution
John Lynch
 
National Air And Space Museum Washington DC
National Air And Space Museum Washington DCNational Air And Space Museum Washington DC
National Air And Space Museum Washington DC
Shivakumar Patil
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
John Lynch
 
One Long Argument
One Long ArgumentOne Long Argument
One Long Argument
John Lynch
 
Meta Languages Railroad Diagrams
Meta Languages Railroad DiagramsMeta Languages Railroad Diagrams
Meta Languages Railroad Diagrams
Kelly Bauer
 
Desk Chekcing Algorithms
Desk Chekcing AlgorithmsDesk Chekcing Algorithms
Desk Chekcing Algorithms
Kelly Bauer
 
Meta Languages Bnf Ebnf Student Version
Meta Languages Bnf Ebnf Student VersionMeta Languages Bnf Ebnf Student Version
Meta Languages Bnf Ebnf Student Version
Kelly Bauer
 
Exam feedback term 1 2011
Exam feedback term 1 2011Exam feedback term 1 2011
Exam feedback term 1 2011
Kelly Bauer
 
Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta Language
Kelly Bauer
 
History of Google Local from 2004-2011
History of Google Local from 2004-2011 History of Google Local from 2004-2011
History of Google Local from 2004-2011
Mike Blumenthal
 
A history of science (volume 1)
A history of science (volume 1) A history of science (volume 1)
A history of science (volume 1)
Dipoceanov Esrever
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"
John Lynch
 
Introduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_aIntroduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_a
Shahi Raz Akhtar
 
Google at a glance 1998 - 2008
Google at a glance 1998 - 2008Google at a glance 1998 - 2008
Google at a glance 1998 - 2008
Andreas Jaffke
 
The Scientific Revolution
The Scientific RevolutionThe Scientific Revolution
The Scientific Revolution
John Lynch
 
National Air And Space Museum Washington DC
National Air And Space Museum Washington DCNational Air And Space Museum Washington DC
National Air And Space Museum Washington DC
Shivakumar Patil
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
John Lynch
 
One Long Argument
One Long ArgumentOne Long Argument
One Long Argument
John Lynch
 
Ad

Similar to Searching algorithms (20)

Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Unit08 dbms
Unit08 dbmsUnit08 dbms
Unit08 dbms
arnold 7490
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
Dr.Umadevi V
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
Terry Yoast
 
06-search-ar121r11111ay-1-LinearBinary.ppt
06-search-ar121r11111ay-1-LinearBinary.ppt06-search-ar121r11111ay-1-LinearBinary.ppt
06-search-ar121r11111ay-1-LinearBinary.ppt
KamalakarHegde
 
arrays in c
arrays in carrays in c
arrays in c
vidhi mehta
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptxLecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptxLecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
Searching & Algorithms IN DATA STRUCTURES
Searching & Algorithms IN DATA STRUCTURESSearching & Algorithms IN DATA STRUCTURES
Searching & Algorithms IN DATA STRUCTURES
RAKSHITDOGRA1
 
9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt
ValhallaExcalibur
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introduction
Sugandh Wafai
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
Chap10
Chap10Chap10
Chap10
Terry Yoast
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
SivaSankar Gorantla
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
babuk110
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
SSN College of Engineering, Kalavakkam
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
Dr.Shweta
 
data structures queue stack insert and delete time complexity
data structures queue stack insert and delete time complexitydata structures queue stack insert and delete time complexity
data structures queue stack insert and delete time complexity
libannpost
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
06-search-ar121r11111ay-1-LinearBinary.ppt
06-search-ar121r11111ay-1-LinearBinary.ppt06-search-ar121r11111ay-1-LinearBinary.ppt
06-search-ar121r11111ay-1-LinearBinary.ppt
KamalakarHegde
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptxLecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptxLecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
Searching & Algorithms IN DATA STRUCTURES
Searching & Algorithms IN DATA STRUCTURESSearching & Algorithms IN DATA STRUCTURES
Searching & Algorithms IN DATA STRUCTURES
RAKSHITDOGRA1
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introduction
Sugandh Wafai
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
babuk110
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
Dr.Shweta
 
data structures queue stack insert and delete time complexity
data structures queue stack insert and delete time complexitydata structures queue stack insert and delete time complexity
data structures queue stack insert and delete time complexity
libannpost
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Ad

More from Trupti Agrawal (6)

Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Trupti Agrawal
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
Trupti Agrawal
 
Arrays
ArraysArrays
Arrays
Trupti Agrawal
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
Trupti Agrawal
 

Recently uploaded (20)

antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 

Searching algorithms

  • 2. Searching Algorithms • Necessary components to search a list of data – Array containing the list – Length of the list – Item for which you are searching • After search completed – If item found, report “success,” return location in array – If item not found, report “not found” or “failure”
  • 3. Searching Algorithms (Cont’d) • Suppose that you want to determine whether 27 is in the list • First compare 27 with list[0]; that is, compare 27 with 35 • Because list[0] ≠ 27, you then compare 27 with list[1] • Because list[1] ≠ 27, you compare 27 with the next element in the list • Because list[2] = 27, the search stops • This search is successful! Figure 1: Array list with seven (07) elements
  • 4. Searching Algorithms (Cont’d) Let’s now search for 10 The search starts at the first element in the list; that is, at list[0] Proceeding as before, we see that this time the search item, which is 10, is compared with every item in the list Eventually, no more data is left in the list to compare with the search item; this is an unsuccessful search
  • 5. Sequential Search Algorithm The previous could be further reduced to: public static int linSearch(int[] list, int listLength, int key) { int loc; boolean found = false; for(int loc = 0; loc < listLength; loc++) { if(list[loc] == key) { found = true; break; } } if(found) return loc; else return -1; }
  • 6. Sequential Search Algorithm (Cont’d) public static int linSearch(int[] list, int listLength, int key) { int loc; for(int loc = 0; loc < listLength; loc++) { if(list[loc] == key) return loc; } return -1; }
  • 7. Sequential Search Algorithm (Cont’d) • Using a while (or a for) loop, the definition of the method seqSearch can also be written without the break statement as: public static int linSearch(int[] list, int listLength, int key) { int loc = 0; boolean found = false; while(loc < listLength && !found) { if(list[loc] == key) found = true; else loc++ } if(found) return loc; else return -1; }
  • 8. Performance of the Sequential Search • Suppose that the first element in the array list contains the variable key, then we have performed one comparison to find the key. • Suppose that the second element in the array list contains the variable key, then we have performed two comparisons to find the key. • Carry on the same analysis till the key is contained in the last element of the array list. In this case, we have performed N comparisons (N is the size of the array list) to find the key. • Finally if the key is NOT in the array list, then we would have performed N comparisons and the key is NOT found and we would return -1.
  • 9. Performance of the Sequential Search (Cont’d) • Therefore, the best case is: 1 • And, the worst case is: N • The average case is: 1 + 2 + 3 + …..+ N + N N+1 Best case Average Number of Comparisons Worst case and key found at the end of the array list! Worst case and key is NOT found! = Number of possible cases
  • 10. Binary Search Algorithm Can only be performed on a sorted list !!! Uses divide and conquer technique to search list
  • 11. Binary Search Algorithm (Cont’d) Search item is compared with middle element of list If search item < middle element of list, search is restricted to first half of the list If search item > middle element of list, search second half of the list If search item = middle element, search is complete
  • 12. Binary Search Algorithm (Cont’d) • Determine whether 75 is in the list Figure 2: Array list with twelve (12) elements Figure 3: Search list, list[0] … list[11]
  • 13. Binary Search Algorithm (Cont’d) Figure 4: Search list, list[6] … list[11]
  • 14. Binary Search Algorithm (Cont’d) public static int binarySearch(int[] list, int listLength, int key) { int first = 0, last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == key) found = true; else if(list[mid] > key) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch
  • 15. Binary Search Algorithm (Cont’d) Figure 5: Sorted list for binary search key = 89 key = 34
  • 16. Binary Search Algorithm (Cont’d) key = 22 Figure 6: Sorted list for binary search
  • 17. Indexed Search • Indexes: Data structures to organize records to optimize certain kinds of retrieval operations. o Speed up searches for a subset of records, based on values in certain (“search key”) fields o Updates are much faster than in sorted files.
  • 18. Alternatives for Data Entry k* in Index Data Entry : Records stored in index file Given search key value k, provide for efficient retrieval of all data entries k* with value k. In a data entry k* , alternatives include that we can store:  alternative 1: Full data record with key value k, or  alternative 2: <k, rid of data record with search key value k>, or  alternative 3: <k, list of rids of data records with search key k> Choice of above 3 alternative data entries is orthogonal to indexing technique used to locate data entries.  Example indexing techniques: B+ trees, hash-based structures, etc.
  • 19. Alternatives for Data Entries Alternative 1: Full data record with key value k Index structure is file organization for data records (instead of a Heap file or sorted file). At most one index on a given collection of data records can use Alternative 1. Otherwise, data records are duplicated, leading to redundant storage and potential inconsistency. If data records are very large, this implies size of auxiliary information in index is also large.
  • 20. Alternatives for Data Entries Alternatives 2 (<k, rid>) and 3 (<k, list-of-rids>): Data entries typically much smaller than data records. Comparison: Both better than Alternative 1 with large data records, especially if search keys are small. Alternative 3 more compact than Alternative 2, but leads to variable sized data entries even if search keys are of fixed length.
  • 21. Index Classification Clustered vs. unclustered index : If order of data records is the same as, or `close to’, order of data entries, then called clustered index.
  • 22. Index Clustered vs Unclustered Observation 1: Alternative 1 implies clustered. True ? Observation 2: In practice, clustered also implies Alternative 1 (since sorted files are rare). Observation 3: A file can be clustered on at most one search key. Observation 4: Cost of retrieving data records through index varies greatly based on whether index is clustered or not !!
  • 23. Index Clustered vs Unclustered Observation 1: Alternative 1 implies clustered. True ? Observation 2: In practice, clustered also implies Alternative 1 (since sorted files are rare). Observation 3: A file can be clustered on at most one search key. Observation 4: Cost of retrieving data records through index varies greatly based on whether index is clustered or not !!
  • 24. Clustered vs. Unclustered Index Index entries CLUSTERED direct search for UNCLUSTERED data entries Data entries (Index File) (Data file) Data Records Data entries Data Records Suppose Alternative (2) is used for data entries.
  • 25. Clustered vs. Unclustered Index Use Alternative (2) for data entries Data records are stored in Heap file. To build clustered index, first sort the Heap file Overflow pages may be needed for inserts. Thus, order of data recs is close to (not identical to) sort order. Index entries CLUSTERED direct search for UNCLUSTERED data entries Data entries (Index File) (Data file) Data Records Data entries Data Records
  • 26. Summary of Index Search Many alternative file organizations exist, each appropriate in some situation. If selection queries are frequent, sorting the file or building an index is important.  Hash-based indexes only good for equality search.  Sorted files and tree-based indexes best for range search; also good for equality search.  Files rarely kept sorted in practice; B+ tree index is better. Index is a collection of data entries plus a way to quickly find entries with given key values.
  • 27. Summary of Index Search  Data entries can be :  actual data records,  <key, rid> pairs, or  <key, rid-list> pairs.  Can have several indexes on a given file of data records, each with a different search key.  Indexes can be classified as clustered vs. unclustered,  Differences have important consequences for utility/performance of query processing
  翻译: