SlideShare a Scribd company logo
1 of 9
Download to read offline
Searching
 Searching is a process of finding a particular element among several given elements.
 The search is successful if the required element is found.
 Otherwise, the search is unsuccessful.
Searching Algorithms
The searching of an element in the given array may be carried out in the following two ways-
1. Linear Search
2. Binary Search
Linear Search
 Linear Search is the simplest searching algorithm.
 It traverses the array sequentially to locate the required element.
 It searches for an element by comparing it with each element of the array one by one.
 So, it is also called as Sequential Search.
Linear Search Algorithm is applied when
 No information is given about the array.
 The given array is unsorted or the elements are unordered.
 The list of data items is smaller.
Algorithm for linear searching
1. Input: A set of data in array a, and variable x. i.e., the target element
A[1….n];
item=x;
location=0;
2. Search the list to find the target element:
for (i=1; i<=n; i++)
{
if (A[i]==item)
{
print “Found”;
location=i;
stop searching;
}
}
3. if (i>n) print ” Not Found”;
4. Output: Found or Not Found.
Example
Consider the following list of elements and the element to be searched...
list
1 2 3 4 5 6 7 8
65 20 10 55 32 12 50 99
Search element 12
Step 1: Search element (12) is compared with first element (65)
list
1 2 3 4 5 6 7 8
65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 2: Search element (12) is compared with next element (20)
list
1 2 3 4 5 6 7 8
65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 3: Search element (12) is compared with next element (10)
list
1 2 3 4 5 6 7 8
65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 4: Search element (12) is compared with next element (55)
list
1 2 3 4 5 6 7 8
65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 5: Search element (12) is compared with next element (32)
list
1 2 3 4 5 6 7 8
65 20 10 55 32 12 50 99
12
Both are not matching. So, move to next element.
Step 6: Search element (12) is compared with next element (12)
list
1 2 3 4 5 6 7 8
65 20 10 55 32 12 50 99
12
Both are matching. So, we stop comparing and display element found at index 6.
Sample Code (Single Occurrence of element):
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in arrayn");
scanf("%d",&n);
printf("Enter %d integer(s)n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.n", search);
return 0;
}
Sample Output
Case 1:
Enter the number of elements in array
5
Enter 5 integer(s)
1 4 5 6 6
Enter the number to search
6
6 is present at location 4.
Case 2:
Enter the number of elements in array
5
Enter 5 integer(s)
1 2 3 4 7
Enter the number to search
8
8 is not present in array.
Sample Code (Multiple Occurrence of element):
#include <stdio.h>
int main()
{
int array[100], search, c, n, count = 0;
printf("Enter the number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d numbersn", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
for (c = 0; c < n; c++) {
if (array[c] == search) {
printf("%d is present at location %d.n", search, c+1);
count++;
}
}
if (count == 0)
printf("%d is not present in array.n", search);
else
printf("%d is present %d times in array.n", search,
count);
return 0;
}
Sample Output
Enter the number of elements in array
5
Enter 5 numbers
1 2 3 3 4
Enter the number to search
3
3 is present at location 3.
3 is present at location 4.
3 is present 2 times in array.
Binary Search
 Binary Search is one of the fastest searching algorithms.
 It is used for finding the location of an element in a linear array.
 It works on the principle of divide and conquer technique.
Binary Search Algorithm can be applied only on Sorted arrays.
So, the elements must be arranged in-
 Either ascending order if the elements are numbers.
 Or dictionary order if the elements are strings.
To apply binary search on an unsorted array,
 First, sort the array using some sorting technique.
 Then, use binary search algorithm.
Algorithm for binary searching
1. Input A[1…..m], x; //A is the array with size m and x is the target element.
2. first=1, last=m
3. while(first<=last)
{
mid = (first+last)/2;
i. if (x=A[mid]), then print mid; //target element=A[mid] or target element is
//in index mid
break(stop searching);
ii. else if(x<A[mid]), then last=mid-1;
iii. else first=mid+1;
}
4. if(first>last), print “Not Found”;
5. Output: mid or “Not Found”;
Example
Consider the following list of elements and element 15 has to be searched in it using Binary Search
Algorithm.
1 2 3 4 5 6 7
3 10 15 20 35 40 60
Step-01:
 To begin with, we take first=1 and last=7.
 We compute location of the middle element as-
mid = (first + last) / 2 = (1 + 7) / 2 = 4
 Here, A[mid] = A[4] = 20 ≠ 15 and first < =last.
 So, we start next iteration.
Step-02:
 Since A[mid] = 20 > 15, so we take last = mid – 1 = 4 – 1 = 3 whereas beg remains
unchanged.
 We compute location of the middle element as-
mid = (first + last) / 2 = (1 + 3) / 2 = 2
 Here, A[mid] = A[2] = 10 ≠ 15 and first < =last.
 So, we start next iteration.
Step-03:
 Since A[mid] = 10 < 15, so we take first = mid + 1 = 2 + 1 = 3 whereas end remains
unchanged.
 We compute location of the middle element as-
mid = (first + last) / 2 = (3 + 3) / 2 = 3
 Here, A[mid] = A[3] = 15 which matches to the element being searched.
 So, our search terminates in success and index 3 is returned.
Sample Code
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to findn");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.n",
search);
return 0;
}
Sample Output
Enter number of elements
5
Enter 5 integers
1 3 4 5 6
Enter value to find
5
5 found at location 4.

More Related Content

What's hot

Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary searchnikunjandy
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsAbdullah Al-hazmy
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
computer notes - List implementation
computer notes - List implementationcomputer notes - List implementation
computer notes - List implementationecomputernotes
 
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
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
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)Neil Soliven
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sortVasim Pathan
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear searchmontazur420
 
Array operations
Array operationsArray operations
Array operationsZAFAR444
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 

What's hot (20)

searching
searchingsearching
searching
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithms
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
computer notes - List implementation
computer notes - List implementationcomputer notes - List implementation
computer notes - List implementation
 
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...
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
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)
 
L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Array operations
Array operationsArray operations
Array operations
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search
Binary searchBinary search
Binary search
 

Similar to SEO-Optimized 40-Character Title for Searching Algorithms Document

Similar to SEO-Optimized 40-Character Title for Searching Algorithms Document (20)

MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Chap10
Chap10Chap10
Chap10
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
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
 

More from A. S. M. Shafi (20)

2D Transformation in Computer Graphics
2D Transformation in Computer Graphics2D Transformation in Computer Graphics
2D Transformation in Computer Graphics
 
3D Transformation in Computer Graphics
3D Transformation in Computer Graphics3D Transformation in Computer Graphics
3D Transformation in Computer Graphics
 
Projection
ProjectionProjection
Projection
 
2D Transformation
2D Transformation2D Transformation
2D Transformation
 
Line drawing algorithm
Line drawing algorithmLine drawing algorithm
Line drawing algorithm
 
Fragmentation
FragmentationFragmentation
Fragmentation
 
File organization
File organizationFile organization
File organization
 
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
 
RR and priority scheduling
RR and priority schedulingRR and priority scheduling
RR and priority scheduling
 
Fcfs and sjf
Fcfs and sjfFcfs and sjf
Fcfs and sjf
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
1D Array
1D Array1D Array
1D Array
 
2D array
2D array2D array
2D array
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Queue
QueueQueue
Queue
 
Sorting
SortingSorting
Sorting
 
Linked list
Linked listLinked list
Linked list
 
Sum of subset problem
Sum of subset problemSum of subset problem
Sum of subset problem
 
Quick sort
Quick sortQuick sort
Quick sort
 
N queens problem
N queens problemN queens problem
N queens problem
 

Recently uploaded

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 

Recently uploaded (20)

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 

SEO-Optimized 40-Character Title for Searching Algorithms Document

  • 1. Searching  Searching is a process of finding a particular element among several given elements.  The search is successful if the required element is found.  Otherwise, the search is unsuccessful. Searching Algorithms The searching of an element in the given array may be carried out in the following two ways- 1. Linear Search 2. Binary Search Linear Search  Linear Search is the simplest searching algorithm.  It traverses the array sequentially to locate the required element.  It searches for an element by comparing it with each element of the array one by one.  So, it is also called as Sequential Search. Linear Search Algorithm is applied when  No information is given about the array.  The given array is unsorted or the elements are unordered.  The list of data items is smaller. Algorithm for linear searching 1. Input: A set of data in array a, and variable x. i.e., the target element A[1….n]; item=x; location=0; 2. Search the list to find the target element: for (i=1; i<=n; i++) { if (A[i]==item) { print “Found”; location=i; stop searching; } } 3. if (i>n) print ” Not Found”; 4. Output: Found or Not Found.
  • 2. Example Consider the following list of elements and the element to be searched... list 1 2 3 4 5 6 7 8 65 20 10 55 32 12 50 99 Search element 12 Step 1: Search element (12) is compared with first element (65) list 1 2 3 4 5 6 7 8 65 20 10 55 32 12 50 99 12 Both are not matching. So, move to next element. Step 2: Search element (12) is compared with next element (20) list 1 2 3 4 5 6 7 8 65 20 10 55 32 12 50 99 12 Both are not matching. So, move to next element. Step 3: Search element (12) is compared with next element (10) list 1 2 3 4 5 6 7 8 65 20 10 55 32 12 50 99 12 Both are not matching. So, move to next element. Step 4: Search element (12) is compared with next element (55) list 1 2 3 4 5 6 7 8 65 20 10 55 32 12 50 99 12 Both are not matching. So, move to next element. Step 5: Search element (12) is compared with next element (32) list 1 2 3 4 5 6 7 8 65 20 10 55 32 12 50 99 12 Both are not matching. So, move to next element. Step 6: Search element (12) is compared with next element (12) list 1 2 3 4 5 6 7 8 65 20 10 55 32 12 50 99 12 Both are matching. So, we stop comparing and display element found at index 6.
  • 3. Sample Code (Single Occurrence of element): #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in arrayn"); scanf("%d",&n); printf("Enter %d integer(s)n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to searchn"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) { printf("%d is present at location %d.n", search, c+1); break; } } if (c == n) printf("%d is not present in array.n", search); return 0; } Sample Output Case 1: Enter the number of elements in array
  • 4. 5 Enter 5 integer(s) 1 4 5 6 6 Enter the number to search 6 6 is present at location 4. Case 2: Enter the number of elements in array 5 Enter 5 integer(s) 1 2 3 4 7 Enter the number to search 8 8 is not present in array. Sample Code (Multiple Occurrence of element): #include <stdio.h> int main() { int array[100], search, c, n, count = 0; printf("Enter the number of elements in arrayn"); scanf("%d", &n); printf("Enter %d numbersn", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the number to searchn"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) {
  • 5. printf("%d is present at location %d.n", search, c+1); count++; } } if (count == 0) printf("%d is not present in array.n", search); else printf("%d is present %d times in array.n", search, count); return 0; } Sample Output Enter the number of elements in array 5 Enter 5 numbers 1 2 3 3 4 Enter the number to search 3 3 is present at location 3. 3 is present at location 4. 3 is present 2 times in array. Binary Search  Binary Search is one of the fastest searching algorithms.  It is used for finding the location of an element in a linear array.  It works on the principle of divide and conquer technique. Binary Search Algorithm can be applied only on Sorted arrays. So, the elements must be arranged in-  Either ascending order if the elements are numbers.  Or dictionary order if the elements are strings.
  • 6. To apply binary search on an unsorted array,  First, sort the array using some sorting technique.  Then, use binary search algorithm. Algorithm for binary searching 1. Input A[1…..m], x; //A is the array with size m and x is the target element. 2. first=1, last=m 3. while(first<=last) { mid = (first+last)/2; i. if (x=A[mid]), then print mid; //target element=A[mid] or target element is //in index mid break(stop searching); ii. else if(x<A[mid]), then last=mid-1; iii. else first=mid+1; } 4. if(first>last), print “Not Found”; 5. Output: mid or “Not Found”; Example Consider the following list of elements and element 15 has to be searched in it using Binary Search Algorithm. 1 2 3 4 5 6 7 3 10 15 20 35 40 60 Step-01:  To begin with, we take first=1 and last=7.  We compute location of the middle element as- mid = (first + last) / 2 = (1 + 7) / 2 = 4  Here, A[mid] = A[4] = 20 ≠ 15 and first < =last.  So, we start next iteration. Step-02:  Since A[mid] = 20 > 15, so we take last = mid – 1 = 4 – 1 = 3 whereas beg remains unchanged.
  • 7.  We compute location of the middle element as- mid = (first + last) / 2 = (1 + 3) / 2 = 2  Here, A[mid] = A[2] = 10 ≠ 15 and first < =last.  So, we start next iteration. Step-03:  Since A[mid] = 10 < 15, so we take first = mid + 1 = 2 + 1 = 3 whereas end remains unchanged.  We compute location of the middle element as- mid = (first + last) / 2 = (3 + 3) / 2 = 3  Here, A[mid] = A[3] = 15 which matches to the element being searched.  So, our search terminates in success and index 3 is returned. Sample Code #include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elementsn"); scanf("%d",&n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("Enter value to findn"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2;
  • 8. while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d is not present in the list.n", search); return 0; } Sample Output Enter number of elements 5 Enter 5 integers 1 3 4 5 6 Enter value to find 5
  • 9. 5 found at location 4.