•0 recomendaciones•10 vistas

Denunciar

Compartir

Descargar para leer sin conexión

hihaihdohdaouhod

- 2. • The searching algorithms are used to search or find one or more than one element from a dataset. These type of algorithms are used to find elements from a specific data structures. • Searching may be sequential or not. If the data in the dataset are random, then we need to use sequential searching. Otherwise we can use other different techniques to reduce the complexity. • Search algorithm is any algorithm which solves the search problem, namely, to retrieve information stored within some data structure, or calculated in the search space of a problem domain, either with discrete or continuous values.
- 3. Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored. Based on the type of search operation, these algorithms are generally classified into two categories: • Sequential Search: In this, the list or array is traversed sequentially and every element is checked. For example: Linear Search. • Interval Search: These algorithms are specifically designed for searching in sorted data-structures. These type of searching algorithms are much more efficient than Linear Search as they repeatedly target the center of the search structure and divide the search space in half. For Example: Binary Search. •
- 4. Linear Search • a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.[1] • A linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of n+1/2 comparisons, but the average case can be affected if the search probabilities for each element vary. Linear search is rarely practical because other search algorithms and schemes, such as the binary search algorithm and hash tables, allow significantly faster searching for all but short lists.[2]
- 5. Time – Space Complexity Worst complexity: O(n) Average complexity: O(n) Space complexity: O(1) Worst-case space complexity: O(1) iterative Average performance: O(n/2)
- 6. • A linear search sequentially checks each element of the list until it finds an element that matches the target value. If the algorithm reaches the end of the list, the search terminates unsuccessfully.[1] Basic algorithm • Given a list L of n elements with values or records L0 .... Ln−1, and target value T, the following subroutine uses linear search to find the index of the target T in L.[3] • Set i to 0. • If Li = T, the search terminates successfully; return i. • Increase i by 1. • If i < n, go to step 2. Otherwise, the search terminates unsuccessfully.
- 7. With a sentinel • The basic algorithm above makes two comparisons per iteration: one to check if Li equals T, and the other to check if i still points to a valid index of the list. By adding an extra record Ln to the list (a sentinel value) that equals the target, the second comparison can be eliminated until the end of the search, making the algorithm faster. The search will reach the sentinel if the target is not contained within the list.[4] • Set i to 0. • If Li = T, go to step 4. • Increase i by 1 and go to step 2. • If i < n, the search terminates successfully; return i. Else, the search terminates unsuccessfully.
- 8. In an ordered table • If the list is ordered such that L0 ≤ L1 ... ≤ Ln−1, the search can establish the absence of the target more quickly by concluding the search once Li exceeds the target. This variation requires a sentinel that is greater than the target.[5] • Set i to 0. • If Li ≥ T, go to step 4. • Increase i by 1 and go to step 2. • If Li = T, the search terminates successfully; return i. Else, the search terminates unsuccessfully.
- 11. Homework: Use Linear search to Search for: 1001 and 904 2, 13, 45, 67, 98, 101, 456, 501, 602, 899, 904, 1000
- 12. Binary Search Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form. Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub- array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the right of the middle item. This process continues on the sub- array as well until the size of the subarray reduces to zero.
- 13. Binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. Time- Space Complexity • Worst complexity: O(log n) • Average complexity: O(log n) • Best complexity: O(1) • Space complexity: O(1) • Data structure: Array
- 14. How Binary Search Works? For a binary search to work, it is mandatory for the target array to be sorted. We shall learn the process of binary search with a pictorial example. The following is our sorted array and let us assume that we need to search the location of value 31 using binary search.
- 15. First, we shall determine half of the array by using this formula − mid = low + ((high - low) / 2 ) Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
- 16. Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array, so we also know that the target value must be in the upper portion of the array.
- 17. We change our low to mid + 1 and find the new mid value again. low = mid + 1 low = 4 + 1 mid = low + ((high - low) / 2) mid = 5 + ((9 – 5)/2) mid = 5+2 = 7 Our new mid is 7 now. We compare the value stored at location 7 with our target value 31. The value stored at location 7 is not a match, rather it is more than what we are looking for. So, the value must be in the lower part from this location.
- 18. Hence, we calculate the mid again. This time it is 5. low = mid + 1 low = 4 + 1 mid = low + ((high - low) / 2) mid = 5 + ((6 – 5)/2) mid = 5+ 0.5 = 5.5 or 5 We compare the value stored at location 5 with our target value. We find that it is a match.
- 25. Jump Search • Jump Search Algorithm is a relatively new algorithm for searching an element in a sorted array. • The fundamental idea behind this searching technique is to search fewer number of elements compared to linear search algorithm (which scans every element in the array to check if it matches with the element being searched or not). This can be done by skipping some fixed number of array elements or jumping ahead by fixed number of steps in every iteration.
- 26. Jump Search is a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements. • For example, suppose we have an array arr[] of size n and block (to be jumped) size m. Then we search at the indexes arr[0], arr[m], arr[2m]…..arr[km] and so on. Once we find the interval (arr[km] < x < arr[(k+1)m]), we perform a linear search operation from the index km to find the element x.
- 27. Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610). Length of the array is 16. Jump search will find the value of 55 with the following steps assuming that the block size to be jumped is 4. STEP 1: Jump from index 0 to index 4; STEP 2: Jump from index 4 to index 8; STEP 3: Jump from index 8 to index 12; STEP 4: Since the element at index 12 is greater than 55 we will jump back a step to come to index 8. STEP 5: Perform linear search from index 8 to get the element 55.
- 28. What is the optimal block size to be skipped? In the worst case, we have to do n/m jumps and if the last checked value is greater than the element to be searched for, we perform m-1 comparisons more for linear search. Therefore the total number of comparisons in the worst case will be ((n/m) + m-1). The value of the function ((n/m) + m-1) will be minimum when m = √n. Therefore, the best step size is m = √n.
- 29. How to choose the block or Jump size? If the jump size is 1, then it will be a linear search. Hence we usually take the jump size of SQRT(n). By doing so, in the worst case we do n/k jumps. Now let us understand the algorithm with help of example: 1, 1, 2, 3, 4, 10, 15, 20, 75, 80, 92, 95, 100 Search Key = 80 N = 13 Jump size = SQRT(13) = 3
- 31. Homework: Given: - 1, 0, 5, 9, 23, 45, 55, 67, 78, 88, 99, 101, 105, 206 Perform Jump Search to search for: 206
- 32. Interpolation search • Interpolation search is an improvement over binary search. • Binary Search always checks the value at middle index. But, interpolation search may check at different locations based on the value of element being searched. • For interpolation search to work efficiently the array elements/data should be sorted and uniformly distributed. • Steps: • A - Array of elements, e - element to be searched, pos - current position • Make start = 0 & end = n-1 • calculate position ( pos ) to start searching at using formula:
- 33. • If A[pos] == e , element found at index pos. • Otherwise if e > A[pos] we make start = pos + 1 • Else if e < A[pos] we make end = pos -1 • Do stpes 3, 4, 5, 6 While : start <= end && e >= A[start] && e =< A[end] • start <= end - that is until we have elements in the sub-array. • e >= A[start] - element we are looking for is greater than or equal to the starting element of sub-array we are looking in. • e =< A[end] - element we are looking for is less than or equal to the last element of sub- array we are looking in.
- 34. • Example if we are looking for element 4 in the given array.
- 35. Given: - 1, 0, 5, 9, 23, 45, 55, 67, 78, 88, 99, 101, 105, 206 Perform Interpolation Search to search for: 99