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….. !!!

More Related Content

What's hot

Binary search
Binary searchBinary search
Binary search
AparnaKumari31
 
Searching
SearchingSearching
Searching
Ashim Lamichhane
 
Linear Search
Linear SearchLinear Search
Linear Search
SWATHIR72
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
Meherul1234
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
Talha Shaikh
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Binary search
Binary searchBinary search
Binary search
Gaurav Solanki
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Selection sort
Selection sortSelection sort
Selection sort
amna izzat
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and Analysis
AkashBorse2
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 

What's hot (20)

Binary search
Binary searchBinary search
Binary search
 
Searching
SearchingSearching
Searching
 
Linear Search
Linear SearchLinear Search
Linear Search
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Binary search
Binary searchBinary search
Binary search
 
Quick sort
Quick sortQuick sort
Quick sort
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Selection sort
Selection sortSelection sort
Selection sort
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and Analysis
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Viewers also liked

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
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
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 AlgorithmsKelly 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 2011Kelly Bauer
 
Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta LanguageKelly 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
 
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 RevolutionJohn 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
 

Viewers also liked (20)

Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sdd HSC Summary
Sdd HSC SummarySdd HSC Summary
Sdd HSC Summary
 
Meta Languages Railroad Diagrams
Meta Languages Railroad DiagramsMeta Languages Railroad Diagrams
Meta Languages Railroad Diagrams
 
Desk Chekcing Algorithms
Desk Chekcing AlgorithmsDesk Chekcing Algorithms
Desk Chekcing Algorithms
 
Meta Languages Bnf Ebnf Student Version
Meta Languages Bnf Ebnf Student VersionMeta Languages Bnf Ebnf Student Version
Meta Languages Bnf Ebnf Student Version
 
Exam feedback term 1 2011
Exam feedback term 1 2011Exam feedback term 1 2011
Exam feedback term 1 2011
 
Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta Language
 
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...
 
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
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Google
GoogleGoogle
Google
 
A history of science (volume 1)
A history of science (volume 1) A history of science (volume 1)
A history of science (volume 1)
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"
 
CSS 3, Style and Beyond
CSS 3, Style and BeyondCSS 3, Style and Beyond
CSS 3, Style and Beyond
 
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
 
Algorithms - Aaron Bloomfield
Algorithms - Aaron BloomfieldAlgorithms - Aaron Bloomfield
Algorithms - Aaron Bloomfield
 
Google at a glance 1998 - 2008
Google at a glance 1998 - 2008Google at a glance 1998 - 2008
Google at a glance 1998 - 2008
 
The Scientific Revolution
The Scientific RevolutionThe Scientific Revolution
The Scientific Revolution
 
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
 

Similar to Searching algorithms

Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
Dr.Umadevi V
 
arrays in c
arrays in carrays in c
arrays in c
vidhi mehta
 
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 ReddyMalikireddy Bramhananda Reddy
 
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
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
MuhammadUmerIhtisham
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer Science
Transweb Global Inc
 

Similar to Searching algorithms (20)

Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Unit08 dbms
Unit08 dbmsUnit08 dbms
Unit08 dbms
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
arrays in c
arrays in carrays in c
arrays in c
 
9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introduction
 
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
 
Chap10
Chap10Chap10
Chap10
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer Science
 

More from Trupti Agrawal

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
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
Trupti Agrawal
 

More from Trupti Agrawal (6)

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

Recently uploaded

CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 

Recently uploaded (20)

CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 

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