SlideShare a Scribd company logo
Lecture 3 Part 2
Searching
23/10/2018 Searching Method Lecture 5 1
Content Lecture
 Overview
 Linear Search
 Binary Search
 Table Search
 Straight String Search
23/10/2018 Searching Method Lecture 5 2
Searching Methods
 Many different algorithms have been developed on
searching methods
 We assume that you search for a given element in a set of n
elements represented as an array
a: ARRAY n OF elements
 The task is to find an index whose element is equal to a
given search element x
 The result has index i so that:
a[i] = x
23/10/2018 Searching Method Lecture 5 3
Linear Search
 Linear search proceeds sequentially through the array until
the element you searched for is found
 It is the simplest search algorithm
 A special case of brute-force search
 There are two conditions terminating the search:
 The element is found that means a[i]= x
 The element is not found in the array that means there
was no element with a[i]= x or a[i] ≠ x for all 0 ≤ i < n
 Linear Search works on any data as long as you can define
an order relation (<,<=,>,>=,=, ≠)
23/10/2018 Searching Method Lecture 5 4
Linear Search
 It terminates when (i = n) or
(a[i] = x)
 In this case the order of the
condition is relevant
 That means you first have to
proof if i is still smaller than n
 If i = n than no match exists
 Because i is increased in each
step the while loop will reach
an end statement
23/10/2018 Searching Method Lecture 5 5
Example Algorithm
int i = 0;
while ((i<n)&&(a[i]!=x))
i++;
if (i < n)
//Search Element found
else
//Search Element not
found
Linear Search
Example
23/10/2018 Searching Method Lecture 5 6
532
=≠ ≠ ≠ ≠
Array a
n=6
i=4 < n=6  index found  a[i=4]=x=532
x = 532
i=0 i=1 i=2 i=3 i=4
16 71 43 82 532 633
0 1 2 3 4 5
Linear Search
Example
23/10/2018 Searching Method Lecture 5 7
332
≠ ≠ ≠ ≠
Array a
n=6
i=6=n=6  no index found  x=332 is not contained
x = 332
i=0 i=1 i=2 i=3 i=4 i=5 i=6
36 11 43 82 531 233
0 1 2 3 4 5
≠ ≠
Linear Search
 You can alter the algorithm by using
an additional element at the end of
the array with value x
 This element is called sentinel
 If you add the element the array has
now n+1 elements with a[n]=x
 The algorithm terminates now
when a[i] = x
 If i = n that implies no match has
found (beside the sentinel)
23/10/2018 Searching Method Lecture 5 8
Example Algorithm
a2[n] = x;
int i = 0;
while (a2[i]!=x)
i++;
if (i==n)
//Search Element
not found
Linear Search
Example
23/10/2018 Searching Method Lecture 5 9
532
≠ ≠ ≠ ≠
i=4 < n=6  index found  a[i=4] = x=532
x = 532
i=0 i=1 i=2 i=3 i=4 i=5 i=6
16 17 43 82 532 233 532
0 1 2 3 4 5
= Sentinel
added
new Array
a2: 6+1
elements
Linear Search
Example
23/10/2018 Searching Method Lecture 5 10
332
≠ ≠ ≠ ≠
i=6=n  sentinel is the only match  element not found
x = 332
i=0 i=1 i=2 i=3 i=4 i=5 i=6
16 17 43 82 532 233 332
0 1 2 3 4 5
≠ Sentinel
added
≠ =
new Array
a’: 6+1
elements
Binary Search
 To speed up a search you need more information about the
search data
 If the data are ordered (sorted) the search is much more
effective
 For example in a telephone book the data are alphabetically
ordered
 It makes it easier to find a certain name
 Assuming our data are ordered such that
a[k-1] ≤ a[k] where 1 ≤ k < n
n number of elements
23/10/2018 Searching Method Lecture 5 11
Binary Search
 The algorithm based on:
 if you pick an element a[i] at random and compare it
with the search element x than:
 the search terminates if they are equal (x=a[i])
 if it is less than the search element (a[i]< x) than all
elements with indices less or equal to i can be
eliminated from the search
 if it is greater than the search element (a[i]> x) than
all elements with indices greater or equal to i can be
eliminated
 This is called binary search
23/10/2018 Searching Method Lecture 5 12
Binary Search
 Be L the left and R the
right end indices of the
section in which
elements still can be
found
 The repetition ends
when
 found is true or
 ((L>R) & (a[k]<x; 0
≤ k < L) & (a[k]>x; R
< k < n) which
implies (a[i] = x) or
(a[k] ≠ x; 0 ≤ k < n)
23/10/2018 Searching Method Lecture 5 13
Algorithm
int L = 0;
int R = n-1;
boolean found = false;
while ((L<=R) && !found) {
i = 3; //any value between
L and R";
if (a[i]==x)
found = true;
else if (a[i]<x)
L = i+1;
else
R = i-1;
}
Binary Search
23/10/2018 Searching Method Lecture 5 14
RL
0 n-1i
a[i]
x
=
index
bounds
array element a[0] a[n-1]
Last index
Element found:
search terminates
Binary Search
23/10/2018 Searching Method Lecture 5 15
RL
0 n-1i
a[i]
x
index
bounds
array element a[0] a[n-1]
Last index
a[i] < x:
Move left bound
<
i+1
L
Range where the
Element can be
still found
Binary Search
23/10/2018 Searching Method Lecture 5 16
RL
0 n-1i
a[i]
x
index
bounds
array element a[0] a[n-1]
Last index
a[i] > x:
Move right bound
>
Range where the
Element can be
still found
i-1
R
Binary Search
 The correctness of this algorithm does not depend on the chosen
i but it does influence the effectiveness
 You wish to eliminate as much elements as possible in each step
 The optimal solution is to choose the middle element
 This eliminates half of the array in any case
 As a result the maximal number of step is log2n
 For linear searches the number of expected comparisons is n/2
 Example: n = 1024
 linear search: 612 (= 1024/2)
 binary search: 10 (log2 1024 = 10  1024 = 210)
 Binary search is faster than linear search but you have to have
sorted data first!
23/10/2018 Searching Method Lecture 5 17
Binary Search
 Improvement can be made if
you chance the algorithm to
the following
 The repetitions ends when L ≥
R
 In each step L will be increased
or R will be decreased and it
ends when L=R
 In contrast to the first solution
this algorithm finds the
matching element with the last
index
23/10/2018 Searching Method Lecture 5 18
Improved Algorithm
int L = 0;
int R = n;
while (L<R) {
i = (L+R)/2;
if (a[i]<x)
L = i+1;
else
R = i;
}
Binary Search
23/10/2018 Searching Method Lecture 5 19
RL
0 Ni=
𝐿+𝑅
2
a[i]
x
<
L
i+1index
bounds
array element
a[i] < x:
Move left bound
n-1
a[n-1]
Last index
a[0]
Binary Search
23/10/2018 Searching Method Lecture 5 20
RL
0 Ni=
𝐿+𝑅
2
a[i]
x
>=
index
bounds
array element
a[i] >= x:
Move right bound
n-1
a[n-1]
Last index
a[0]
R
Ends with R = L
If a[L = R] = x  element found
else  element is not found
Binary Search
Example
Array a = {1, 4, 6, 9, 10, 15, 18, 23, 27, 34, 44}
Searching for x = 27
L = 0 and R = 11 = N
First Round: i = (R + L)/2 = (0+11)/2 = 11/2 = 5
a[i = 5] = 15 < x = 27 L = i + 1 = 6 and R = 11
Second Round: i = (R + L)/2 = (6+11)/2 = 17/2 = 8
a[i = 8] = 27 = x  L = 6 and R = i = 8
Third Round: i = (R + L)/2 = (6+8)/2 = 14/2 = 7
a[i = 7] = 23 < x = 27  L = i + 1 = 8 and R = 8
Index = R = L = 8: a[L = R = 8] = 27 = x
 Element found at index 8
23/10/2018 Searching Method Lecture 5 21
Table Search
 A search through an array is sometimes called a table
search
 This is particularly the case if the elements of the array are
themselves structured object like numbers of characters
 An array of characters is called a string
 Equality of strings is defined by:
s = t ≡ (si = ti; 0 ≤ i < N)
 An order of strings is defined by:
s < t ≡ (si = ti) & (sj < tj); 0 ≤ i ≤ j and 0 ≤ j < N
23/10/2018 Searching Method Lecture 5 22
Table Search
 Examples
 Equality: s=Peter = t=Peter
i = 0 s0 = P = t0 = P
i = 1 s1 = e = t1 = e
i = 2 s2 = t = t2 = t  si=ti for all 0 ≤ i < N
i = 3 s3 = e = t3 = e
i = 4 s4 = r = t4 = r
 Order: s=Tom > t=Tim
i = 0 s0 = T = t0 = T  si = ti for i = 0
i = 1 s1 = o > t1 = I si > ti for i = 1
23/10/2018 Searching Method Lecture 5 23
Table Search
 To find a match between strings all characters have to be
equal
 This comparison can also be seen as a search of an unequal
pair that means a search for inequality
 If no unequal pairs exist the strings are equal
 If the length of the strings is small a linear search can be
used
(it works on sorted data just like binary search)
23/10/2018 Searching Method Lecture 5 24
Table Search
 The length of a string can be represented as:
 A string is terminated by a specific terminating
character; in the most cases the null character0 is used
 The length is stored at the first element of the array
 Therefore the string s is represented as
s = s0, s1, s2… sN-1
where s0 is the length and s1, …, sN-1 are the
characters of the string
 In the second solution the length is directly available
23/10/2018 Searching Method Lecture 5 25
Table Search
 The table search needs a nested search
 That means a search through the entries of the table and for each
entry in the table a sequence of comparisons between the
components (comparing the strings)
 Algorithm (for the components)
i = 0;
while ((s[i]==t[i])&&(s[i]!='0'))
i++;
 In this case the termination character 0 functions as a sentinel
 To complete the table search an alphabetically order of the
strings (each entry of the table) is required
 Because then a binary search can be used
23/10/2018 Searching Method Lecture 5 26
Table Search
 Be T a table of
strings
alphabetically
ordered
 s the search
argument
 n the number of
elements in the
table
 m the length of the
search argument s
23/10/2018 Searching Method Lecture 5 27
L = 0;
R = n;
while (L < R) {
i = (L + R) / 2;
j = 0;
while ((T[i][j]==s[j])&&(s[j]!='0'))
j++;
//T is the array of strings and S is
the search string : it is given
if (T[i][j] < s[j])
L = i + 1;
else
R = i;
}
Table Search
Example
Table T = {Valarie, Velma, Vicky, Victor, Victoria}
Search string s = Victor
L = 0
R = 5
First Round i = (0 + 5)/2 = 2, j = 0
while T[2][0] = V = s[0] = V  j = 1
T[2][1] = i = s[1] = i  j = 2
T[2][2] = c = s[2] = c  j = 3
T[2][3] = k != s[3] = t T[2] < s
if  L = i + 1 = 2 + 1 = 3
23/10/2018 Searching Method Lecture 5 28
Table Search
Second Round i = (3 + 5)/2 = 4, j = 0
while T[4][0] = V = s[0] = V  j = 1
T[4][1] = i = s[1] = i  j = 2
T[4][2] = c = s[2] = c  j = 3
T[4][3] = t = s[3] = t  j = 4
T[4][4] = o = s[4] = o  j = 5
T[4][5] = r = s[5] = r  j = 6
T[4][6] = i != s[6]=‘0’  T[4] > s
if  R = i = 4
23/10/2018 Searching Method Lecture 5 29
Table Search
Third Round i = (3 + 4)/2 = 3, j = 0
while T[3][0] = V = s[0] = V  j = 1
T[3][1] = i = s[1] = i  j = 2
T[3][2] = c = s[2] = c  j = 3
T[3][3] = t = s[3] = t  j = 4
T[3][4] = o = s[4] = o  j = 5
T[3][5] = r = s[5] = r  j = 6
T[3][6]=‘0’=s[6]=‘0’  T = s
if  R = 3
 R = L = 3  check if T[3] = s for 0 ≤ j < m
 array T contains search element s
23/10/2018 Searching Method Lecture 5 30
Straight String Search
 A string search is to find the first appearance of a string in
another string
 Typically the elements of the arrays are characters (strings)
 One array is a text and the searching array is a pattern or
word you wish to find in the text
 In the most cases you look for the first appearance of the
word in the text
 The straight string search is a straightforward searching
algorithm for this task
23/10/2018 Searching Method Lecture 5 31
Straight String Search
Definition
s an array of N element
p an array of M element where 0 < M < N with p0, …, pM-1 characters
i the result index of the first occurrence of the searching array p in
an array s
A predicate P is defined as:
P(i, j): si+k = pk with 0 ≤ k < j
Because the first occurrence of the pattern is searched P(k, M) must
be false for all k < i
Therefore it follows the condition Q(i) = ~P(k, M) with 0 ≤ k < I
23/10/2018 Searching Method Lecture 5 32
Straight String Search
i = -1;
do {
i++;
j = 0;
while ((j<M)&&(s[i+j]==p[j]))
//Handling the pattern
if necessary
j++;
if (j == M)
//”Pattern found at “
} while (j != M && i != N - M);
23/10/2018 Searching Method Lecture 5 33
• The best way is
that the iteration
for P is a search
for inequality
among the
corresponding
pattern and
string characters
• The term i = N –
M implies the
nonexistence of a
match anywhere
in the string
Straight String Search
Example
p = “the” s = “Today the students appeared well prepared.”
N = 43, M = 3
i=0 j=0 < M=3 && s[0+0]=T!=p[0]=t  i++
i=1 j=0 < M=3 && s[1+0]=o!=p[0]=t  i++
i=2 j=0 < M=3 && s[2+o]=d!=p[0]=t  i++
i=3 j=0 < M=3 && s[3+0]=a!=p[0]=t  i++
i=4 j=0 < M=3 && s[4+0]=y!=p[0]=t  i++
i=5 j=0 < M=3 && s[5+0]= !=p[0]=t  i++
i=6 j=0 < M=3 && s[6+0]=t==p[0]=t  j++
i=6 j=1 < M=3 && s[6+1]=h==p[1]=h  j++
i=6 j=2 < M=3 && s[6+2]=e!=p[2]=e  j++
i=6 j=3 == M=3
 Pattern found!
23/10/2018 Searching Method Lecture 5 34
Straight String Search
s:
i:
p:
j:
23/10/2018 Searching Method Lecture 5 35
t h e
0 1 2
T o d a y t h e s t u d
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Straight String Search
Other string searches
 Knuth-Morris-Pratt String Search
 The Boyer-Moore String Search
23/10/2018 Searching Method Lecture 5 36
Any
questions?
23/10/2018 Searching Method Lecture 5 37

More Related Content

PPTX
Lecture3a sorting
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
Implementing Merge Sort
PDF
Sorting algorithm
PDF
GRID SEARCHING Novel way of Searching 2D Array
PDF
IRJET- A Survey on Different Searching Algorithms
PPTX
PPTX
Search algorithms master
Lecture3a sorting
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Implementing Merge Sort
Sorting algorithm
GRID SEARCHING Novel way of Searching 2D Array
IRJET- A Survey on Different Searching Algorithms
Search algorithms master

What's hot (20)

PPTX
Merge sort analysis and its real time applications
PDF
Algorithms Lecture 6: Searching Algorithms
PDF
Data Structures & Algorithm design using C
PPTX
Unit 8 searching and hashing
PDF
Sorting Algorithms
PPT
L10 sorting-searching
PPT
SEARCHING AND SORTING ALGORITHMS
PDF
DATA STRUCTURES USING C -ENGGDIGEST
PPT
Searching algorithms
PDF
LINEAR SEARCH VERSUS BINARY SEARCH: A STATISTICAL COMPARISON FOR BINOMIAL INPUTS
PDF
Binary Search - Design & Analysis of Algorithms
PPT
Data Structures & Algorithms
PPTX
Sorting and hashing concepts
PPT
Chapter 11 - Sorting and Searching
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PPT
358 33 powerpoint-slides_15-hashing-collision_chapter-15
PPTX
Different types of Shoring Algorithms with Animation
PPSX
Algorithm and Programming (Searching)
PPTX
Matlab ch1 (1)
Merge sort analysis and its real time applications
Algorithms Lecture 6: Searching Algorithms
Data Structures & Algorithm design using C
Unit 8 searching and hashing
Sorting Algorithms
L10 sorting-searching
SEARCHING AND SORTING ALGORITHMS
DATA STRUCTURES USING C -ENGGDIGEST
Searching algorithms
LINEAR SEARCH VERSUS BINARY SEARCH: A STATISTICAL COMPARISON FOR BINOMIAL INPUTS
Binary Search - Design & Analysis of Algorithms
Data Structures & Algorithms
Sorting and hashing concepts
Chapter 11 - Sorting and Searching
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_15-hashing-collision_chapter-15
Different types of Shoring Algorithms with Animation
Algorithm and Programming (Searching)
Matlab ch1 (1)
Ad

Similar to Lecture3b searching (20)

PDF
Searching
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPT
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
PPT
Data structure and problem solving ch03.ppt
PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPT
21-algorithms (1).ppt
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
PDF
Searching and Sorting Algorithms
PPTX
Lect-2.pptx
PPT
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
PPTX
UNEC__1683196273.pptx
PDF
PPT
Algorithm, Pseudocode and Flowcharting in C++
PPT
21-algorithms.ppt
PPT
Searching algorithms
PPTX
Unit viii searching and hashing
PPTX
Chapter3.pptx
PPTX
Dsa – data structure and algorithms searching
Searching
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
Data structure and problem solving ch03.ppt
Searching and Sorting Algorithms in Data Structures
Chapter 2 Sorting and Searching .pptx.soft
21-algorithms (1).ppt
Chapter 3 - Data Structure and Algorithms.pptx
Searching and Sorting Algorithms
Lect-2.pptx
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
UNEC__1683196273.pptx
Algorithm, Pseudocode and Flowcharting in C++
21-algorithms.ppt
Searching algorithms
Unit viii searching and hashing
Chapter3.pptx
Dsa – data structure and algorithms searching
Ad

More from mbadhi barnabas (8)

PPTX
Lecture4b dynamic data_structure
PPTX
Lecture4a dynamic data_structure
PPTX
Lecture2b algorithm
PPTX
Lecture2a algorithm
PPTX
Lecture1b data types
PPTX
Lecture1a data types
PDF
Data struture and aligorism
PDF
Data structures and algorithm
Lecture4b dynamic data_structure
Lecture4a dynamic data_structure
Lecture2b algorithm
Lecture2a algorithm
Lecture1b data types
Lecture1a data types
Data struture and aligorism
Data structures and algorithm

Recently uploaded (20)

PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
Mega Projects Data Mega Projects Data
PDF
Introduction to Data Science and Data Analysis
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Database Infoormation System (DBIS).pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPT
Quality review (1)_presentation of this 21
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
[EN] Industrial Machine Downtime Prediction
PPT
Reliability_Chapter_ presentation 1221.5784
PPT
ISS -ESG Data flows What is ESG and HowHow
PDF
Clinical guidelines as a resource for EBP(1).pdf
PDF
Introduction to the R Programming Language
Miokarditis (Inflamasi pada Otot Jantung)
Mega Projects Data Mega Projects Data
Introduction to Data Science and Data Analysis
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Database Infoormation System (DBIS).pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Quality review (1)_presentation of this 21
STUDY DESIGN details- Lt Col Maksud (21).pptx
Fluorescence-microscope_Botany_detailed content
Qualitative Qantitative and Mixed Methods.pptx
1_Introduction to advance data techniques.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
[EN] Industrial Machine Downtime Prediction
Reliability_Chapter_ presentation 1221.5784
ISS -ESG Data flows What is ESG and HowHow
Clinical guidelines as a resource for EBP(1).pdf
Introduction to the R Programming Language

Lecture3b searching

  • 1. Lecture 3 Part 2 Searching 23/10/2018 Searching Method Lecture 5 1
  • 2. Content Lecture  Overview  Linear Search  Binary Search  Table Search  Straight String Search 23/10/2018 Searching Method Lecture 5 2
  • 3. Searching Methods  Many different algorithms have been developed on searching methods  We assume that you search for a given element in a set of n elements represented as an array a: ARRAY n OF elements  The task is to find an index whose element is equal to a given search element x  The result has index i so that: a[i] = x 23/10/2018 Searching Method Lecture 5 3
  • 4. Linear Search  Linear search proceeds sequentially through the array until the element you searched for is found  It is the simplest search algorithm  A special case of brute-force search  There are two conditions terminating the search:  The element is found that means a[i]= x  The element is not found in the array that means there was no element with a[i]= x or a[i] ≠ x for all 0 ≤ i < n  Linear Search works on any data as long as you can define an order relation (<,<=,>,>=,=, ≠) 23/10/2018 Searching Method Lecture 5 4
  • 5. Linear Search  It terminates when (i = n) or (a[i] = x)  In this case the order of the condition is relevant  That means you first have to proof if i is still smaller than n  If i = n than no match exists  Because i is increased in each step the while loop will reach an end statement 23/10/2018 Searching Method Lecture 5 5 Example Algorithm int i = 0; while ((i<n)&&(a[i]!=x)) i++; if (i < n) //Search Element found else //Search Element not found
  • 6. Linear Search Example 23/10/2018 Searching Method Lecture 5 6 532 =≠ ≠ ≠ ≠ Array a n=6 i=4 < n=6  index found  a[i=4]=x=532 x = 532 i=0 i=1 i=2 i=3 i=4 16 71 43 82 532 633 0 1 2 3 4 5
  • 7. Linear Search Example 23/10/2018 Searching Method Lecture 5 7 332 ≠ ≠ ≠ ≠ Array a n=6 i=6=n=6  no index found  x=332 is not contained x = 332 i=0 i=1 i=2 i=3 i=4 i=5 i=6 36 11 43 82 531 233 0 1 2 3 4 5 ≠ ≠
  • 8. Linear Search  You can alter the algorithm by using an additional element at the end of the array with value x  This element is called sentinel  If you add the element the array has now n+1 elements with a[n]=x  The algorithm terminates now when a[i] = x  If i = n that implies no match has found (beside the sentinel) 23/10/2018 Searching Method Lecture 5 8 Example Algorithm a2[n] = x; int i = 0; while (a2[i]!=x) i++; if (i==n) //Search Element not found
  • 9. Linear Search Example 23/10/2018 Searching Method Lecture 5 9 532 ≠ ≠ ≠ ≠ i=4 < n=6  index found  a[i=4] = x=532 x = 532 i=0 i=1 i=2 i=3 i=4 i=5 i=6 16 17 43 82 532 233 532 0 1 2 3 4 5 = Sentinel added new Array a2: 6+1 elements
  • 10. Linear Search Example 23/10/2018 Searching Method Lecture 5 10 332 ≠ ≠ ≠ ≠ i=6=n  sentinel is the only match  element not found x = 332 i=0 i=1 i=2 i=3 i=4 i=5 i=6 16 17 43 82 532 233 332 0 1 2 3 4 5 ≠ Sentinel added ≠ = new Array a’: 6+1 elements
  • 11. Binary Search  To speed up a search you need more information about the search data  If the data are ordered (sorted) the search is much more effective  For example in a telephone book the data are alphabetically ordered  It makes it easier to find a certain name  Assuming our data are ordered such that a[k-1] ≤ a[k] where 1 ≤ k < n n number of elements 23/10/2018 Searching Method Lecture 5 11
  • 12. Binary Search  The algorithm based on:  if you pick an element a[i] at random and compare it with the search element x than:  the search terminates if they are equal (x=a[i])  if it is less than the search element (a[i]< x) than all elements with indices less or equal to i can be eliminated from the search  if it is greater than the search element (a[i]> x) than all elements with indices greater or equal to i can be eliminated  This is called binary search 23/10/2018 Searching Method Lecture 5 12
  • 13. Binary Search  Be L the left and R the right end indices of the section in which elements still can be found  The repetition ends when  found is true or  ((L>R) & (a[k]<x; 0 ≤ k < L) & (a[k]>x; R < k < n) which implies (a[i] = x) or (a[k] ≠ x; 0 ≤ k < n) 23/10/2018 Searching Method Lecture 5 13 Algorithm int L = 0; int R = n-1; boolean found = false; while ((L<=R) && !found) { i = 3; //any value between L and R"; if (a[i]==x) found = true; else if (a[i]<x) L = i+1; else R = i-1; }
  • 14. Binary Search 23/10/2018 Searching Method Lecture 5 14 RL 0 n-1i a[i] x = index bounds array element a[0] a[n-1] Last index Element found: search terminates
  • 15. Binary Search 23/10/2018 Searching Method Lecture 5 15 RL 0 n-1i a[i] x index bounds array element a[0] a[n-1] Last index a[i] < x: Move left bound < i+1 L Range where the Element can be still found
  • 16. Binary Search 23/10/2018 Searching Method Lecture 5 16 RL 0 n-1i a[i] x index bounds array element a[0] a[n-1] Last index a[i] > x: Move right bound > Range where the Element can be still found i-1 R
  • 17. Binary Search  The correctness of this algorithm does not depend on the chosen i but it does influence the effectiveness  You wish to eliminate as much elements as possible in each step  The optimal solution is to choose the middle element  This eliminates half of the array in any case  As a result the maximal number of step is log2n  For linear searches the number of expected comparisons is n/2  Example: n = 1024  linear search: 612 (= 1024/2)  binary search: 10 (log2 1024 = 10  1024 = 210)  Binary search is faster than linear search but you have to have sorted data first! 23/10/2018 Searching Method Lecture 5 17
  • 18. Binary Search  Improvement can be made if you chance the algorithm to the following  The repetitions ends when L ≥ R  In each step L will be increased or R will be decreased and it ends when L=R  In contrast to the first solution this algorithm finds the matching element with the last index 23/10/2018 Searching Method Lecture 5 18 Improved Algorithm int L = 0; int R = n; while (L<R) { i = (L+R)/2; if (a[i]<x) L = i+1; else R = i; }
  • 19. Binary Search 23/10/2018 Searching Method Lecture 5 19 RL 0 Ni= 𝐿+𝑅 2 a[i] x < L i+1index bounds array element a[i] < x: Move left bound n-1 a[n-1] Last index a[0]
  • 20. Binary Search 23/10/2018 Searching Method Lecture 5 20 RL 0 Ni= 𝐿+𝑅 2 a[i] x >= index bounds array element a[i] >= x: Move right bound n-1 a[n-1] Last index a[0] R Ends with R = L If a[L = R] = x  element found else  element is not found
  • 21. Binary Search Example Array a = {1, 4, 6, 9, 10, 15, 18, 23, 27, 34, 44} Searching for x = 27 L = 0 and R = 11 = N First Round: i = (R + L)/2 = (0+11)/2 = 11/2 = 5 a[i = 5] = 15 < x = 27 L = i + 1 = 6 and R = 11 Second Round: i = (R + L)/2 = (6+11)/2 = 17/2 = 8 a[i = 8] = 27 = x  L = 6 and R = i = 8 Third Round: i = (R + L)/2 = (6+8)/2 = 14/2 = 7 a[i = 7] = 23 < x = 27  L = i + 1 = 8 and R = 8 Index = R = L = 8: a[L = R = 8] = 27 = x  Element found at index 8 23/10/2018 Searching Method Lecture 5 21
  • 22. Table Search  A search through an array is sometimes called a table search  This is particularly the case if the elements of the array are themselves structured object like numbers of characters  An array of characters is called a string  Equality of strings is defined by: s = t ≡ (si = ti; 0 ≤ i < N)  An order of strings is defined by: s < t ≡ (si = ti) & (sj < tj); 0 ≤ i ≤ j and 0 ≤ j < N 23/10/2018 Searching Method Lecture 5 22
  • 23. Table Search  Examples  Equality: s=Peter = t=Peter i = 0 s0 = P = t0 = P i = 1 s1 = e = t1 = e i = 2 s2 = t = t2 = t  si=ti for all 0 ≤ i < N i = 3 s3 = e = t3 = e i = 4 s4 = r = t4 = r  Order: s=Tom > t=Tim i = 0 s0 = T = t0 = T  si = ti for i = 0 i = 1 s1 = o > t1 = I si > ti for i = 1 23/10/2018 Searching Method Lecture 5 23
  • 24. Table Search  To find a match between strings all characters have to be equal  This comparison can also be seen as a search of an unequal pair that means a search for inequality  If no unequal pairs exist the strings are equal  If the length of the strings is small a linear search can be used (it works on sorted data just like binary search) 23/10/2018 Searching Method Lecture 5 24
  • 25. Table Search  The length of a string can be represented as:  A string is terminated by a specific terminating character; in the most cases the null character0 is used  The length is stored at the first element of the array  Therefore the string s is represented as s = s0, s1, s2… sN-1 where s0 is the length and s1, …, sN-1 are the characters of the string  In the second solution the length is directly available 23/10/2018 Searching Method Lecture 5 25
  • 26. Table Search  The table search needs a nested search  That means a search through the entries of the table and for each entry in the table a sequence of comparisons between the components (comparing the strings)  Algorithm (for the components) i = 0; while ((s[i]==t[i])&&(s[i]!='0')) i++;  In this case the termination character 0 functions as a sentinel  To complete the table search an alphabetically order of the strings (each entry of the table) is required  Because then a binary search can be used 23/10/2018 Searching Method Lecture 5 26
  • 27. Table Search  Be T a table of strings alphabetically ordered  s the search argument  n the number of elements in the table  m the length of the search argument s 23/10/2018 Searching Method Lecture 5 27 L = 0; R = n; while (L < R) { i = (L + R) / 2; j = 0; while ((T[i][j]==s[j])&&(s[j]!='0')) j++; //T is the array of strings and S is the search string : it is given if (T[i][j] < s[j]) L = i + 1; else R = i; }
  • 28. Table Search Example Table T = {Valarie, Velma, Vicky, Victor, Victoria} Search string s = Victor L = 0 R = 5 First Round i = (0 + 5)/2 = 2, j = 0 while T[2][0] = V = s[0] = V  j = 1 T[2][1] = i = s[1] = i  j = 2 T[2][2] = c = s[2] = c  j = 3 T[2][3] = k != s[3] = t T[2] < s if  L = i + 1 = 2 + 1 = 3 23/10/2018 Searching Method Lecture 5 28
  • 29. Table Search Second Round i = (3 + 5)/2 = 4, j = 0 while T[4][0] = V = s[0] = V  j = 1 T[4][1] = i = s[1] = i  j = 2 T[4][2] = c = s[2] = c  j = 3 T[4][3] = t = s[3] = t  j = 4 T[4][4] = o = s[4] = o  j = 5 T[4][5] = r = s[5] = r  j = 6 T[4][6] = i != s[6]=‘0’  T[4] > s if  R = i = 4 23/10/2018 Searching Method Lecture 5 29
  • 30. Table Search Third Round i = (3 + 4)/2 = 3, j = 0 while T[3][0] = V = s[0] = V  j = 1 T[3][1] = i = s[1] = i  j = 2 T[3][2] = c = s[2] = c  j = 3 T[3][3] = t = s[3] = t  j = 4 T[3][4] = o = s[4] = o  j = 5 T[3][5] = r = s[5] = r  j = 6 T[3][6]=‘0’=s[6]=‘0’  T = s if  R = 3  R = L = 3  check if T[3] = s for 0 ≤ j < m  array T contains search element s 23/10/2018 Searching Method Lecture 5 30
  • 31. Straight String Search  A string search is to find the first appearance of a string in another string  Typically the elements of the arrays are characters (strings)  One array is a text and the searching array is a pattern or word you wish to find in the text  In the most cases you look for the first appearance of the word in the text  The straight string search is a straightforward searching algorithm for this task 23/10/2018 Searching Method Lecture 5 31
  • 32. Straight String Search Definition s an array of N element p an array of M element where 0 < M < N with p0, …, pM-1 characters i the result index of the first occurrence of the searching array p in an array s A predicate P is defined as: P(i, j): si+k = pk with 0 ≤ k < j Because the first occurrence of the pattern is searched P(k, M) must be false for all k < i Therefore it follows the condition Q(i) = ~P(k, M) with 0 ≤ k < I 23/10/2018 Searching Method Lecture 5 32
  • 33. Straight String Search i = -1; do { i++; j = 0; while ((j<M)&&(s[i+j]==p[j])) //Handling the pattern if necessary j++; if (j == M) //”Pattern found at “ } while (j != M && i != N - M); 23/10/2018 Searching Method Lecture 5 33 • The best way is that the iteration for P is a search for inequality among the corresponding pattern and string characters • The term i = N – M implies the nonexistence of a match anywhere in the string
  • 34. Straight String Search Example p = “the” s = “Today the students appeared well prepared.” N = 43, M = 3 i=0 j=0 < M=3 && s[0+0]=T!=p[0]=t  i++ i=1 j=0 < M=3 && s[1+0]=o!=p[0]=t  i++ i=2 j=0 < M=3 && s[2+o]=d!=p[0]=t  i++ i=3 j=0 < M=3 && s[3+0]=a!=p[0]=t  i++ i=4 j=0 < M=3 && s[4+0]=y!=p[0]=t  i++ i=5 j=0 < M=3 && s[5+0]= !=p[0]=t  i++ i=6 j=0 < M=3 && s[6+0]=t==p[0]=t  j++ i=6 j=1 < M=3 && s[6+1]=h==p[1]=h  j++ i=6 j=2 < M=3 && s[6+2]=e!=p[2]=e  j++ i=6 j=3 == M=3  Pattern found! 23/10/2018 Searching Method Lecture 5 34
  • 35. Straight String Search s: i: p: j: 23/10/2018 Searching Method Lecture 5 35 t h e 0 1 2 T o d a y t h e s t u d 0 1 2 3 4 5 6 7 8 9 10 11 12 13
  • 36. Straight String Search Other string searches  Knuth-Morris-Pratt String Search  The Boyer-Moore String Search 23/10/2018 Searching Method Lecture 5 36