SlideShare a Scribd company logo
1 of 22
Prepared By:
Dr. Chandan Kumar
Assistant Professor, Computer Science & Engineering Department
Invertis University, Bareilly
Introduction
 In computer science, searching play a vital role
 Helpful to find a desired element in a list
 Also helpful to find number of occurrence of a desired
element or a place of an given element in a list
Searching
 An operation or process or technique to find out a desired
element or the place of an element in a list
 It decides whether an element is present or not
 It is the algorithmic method of finding a particular element
in a list
 It can be done on both data structure i.e. internal or
external data structure
 Search is successful if the desired number is found in a list
 Search is unsuccessful if the desired number doesn't found
in a list
Searching Techniques
 To search or find out the desired element in a list or a given
array, there are two ways-
 Linear Search or Sequential Search
 Binary Search or Interval Search
Linear or Sequential Search
 Linear search is also called sequential search
 Simplest method of searching
 In this search technique, the item to be found in the search
is sequentially searched in the list
 Sequential search starts at the start of the list and checks
every item in the list
 This method can be performed on sorted or unsorted array
or list both
Linear Search
 We compare each element with the search item until the
list ends or we find it
 The list given above is the list of elements in an unsorted
array. The array has seven elements. Suppose the element
to be searched is ‘16', so 16 is compared with all the
elements starting from the 0th position element, and the
searching process ends where 16 is found, or the list ends.
Linear Search
 The desired number 16 has found at 6th position in an
array. so the search is successful
 The performance of the linear search can be measured by
counting the comparisons done to find out an element. The
number of comparisons is O(n) i.e. time complexity of
linear search is O(n).
Steps to implement linear search
 Step 1 - Read the search element from the user.
 Step 2 - Compare the search element with the first element
in the list.
 Step 3 - If both are matched, then display "Given element is
found!!!" and terminate the function
 Step 4 - If both are not matched, then compare search
element with the next element in the list.
 Step 5 - Repeat steps 3 and 4 until search element is
compared with last element in the list.
 Step 6 - If last element in the list also doesn't match, then
display "Element is not found!!!" and terminate the
function.
Algorithm
Linear Search (Array A[n], Number X)
Step 1: set i to 0
Step 2: if i>n then goto step 5
Step 3: if A[i]=X then goto step 6
Step 4: set i to i+1 and goto step 2
Step 5: print element X not found and goto step 7
Step 6: print element X found at index i
Step 7: Exit
Implementation in C
//Linear search
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], number, i, n, count=0;
clrscr();
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elements n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Enter a desired number to searchn");
scanf("%d", &number);
for (i = 0; i < n; i++)
{
if (array[i] == number)
{
count++;
break;
}
}
if (count==1)
printf("%d is present at location %d.n", number, i+1);
else
printf("%d isn't present in the array.n", number);
getch();
}
Output:
Binary Search
 Very fast and efficient searching technique
 Used for searching an element in a sorted array only
 If the array isn't sorted, we must sort it using a sorting
technique
 Fast search algorithm with run-time complexity of
O(log n) where n is total number of elements in the
list
 Works on the principle of divide and conquer
Binary Search
 This searching technique looks for a particular
element by comparing the middle most element of the
collection
 Useful when there are large number of elements in an
array
Steps to implement Binary Search
 Step 1 - Read the search element from the user.
 Step 2 - Find the middle element in the sorted list.
 Step 3 - Compare the search element with the middle
element in the sorted list.
 Step 4 - If both are matched, then display "Given
element is found!!!" and terminate the function.
 Step 5 - If both are not matched, then check whether
the search element is smaller or larger than the middle
element.
Steps to implement Binary Search
 Step 6 - If the search element is smaller than middle
element, repeat steps 2, 3, 4 and 5 for the left sublist of
the middle element.
 Step 7 - If the search element is larger than middle
element, repeat steps 2, 3, 4 and 5 for the right sublist
of the middle element.
 Step 8 - Repeat the same process until we find the
search element in the list or until sublist contains only
one element.
Steps to implement Binary Search
 Step 9 - If that element also doesn't match with the
search element, then display "Element is not found in
the list!!!" and terminate the function.
Algorithm
Binary Search (List,L_bound, H_bound, D_number)
Step 1: [Input] Input List, D_number
Step 2: [Initialize] set L_bound<- 0, H_bound<- Sizeof(List)-1
Step 3: repeat step 4 and 5 while L_bound<=H_bound
Step 4: set mid<- (L_bound+H_bound)/2
Step 5: if List[mid]=D_number then
return mid; //Desired Number found
else if D_number<List[mid] then
H_bound<- mid -1
else
L_bound<mid+1
Step 6: print "value is not present in the array“
Step 7: Exit
Implementation Binary search
#include<stdio.h>
#include<conio.h>
void main()
{
int first, last, middle, size, i, sElement, list[100];
clrscr();
printf("Enter the size of the list: ");
scanf("%d",&size);
printf("Enter %d integer values in Assending ordern", size);
for (i = 0; i < size; i++)
scanf("%d",&list[i]);
printf("Enter value to be search: ");
scanf("%d", &sElement);
first = 0;
last = size - 1;
middle = (first+last)/2;
while (first <= last)
{
if (list[middle] < sElement)
first = middle + 1;
else if (list[middle] == sElement)
{
printf("Element found at index %d.n",middle);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Element Not found in the list.");
getch();
}
Searching in c language

More Related Content

What's hot

What's hot (20)

Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Linked List
Linked ListLinked List
Linked List
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Linked lists
Linked listsLinked lists
Linked lists
 
Stack
StackStack
Stack
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stack project
Stack projectStack project
Stack project
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
stack presentation
stack presentationstack presentation
stack presentation
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 

Similar to Searching in c language

Similar to Searching in c language (20)

Searching
SearchingSearching
Searching
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
seaching internal 2 ppt
seaching internal 2 pptseaching internal 2 ppt
seaching internal 2 ppt
 
searching
searchingsearching
searching
 
Searching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxSearching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptx
 
Searching
Searching Searching
Searching
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
 
Linear and Binary search .pptx
Linear and Binary search .pptxLinear and Binary search .pptx
Linear and Binary search .pptx
 
seaching internal 2 ppt.pptx
seaching internal 2 ppt.pptxseaching internal 2 ppt.pptx
seaching internal 2 ppt.pptx
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
 
programming in C
programming in Cprogramming in C
programming in C
 
Linear Search for design and analysis of algorithm
Linear Search for design and analysis of algorithmLinear Search for design and analysis of algorithm
Linear Search for design and analysis of algorithm
 
Standard Algorithms
Standard AlgorithmsStandard Algorithms
Standard Algorithms
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 

More from CHANDAN KUMAR

Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language CHANDAN KUMAR
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithmCHANDAN KUMAR
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programmingCHANDAN KUMAR
 
Technical questions for interview c programming
Technical questions for interview  c programmingTechnical questions for interview  c programming
Technical questions for interview c programmingCHANDAN KUMAR
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statementCHANDAN KUMAR
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "CHANDAN KUMAR
 

More from CHANDAN KUMAR (13)

Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
 
Raid technology
Raid technologyRaid technology
Raid technology
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Linked List
Linked ListLinked List
Linked List
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Technical questions for interview c programming
Technical questions for interview  c programmingTechnical questions for interview  c programming
Technical questions for interview c programming
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statement
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
 

Recently uploaded

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 

Recently uploaded (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Searching in c language

  • 1. Prepared By: Dr. Chandan Kumar Assistant Professor, Computer Science & Engineering Department Invertis University, Bareilly
  • 2. Introduction  In computer science, searching play a vital role  Helpful to find a desired element in a list  Also helpful to find number of occurrence of a desired element or a place of an given element in a list
  • 3. Searching  An operation or process or technique to find out a desired element or the place of an element in a list  It decides whether an element is present or not  It is the algorithmic method of finding a particular element in a list  It can be done on both data structure i.e. internal or external data structure  Search is successful if the desired number is found in a list  Search is unsuccessful if the desired number doesn't found in a list
  • 4. Searching Techniques  To search or find out the desired element in a list or a given array, there are two ways-  Linear Search or Sequential Search  Binary Search or Interval Search
  • 5. Linear or Sequential Search  Linear search is also called sequential search  Simplest method of searching  In this search technique, the item to be found in the search is sequentially searched in the list  Sequential search starts at the start of the list and checks every item in the list  This method can be performed on sorted or unsorted array or list both
  • 6. Linear Search  We compare each element with the search item until the list ends or we find it  The list given above is the list of elements in an unsorted array. The array has seven elements. Suppose the element to be searched is ‘16', so 16 is compared with all the elements starting from the 0th position element, and the searching process ends where 16 is found, or the list ends.
  • 7. Linear Search  The desired number 16 has found at 6th position in an array. so the search is successful  The performance of the linear search can be measured by counting the comparisons done to find out an element. The number of comparisons is O(n) i.e. time complexity of linear search is O(n).
  • 8. Steps to implement linear search  Step 1 - Read the search element from the user.  Step 2 - Compare the search element with the first element in the list.  Step 3 - If both are matched, then display "Given element is found!!!" and terminate the function  Step 4 - If both are not matched, then compare search element with the next element in the list.  Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.  Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and terminate the function.
  • 9. Algorithm Linear Search (Array A[n], Number X) Step 1: set i to 0 Step 2: if i>n then goto step 5 Step 3: if A[i]=X then goto step 6 Step 4: set i to i+1 and goto step 2 Step 5: print element X not found and goto step 7 Step 6: print element X found at index i Step 7: Exit
  • 10. Implementation in C //Linear search #include <stdio.h> #include<conio.h> void main() { int array[100], number, i, n, count=0; clrscr(); printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elements n", n);
  • 11. for (i = 0; i < n; i++) { scanf("%d", &array[i]); } printf("Enter a desired number to searchn"); scanf("%d", &number); for (i = 0; i < n; i++) { if (array[i] == number) { count++; break; } }
  • 12. if (count==1) printf("%d is present at location %d.n", number, i+1); else printf("%d isn't present in the array.n", number); getch(); } Output:
  • 13. Binary Search  Very fast and efficient searching technique  Used for searching an element in a sorted array only  If the array isn't sorted, we must sort it using a sorting technique  Fast search algorithm with run-time complexity of O(log n) where n is total number of elements in the list  Works on the principle of divide and conquer
  • 14. Binary Search  This searching technique looks for a particular element by comparing the middle most element of the collection  Useful when there are large number of elements in an array
  • 15. Steps to implement Binary Search  Step 1 - Read the search element from the user.  Step 2 - Find the middle element in the sorted list.  Step 3 - Compare the search element with the middle element in the sorted list.  Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.  Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element.
  • 16. Steps to implement Binary Search  Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.  Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sublist of the middle element.  Step 8 - Repeat the same process until we find the search element in the list or until sublist contains only one element.
  • 17. Steps to implement Binary Search  Step 9 - If that element also doesn't match with the search element, then display "Element is not found in the list!!!" and terminate the function.
  • 18. Algorithm Binary Search (List,L_bound, H_bound, D_number) Step 1: [Input] Input List, D_number Step 2: [Initialize] set L_bound<- 0, H_bound<- Sizeof(List)-1 Step 3: repeat step 4 and 5 while L_bound<=H_bound Step 4: set mid<- (L_bound+H_bound)/2 Step 5: if List[mid]=D_number then return mid; //Desired Number found else if D_number<List[mid] then H_bound<- mid -1 else L_bound<mid+1 Step 6: print "value is not present in the array“ Step 7: Exit
  • 19. Implementation Binary search #include<stdio.h> #include<conio.h> void main() { int first, last, middle, size, i, sElement, list[100]; clrscr(); printf("Enter the size of the list: "); scanf("%d",&size); printf("Enter %d integer values in Assending ordern", size); for (i = 0; i < size; i++) scanf("%d",&list[i]);
  • 20. printf("Enter value to be search: "); scanf("%d", &sElement); first = 0; last = size - 1; middle = (first+last)/2; while (first <= last) { if (list[middle] < sElement) first = middle + 1; else if (list[middle] == sElement) { printf("Element found at index %d.n",middle); break; }
  • 21. else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Element Not found in the list."); getch(); }