Академический Документы
Профессиональный Документы
Культура Документы
1 Linear search
1.1 Linear search in an array
The following example shows a method doing linear search in an array of integers. The method
uses a while loop:
/**
Returns true, if target is found in arr.
*/
public boolean linearSearchArray(int[] arr, int target){
boolean found = false;
int i = 0;
while (!found && i < arr.length){
int k = arr[i];
if (k == target)
found = true;
i++;
}
return found;
}
The algorithm can be illustrated as
0
arr.length-1
arr
i
Where the blue illustrates the elements, we have been searching so far, without finding what we are
looking for. The white part is the elements we have not checked yet. The variable i is pointing at the
element we are checking at the moment.
The method returns a boolean, indicating whether target was found among the values in the
array. Please observe that the loop stops, when the element being searched for is found in the array,
as there is no reason to continue the search.
}
Normally, it is not satisfactory just to know, whether the target exists in the array. We want to
know the index of the target in the array. If that is the case, we must change the method to:
/** Returns the index of target in arr.
If target is not found, -1 is returned.
*/
public int linearSearchArray(int[] arr, int target){
int index = -1;
boolean found = false;
int i = 0;
while (!found && i < arr.length){
int k = arr[i];
if (k == target) {
index = i;
found = true;
}
i++;
}
return index;
}
If sm has the length 13 (as in happy holiday) and m has length 4 (as in holi) then the last index i
in the string to use is 13 - 4 = 9. If i = 9, then holi is trying to match the indexes 9,10,11 and 12,
and 12 is exactly the last index of sm.
All the complicated matching is encapsulated in the method match(). The match() is being made
from index i in sm. The method match() must also know the two strings sm and m.
The method call must therefore be
match(i,sm, m);
A match is found, if
m[j] = sm[i+j] for all j in [0..length(m)-1]
The task remaining is writing the match() method.
If match() finds one character that is not matching, we can stop, because then the substring m is not
in sm starting with index i. Therefore, we search for the first not matching character.
/**
* Returns whether m occurs as substring in sm from index i.
* Req: i <= sm.length() m.length().
*/
public boolean match(int i, String sm, String m) {
boolean found = false;
int j = 0;
while (!found && j < m.length()){
if (m.charAt(j) != sm.charAt(i+j))
found = true;
j++;
}
return !found;
}
Please note that match is true, if we have NOT found a position, where sm and m differs.
Binary search
2.1 Binary search in a sorted array of String
In this example we show a binary search in an array of integers. A binary search requires that the
data searched in is sorted.
The principle in binary search is to look first at the element in the middle of the array. If this
element is equal to the target, the index of this element is returned. If the element in the middle
is greater than target, the search continues in the left half of the array. If the element in the
middle is smaller than target, the search continues in the right half of the array.
We use two int-variables left and right to define the interval, we want to search.
0
arr.length-1
arr
left
right
If the value of target exists more than once in the array, the above method does not necessarily find
the value with the smallest index.
If we are searching for 5 in the array [2,5,5,5,10,12,20,28,28,30,36,50,54,60,72] the last 5 is found;
but if we are searching for 5 in the array [2,5,5,5,10,12,20,28,28] the first 5 is found.