Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.

Similar presentations


Presentation on theme: "Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and."— Presentation transcript:

1 Searching

2 Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list. Look at every element : This is a very straightforward loop comparing every element in the array with the key. As soon as an equal value is found, it returns. If the loop finishes without finding a match, the search failed and -1 is returned.

3

4 int LinearSearch(const int *Array, const int Size, const int ValToSearch) { bool NotFound = true; int i = 0; while(i < Size && NotFound) { if(ValToSearch != Array[i]) i++; else NotFound = false; } if( NotFound == false ) return i; else return -1; }

5 Binary search algorithm 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. A fast way to search a sorted array is to use a binary search. The idea is to look at the element in the middle. If the key is equal to that, the search is finished. If the key is less than the middle element, do a binary search on the first half. If it's greater, do a binary search of the second half.

6 Algorithm Algorithm is quite simple. It can be done either recursively or iteratively: 1. Get the middle element; 2. If the middle element equals to the searched value, the algorithm stops; 3. Otherwise, two cases are possible: o searched value is less than the middle element. In this case, go to the step 1 for the part of the array before middle element. o searched value is greater than the middle element. In this case, go to the step 1 for the part of the array after middle element.

7 Example : we have the array X[12]: Search for b=12 mid = (0+11)/2 = 5. Compare b with X[5]: 12<20. So search in left half X[0..4] mid = (0+4)/2 = 2. Compare b with X[2]: 12 > 7. So search right half X[3..4] mid = (3+4)/2 = 3.Compare b with X[3]: b=X[3]=12. Return 3.

8 8 Example: Express the linear search algorithm as a recursive procedure. 8 Recursive Algorithms (the linear search algorithm ) ALGORITHM 1 A Recursive Linear Search Algorithm. procedure search(i,j,x: i,j,x integers, 1 <=i<=n, 1<=j<=n) if a i = x then location := i else if i = j then location : = 0 else search(i+1,j, x)

9 9 Example: Construct a recursive version of a binary search algorithm 9 ALGORITHM 2 -1 A Recursive Binary Search Algorithm. procedure binarySearch(i,j,x: i,j,x integers, 1 <= i <= n, 1<= j <= n) m := if x = a m then location := m else if (x < a m and i < m ) then binarySearch( x, i, m-1) else if (x > a m and j > m ) then binarySearch(x, m+1, j) else location := 0 Recursive Algorithms (the binary search algorithm )

10 Lecturer: Shaykhah 10 Example: Construct a recursive version of a binary search algorithm 10 ALGORITHM 2-2 A Recursive Binary Search Algorithm. procedure binarySearch(i,j,x: i,j,x integers, 1 <= i <= n, 1<= j <= n) m := if x = a m then location := m else if (i=j) then location := -1 else if (x < a m ) then binarySearch( x, i, m-1) else if (x > a m ) then binarySearch(x, m+1, j) Recursive Algorithms (the binary search algorithm ): another way


Download ppt "Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and."

Similar presentations


Ads by Google