Chapter 6Chapter 6
searChingsearChing
01/31/18 BY MS. SHAISTA QADIR 1
PRESENTED BY
Shaista Qadir
Lecturer king khalid university
COntentsCOntents
 searChing
 seQUentiaL searCh
 exampLe
 aLgOrithm
 prOgram LOgiC
 BinarY searCh
 exampLe
 aLgOrithm
 prOgram LOgiC
01/31/18 BY MS. SHAISTA QADIR 2
searChingsearChing
01/31/18 BY MS. SHAISTA QADIR 3
 Computer systems are often used to store large amounts of
data.
 Sometimes data must be retrieved from storage based on a
searching criteria.
 Efficient storage of data to facilitate fast searching.
seQUentiaL searChseQUentiaL searCh
01/31/18 BY MS. SHAISTA QADIR 4
 The sequential search (also called the Linear Search) is the
simplest search algorithm.
 It is also the least efficient.
 It simply examines each element sequentially, starting with
the first element, until it finds the
 key element or it reaches the end of the array.
 Application: To search a person in the train
SEQUENTIAL SEARCH ExAmpLESEQUENTIAL SEARCH ExAmpLE
01/31/18 BY MS. SHAISTA QADIR 5
 Example:
 Consider an array with the following numbers:
SEQUENTIAL SEARCHSEQUENTIAL SEARCH
ALGORITHmALGORITHm
01/31/18 BY MS. SHAISTA QADIR 6
 ALGORITHM:
(Postcondition: either the index i is returned where si = x,
or –1 is returned.)
1. Repeat steps 2–3, for i = 0 to n – 1.
2. If si = x, return i .
3. Return –1.
SEQUENTIAL SEARCH pROGRAmSEQUENTIAL SEARCH pROGRAm
LOGICLOGIC
01/31/18 BY MS. SHAISTA QADIR 7
 PROGRAM LOGIC:
public int seqsearch(int [ ]arr, int x)
{
for(int i=0; i< arr.length; i++)
if(arr [ i ] == x)
return i;
return -1;
}
 Time Complexity of Sequential search algorithm is: O(n)3.
BINARY SEARCHBINARY SEARCH
01/31/18 BY MS. SHAISTA QADIR 8
 The binary search is the standard algorithm for searching
through a sorted sequence.
 It is much more efficient than the sequential search.
 It repeatedly divides the sequence in two, each time
restricting the search to the half that would contain the
element
 Application: Binary search is used to search a word in a
dictionary.
BINARY SEARCH ExAmplEBINARY SEARCH ExAmplE
01/31/18 BY MS. SHAISTA QADIR 9
 Example: Condition : The array must be sorted.
 Consider an array with the following numbers:
 Divide-and-Conquer Strategy
 Search for the number 16.
 Calculate: mid=(First + last)/2
BINARY SEARCH ExAmplEBINARY SEARCH ExAmplE
01/31/18 BY MS. SHAISTA QADIR 10
 Example: Condition : The array must be sorted.
 Return the position of 16 which is 3
BINARY SEARCH AlGORITHmBINARY SEARCH AlGORITHm
01/31/18 BY MS. SHAISTA QADIR 11
 ALGORITHM:
 (Precondition:1. s={s0, s1, . . ., sn–1} is a sorted sequence of
n values of the same type as x.either the index i is returned
where si = x, or –1 is returned.)
1. Let ss be a subsequence of the sequence s, initially set
equal to s.
2. If the subsequence ss is empty, return –1.
3. Let si be the middle element of ss.
4. If si = x, return its index i .
5. If si < x, repeat steps 2–6on the subsequence that lies
above si.
6. Repeat st
BINARY SEARCH pROGRAmBINARY SEARCH pROGRAm
lOGIClOGIC
01/31/18 BY MS. SHAISTA QADIR 12
 PROGRAM LOGIC:
public static int binarysearch(int[ ] a, int x) {
int lo = 0; int hi = a.length;
while (lo < hi) {
int i = (lo + hi) / 2;
if (a[i] == x)
return i;
else if (a[i] < x)
lo = i+1;
else hi = I; }
return -1; }
 Time Complexity of binary search algorithm is: O(logn)
01/31/18 BY MS. SHAISTA QADIR 13
THANK YOUTHANK YOU

Searching

  • 1.
    Chapter 6Chapter 6 searChingsearChing 01/31/18BY MS. SHAISTA QADIR 1 PRESENTED BY Shaista Qadir Lecturer king khalid university
  • 2.
    COntentsCOntents  searChing  seQUentiaLsearCh  exampLe  aLgOrithm  prOgram LOgiC  BinarY searCh  exampLe  aLgOrithm  prOgram LOgiC 01/31/18 BY MS. SHAISTA QADIR 2
  • 3.
    searChingsearChing 01/31/18 BY MS.SHAISTA QADIR 3  Computer systems are often used to store large amounts of data.  Sometimes data must be retrieved from storage based on a searching criteria.  Efficient storage of data to facilitate fast searching.
  • 4.
    seQUentiaL searChseQUentiaL searCh 01/31/18BY MS. SHAISTA QADIR 4  The sequential search (also called the Linear Search) is the simplest search algorithm.  It is also the least efficient.  It simply examines each element sequentially, starting with the first element, until it finds the  key element or it reaches the end of the array.  Application: To search a person in the train
  • 5.
    SEQUENTIAL SEARCH ExAmpLESEQUENTIALSEARCH ExAmpLE 01/31/18 BY MS. SHAISTA QADIR 5  Example:  Consider an array with the following numbers:
  • 6.
    SEQUENTIAL SEARCHSEQUENTIAL SEARCH ALGORITHmALGORITHm 01/31/18BY MS. SHAISTA QADIR 6  ALGORITHM: (Postcondition: either the index i is returned where si = x, or –1 is returned.) 1. Repeat steps 2–3, for i = 0 to n – 1. 2. If si = x, return i . 3. Return –1.
  • 7.
    SEQUENTIAL SEARCH pROGRAmSEQUENTIALSEARCH pROGRAm LOGICLOGIC 01/31/18 BY MS. SHAISTA QADIR 7  PROGRAM LOGIC: public int seqsearch(int [ ]arr, int x) { for(int i=0; i< arr.length; i++) if(arr [ i ] == x) return i; return -1; }  Time Complexity of Sequential search algorithm is: O(n)3.
  • 8.
    BINARY SEARCHBINARY SEARCH 01/31/18BY MS. SHAISTA QADIR 8  The binary search is the standard algorithm for searching through a sorted sequence.  It is much more efficient than the sequential search.  It repeatedly divides the sequence in two, each time restricting the search to the half that would contain the element  Application: Binary search is used to search a word in a dictionary.
  • 9.
    BINARY SEARCH ExAmplEBINARYSEARCH ExAmplE 01/31/18 BY MS. SHAISTA QADIR 9  Example: Condition : The array must be sorted.  Consider an array with the following numbers:  Divide-and-Conquer Strategy  Search for the number 16.  Calculate: mid=(First + last)/2
  • 10.
    BINARY SEARCH ExAmplEBINARYSEARCH ExAmplE 01/31/18 BY MS. SHAISTA QADIR 10  Example: Condition : The array must be sorted.  Return the position of 16 which is 3
  • 11.
    BINARY SEARCH AlGORITHmBINARYSEARCH AlGORITHm 01/31/18 BY MS. SHAISTA QADIR 11  ALGORITHM:  (Precondition:1. s={s0, s1, . . ., sn–1} is a sorted sequence of n values of the same type as x.either the index i is returned where si = x, or –1 is returned.) 1. Let ss be a subsequence of the sequence s, initially set equal to s. 2. If the subsequence ss is empty, return –1. 3. Let si be the middle element of ss. 4. If si = x, return its index i . 5. If si < x, repeat steps 2–6on the subsequence that lies above si. 6. Repeat st
  • 12.
    BINARY SEARCH pROGRAmBINARYSEARCH pROGRAm lOGIClOGIC 01/31/18 BY MS. SHAISTA QADIR 12  PROGRAM LOGIC: public static int binarysearch(int[ ] a, int x) { int lo = 0; int hi = a.length; while (lo < hi) { int i = (lo + hi) / 2; if (a[i] == x) return i; else if (a[i] < x) lo = i+1; else hi = I; } return -1; }  Time Complexity of binary search algorithm is: O(logn)
  • 13.
    01/31/18 BY MS.SHAISTA QADIR 13 THANK YOUTHANK YOU