data structures & algorithms chapter 4 searching ms. manal al-asmari
TRANSCRIPT
Ms. Manal Al-Asmari
Searching Algorithms
• Searching is the process of determining whether or not a given value exists in a data structure or a storage media.
• We will study two searching algorithms:
– Linear Search
– Binary Search
Ms. Manal Al-Asmari
Linear Search
• The linear (or sequential) search algorithm on an array is:
– Start from beginning of an array/list and continues until the
item is found or the entire array/list has been searched.
– Sequentially scan the array, comparing each array item with
the searched value.
– If a match is found; return the index of the matched
element; otherwise return –1.
• Note: linear search can be applied to both sorted and unsorted
arrays.
Linear Searchint seqSearch(int[] list, int listLength, int searchItem){int loc;boolean found = false;for (loc = 0; loc < listLength; loc++)if (list[loc] == searchItem){found = true;break;}if (found)return loc;elsereturn -1;}
Ms. Manal Al-Asmari
Ms. Manal Al-Asmari
Linear Search Disadvantages
– Inefficient for array of N elementsExamines N/2 elements on average for value in array, N elements for value not in array
Ms. Manal Al-Asmari
Binary Search
Binary search looks for an item in an array/list using divide and conquer strategy
Ms. Manal Al-Asmari
Binary Search– Binary search algorithm assumes that the items in the array
being searched is sorted
– The algorithm begins at the middle of the array in a binary
search
– If the item for which we are searching is less than the item in
the middle, we know that the item won’t be in the second half of
the array
– Once again we examine the “middle” element
– The process continues with each comparison cutting in half the
portion of the array where the item might be
Ms. Manal Al-Asmari
Binary Search
• Binary search uses a recursive method to search an array to find a
specified value
• The array must be a sorted array:
a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex]
• If the value is found, its index is returned
• If the value is not found, -1 is returned
• Note: Each execution of the recursive method reduces the search
space by about a half
Binary Search Algorithmpublic static int binarySearch(int[] list, int listLength,int searchItem){int first = 0;int last = listLength - 1;int mid;boolean found = false;while (first <= last){mid = (first + last) / 2;if (list[mid] == searchItem)found = true;elseif (list[mid] > searchItem)last = mid - 1;elsefirst = mid + 1;}if (found)return mid;elsereturn –1;}
Ms. Manal Al-Asmari
Ms. Manal Al-Asmari
1. There is no infinite recursion
• On each recursive call, the value of first is increased, or the
value of last is decreased
• If the chain of recursive calls does not end in some other way,
then eventually the method will be called with first larger than
last
2. Each stopping case performs the correct action for that case
• If first > last, there are no array elements between a[first] and
a[last], so key is not in this segment of the array, and result is
correctly set to -1
• If key == a[mid], result is correctly set to mid
Key Points in Binary Search
Ms. Manal Al-Asmari
Key Points in Binary Search
3. For each of the cases that involve recursion, if all recursive calls
perform their actions correctly, then the entire case performs
correctly
• If key < a[mid], then key must be one of the elements a[first]
through a[mid-1], or it is not in the array
• The method should then search only those elements, which it
does
• The recursive call is correct, therefore the entire action is
correct
Ms. Manal Al-Asmari
Key Points in Binary Search• If key > a[mid], then key must be one of the elements
a[mid+1] through a[last], or it is not in the array
• The method should then search only those elements, which it
does
• The recursive call is correct, therefore the entire action is
correct
The method search passes all three tests:
Therefore, it is a good recursive method definition
Ms. Manal Al-Asmari
Efficiency of Binary Search
• The binary search algorithm is extremely fast compared
to an algorithm that tries all array elements in order
– About half the array is eliminated from consideration
right at the start
– Then a quarter of the array, then an eighth of the
array, and so forth
Ms. Manal Al-Asmari
Sequential Search Analysis• The statements in the for loop are repeated
several times• For each iteration of the loop, the search item
is compared with an element in the list• When analyzing a search algorithm, you count
the number of comparisons• Suppose that L is a list of length n• The number of key comparisons depends on
where ”in the list” the search item is located
Ms. Manal Al-Asmari
Sequential Search Analysis (continued)
• Best case• The item is the first element of the list• You make only one key comparison• Worst case• The item is the last element of the list• You make n key comparisons• What is the average case ?
Ms. Manal Al-Asmari
Sequential Search Analysis (continued)
• To determine the average case• Consider all possible cases• Find the number of comparisons for each
case• Add them and divide by the number of cases
Ms. Manal Al-Asmari
On average, a successful sequential search searches half the list
Sequential Search Analysis (continued)
Average Case: