SlideShare a Scribd company logo
Searching in Arrays
1
Instructor
Dhiviya Rose J , AP-Sr. Scale | SoCSE
INFO 106 – Computer Programming
Searching Arrays: Linear Search and
Binary Search
• Search an array for a key value
• Linear search
▫ Simple
▫ Compare each element of array with key value
▫ Useful for small and unsorted arrays
2
INFO 106 – Computer Programming
Linear Search
• Step through array of records, one at a time.
• Look for record with matching key.
• Search stops when
▫ record with matching key is found
▫ or when search has examined all records without
success.
INFO 106 – Computer Programming
How Linear Search works
4
5
Case 1: Target
found
6
Case 1: Target NOT
found
INFO 106 – Computer Programming
7
Program
#include<stdio.h>
void main()
{
int a[10],i,target;
printf("Enter array value n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Which value to be search ->");
scanf("%d",&target);
/* Linear Search logic */
for(i=0;i<n;i++)
if(target==a[i])
{
printf(“Value found at %d”,i);
}
}
INFO 106 – Computer Programming
Advantages of Linear Search
• Don't have to sort the data we search.
• Works well if only search operation is minimum
• Not optimal in case of large amount of data
8
INFO 106 – Computer Programming
Binary Search
• Assume that we are give an array of records that
is sorted. For instance:
▫ an array of records with integer keys sorted from
smallest to largest (e.g., ID numbers), or
▫ an array of records with string keys sorted in
alphabetical order (e.g., names).
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
11
INFO 106 – Computer Programming
Binary Search Pseudocode
…
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
…
INFO 106 – Computer Programming
Program
int result=-1;
int low=0;
int high=length-1;
int mid;
while( result==-1 && low<=high )
{ mid= low + ((high - low) / 2);
if( list[mid] == target )
result = mid;
else if( list[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
13
INFO 106 – Computer Programming
Other Searching Algorithms
• Interpolation Search
• Indexed Searching
• Binary Search Trees
• Hash Table Searching
• Grover's Algorithm
• Best-first
• A*
14
Ad

More Related Content

What's hot (20)

Stack
StackStack
Stack
Seema Sharma
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Data structure
Data structureData structure
Data structure
Muhammad Farhan
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
sahil kumar
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Strings
StringsStrings
Strings
Nilesh Dalvi
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
DivyeshKumar Jagatiya
 
Stack
StackStack
Stack
Zaid Shabbir
 
Array operations
Array operationsArray operations
Array operations
ZAFAR444
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Abhishek L.R
 
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
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
channa basava
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
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
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
sahil kumar
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Array operations
Array operationsArray operations
Array operations
ZAFAR444
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Abhishek L.R
 
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
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
channa basava
 
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
 

Similar to Searching in Arrays (20)

Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
dsa pdf.pdf
dsa pdf.pdfdsa pdf.pdf
dsa pdf.pdf
DewashishDwivedi
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
Durga Devi
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
MG Thushara Pradeesh
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
Dhiviya Rose
 
Arrays
ArraysArrays
Arrays
Aman Agarwal
 
Data Structures and Algorithms (DSA) in C
Data Structures and Algorithms (DSA) in CData Structures and Algorithms (DSA) in C
Data Structures and Algorithms (DSA) in C
Nabajyoti Banik
 
placement preparation for Array Searching.pptx
placement preparation for Array Searching.pptxplacement preparation for Array Searching.pptx
placement preparation for Array Searching.pptx
upasnatiwari10
 
Data structure presentation sorting
Data structure presentation sortingData structure presentation sorting
Data structure presentation sorting
Pranjali Rawat
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Janpreet Singh
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
p83629918
 
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
mrhabib10
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
rasheed747195
 
Searching inear Search And Binary Agorithms
Searching inear Search And Binary AgorithmsSearching inear Search And Binary Agorithms
Searching inear Search And Binary Agorithms
PuneetVashistha1
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Chapter 2. data structure and algorithm
Chapter  2. data structure and algorithmChapter  2. data structure and algorithm
Chapter 2. data structure and algorithm
SolomonEndalu
 
Zoho Interview Questions By Scholarhat.pdf
Zoho Interview Questions By Scholarhat.pdfZoho Interview Questions By Scholarhat.pdf
Zoho Interview Questions By Scholarhat.pdf
Scholarhat
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
Ajharul Abedeen
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
Durga Devi
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
Dhiviya Rose
 
Data Structures and Algorithms (DSA) in C
Data Structures and Algorithms (DSA) in CData Structures and Algorithms (DSA) in C
Data Structures and Algorithms (DSA) in C
Nabajyoti Banik
 
placement preparation for Array Searching.pptx
placement preparation for Array Searching.pptxplacement preparation for Array Searching.pptx
placement preparation for Array Searching.pptx
upasnatiwari10
 
Data structure presentation sorting
Data structure presentation sortingData structure presentation sorting
Data structure presentation sorting
Pranjali Rawat
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
p83629918
 
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
mrhabib10
 
Searching inear Search And Binary Agorithms
Searching inear Search And Binary AgorithmsSearching inear Search And Binary Agorithms
Searching inear Search And Binary Agorithms
PuneetVashistha1
 
Chapter 2. data structure and algorithm
Chapter  2. data structure and algorithmChapter  2. data structure and algorithm
Chapter 2. data structure and algorithm
SolomonEndalu
 
Zoho Interview Questions By Scholarhat.pdf
Zoho Interview Questions By Scholarhat.pdfZoho Interview Questions By Scholarhat.pdf
Zoho Interview Questions By Scholarhat.pdf
Scholarhat
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
Ajharul Abedeen
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Ad

More from Dhiviya Rose (15)

Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2
Dhiviya Rose
 
Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1
Dhiviya Rose
 
Module 3 microsoft powerpoint
Module 3 microsoft powerpointModule 3 microsoft powerpoint
Module 3 microsoft powerpoint
Dhiviya Rose
 
Module 3 business computing.pdf
Module 3 business computing.pdfModule 3 business computing.pdf
Module 3 business computing.pdf
Dhiviya Rose
 
Module 2 Digital Devices and its Applications
Module 2 Digital Devices and its ApplicationsModule 2 Digital Devices and its Applications
Module 2 Digital Devices and its Applications
Dhiviya Rose
 
Software
SoftwareSoftware
Software
Dhiviya Rose
 
Unit 1 Business Computing
Unit 1 Business ComputingUnit 1 Business Computing
Unit 1 Business Computing
Dhiviya Rose
 
Module 1 - Digital Devices and its Application
Module 1 - Digital Devices and its ApplicationModule 1 - Digital Devices and its Application
Module 1 - Digital Devices and its Application
Dhiviya Rose
 
CSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and PointersCSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and Pointers
Dhiviya Rose
 
CSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and UnionsCSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and Unions
Dhiviya Rose
 
CSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming FundamentalsCSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming Fundamentals
Dhiviya Rose
 
CSEG1001 Lecture 1 Introduction to Computers
CSEG1001 Lecture 1 Introduction to ComputersCSEG1001 Lecture 1 Introduction to Computers
CSEG1001 Lecture 1 Introduction to Computers
Dhiviya Rose
 
Lecture 3 internet and web
Lecture 3 internet and webLecture 3 internet and web
Lecture 3 internet and web
Dhiviya Rose
 
Strings
StringsStrings
Strings
Dhiviya Rose
 
Multidimentional array
Multidimentional arrayMultidimentional array
Multidimentional array
Dhiviya Rose
 
Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2
Dhiviya Rose
 
Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1
Dhiviya Rose
 
Module 3 microsoft powerpoint
Module 3 microsoft powerpointModule 3 microsoft powerpoint
Module 3 microsoft powerpoint
Dhiviya Rose
 
Module 3 business computing.pdf
Module 3 business computing.pdfModule 3 business computing.pdf
Module 3 business computing.pdf
Dhiviya Rose
 
Module 2 Digital Devices and its Applications
Module 2 Digital Devices and its ApplicationsModule 2 Digital Devices and its Applications
Module 2 Digital Devices and its Applications
Dhiviya Rose
 
Unit 1 Business Computing
Unit 1 Business ComputingUnit 1 Business Computing
Unit 1 Business Computing
Dhiviya Rose
 
Module 1 - Digital Devices and its Application
Module 1 - Digital Devices and its ApplicationModule 1 - Digital Devices and its Application
Module 1 - Digital Devices and its Application
Dhiviya Rose
 
CSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and PointersCSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and Pointers
Dhiviya Rose
 
CSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and UnionsCSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and Unions
Dhiviya Rose
 
CSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming FundamentalsCSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming Fundamentals
Dhiviya Rose
 
CSEG1001 Lecture 1 Introduction to Computers
CSEG1001 Lecture 1 Introduction to ComputersCSEG1001 Lecture 1 Introduction to Computers
CSEG1001 Lecture 1 Introduction to Computers
Dhiviya Rose
 
Lecture 3 internet and web
Lecture 3 internet and webLecture 3 internet and web
Lecture 3 internet and web
Dhiviya Rose
 
Multidimentional array
Multidimentional arrayMultidimentional array
Multidimentional array
Dhiviya Rose
 
Ad

Recently uploaded (20)

THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdfAPM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
Association for Project Management
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 

Searching in Arrays

  • 1. Searching in Arrays 1 Instructor Dhiviya Rose J , AP-Sr. Scale | SoCSE
  • 2. INFO 106 – Computer Programming Searching Arrays: Linear Search and Binary Search • Search an array for a key value • Linear search ▫ Simple ▫ Compare each element of array with key value ▫ Useful for small and unsorted arrays 2
  • 3. INFO 106 – Computer Programming Linear Search • Step through array of records, one at a time. • Look for record with matching key. • Search stops when ▫ record with matching key is found ▫ or when search has examined all records without success.
  • 4. INFO 106 – Computer Programming How Linear Search works 4
  • 6. 6 Case 1: Target NOT found
  • 7. INFO 106 – Computer Programming 7 Program #include<stdio.h> void main() { int a[10],i,target; printf("Enter array value n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Which value to be search ->"); scanf("%d",&target); /* Linear Search logic */ for(i=0;i<n;i++) if(target==a[i]) { printf(“Value found at %d”,i); } }
  • 8. INFO 106 – Computer Programming Advantages of Linear Search • Don't have to sort the data we search. • Works well if only search operation is minimum • Not optimal in case of large amount of data 8
  • 9. INFO 106 – Computer Programming Binary Search • Assume that we are give an array of records that is sorted. For instance: ▫ an array of records with integer keys sorted from smallest to largest (e.g., ID numbers), or ▫ an array of records with string keys sorted in alphabetical order (e.g., names).
  • 10. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
  • 11. 11
  • 12. INFO 106 – Computer Programming Binary Search Pseudocode … if(size == 0) found = false; else { middle = index of approximate midpoint of array segment; if(target == a[middle]) target has been found! else if(target < a[middle]) search for target in area before midpoint; else search for target in area after midpoint; } …
  • 13. INFO 106 – Computer Programming Program int result=-1; int low=0; int high=length-1; int mid; while( result==-1 && low<=high ) { mid= low + ((high - low) / 2); if( list[mid] == target ) result = mid; else if( list[mid] < target) low = mid + 1; else high = mid - 1; } 13
  • 14. INFO 106 – Computer Programming Other Searching Algorithms • Interpolation Search • Indexed Searching • Binary Search Trees • Hash Table Searching • Grover's Algorithm • Best-first • A* 14