Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.

Similar presentations


Presentation on theme: "1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its."— Presentation transcript:

1 1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its Implementation.  Brief Analysis of Binary Search.  Preview: Role of Interfaces in Large-Scale Software.

2 2 Searching Algorithms What is Searching?  Searching means scanning through a list of items or records to find if a particular one exists.  It usually requires the user to specify the target item (or a target key e.g. id_no, name, etc.)  If the target item is found, the item or its location is returned, otherwise, an appropriate message or flag (example –1) is returned.  Like sorting, an important issue in processing a search request is response time.  This depends on factors similar to those that affect sorting such as: The size of the list. The data structure used; array, linked-list, binary tree. The organization of data; random or ordered.  We restrict our discussion to methods for lists represented as arrays.

3 3 Searching Algorithms Linear Search: An Implementation  This involves searching through the list sequentially until the target item is found or the list is exhausted.  If the target is found, its location is returned, otherwise a flag such as –1 is returned.  The following implements Linear Search. public class LinearSearch { public static int search(int[] a, int v) { for (int i = 0; i < a.length; i++) { if (a[i] == v) return i; } return -1; }

4 4 Searching Algorithms Linear Search: An Implementation (Cont’d) public static void main(String[] args) throws IOException { BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); int[] a = ArrayUtil.randomIntArray(20, 100); ArrayUtil.print(a); System.out.println("Enter number to search for:"); int n = Integer.parseInt(console.readLine()); int j = search(a, n); if (j != -1) System.out.println("Found in position " + j); else System.out.println(n + " not found"); } }

5 5 Searching Algorithms Brief Analysis of Linear Search  For a list of n elements, the linear search takes an average of n/2 comparisons to find an item, with the best case being 1 comparison and the worst case being n comparisons.  However, if the list is ordered, it is a waste of time to look for an item using linear search (it would be like looking for a word in a dictionary sequentially). In this case we apply binary search, which we discuss next.

6 6 Searching Algorithms Binary Search: An Implementation  Binary search works by comparing the target with the item at the middle of the list. This leads two one of three results:  The middle item is the target – we are done.  The middle item is less than the target – we apply the algorithm to the upper half of the list.  The middle item is bigger than the target – we apply the algorithm to the lower half of the list.  This process is repeated until the item is found or the list is exhausted.  The following implements this approach.

7 7 Searching Algorithms Binary Search: An Implementation (Cont’d) import java.io.*; public class BinarySearch { public static int binarySearch(int[] a, int from, int to, int v) { if (from > to) return -1; int mid = (from + to) / 2; int diff = a[mid] - v; if (diff == 0) // a[mid] == v return mid; else if (diff < 0) // a[mid] < v return binarySearch(a, mid + 1, to, v); else return binarySearch(a, from, mid - 1, v); } public static int search(int[] a, int v) { return binarySearch(a, 0, a.length - 1, v); } public static void main(String[] args) throws IOException { BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); int[] a=ArrayUtil.randomIntArray(20, 100); SelectSort.sort(a); ArrayUtil.print(a); System.out.println("Enter number to search for:"); int n = Integer.parseInt(console.readLine()); int j = search(a, n); System.out.println("Found in position " + j); } }

8 8 Searching Algorithms Binary Search

9 9 Searching Algorithms Binary Search (Cont’d)

10 10 Searching Algorithms Brief Analysis of Binary Search Binary search is by far more efficient than linear search. – the number of comparisons required is on the average log 2 (n).  Thus, for a list of 1000 items, binary search requires only log 2 (1000)  10, whereas linear search requires 1000/2 = 500. The difference gets more dramatic with larger lists as the following table shows.  Binary search has one problem -- it is only applicable to ordered data. Thus, there is an additional overhead of sorting which can be very significant.  We shall experiment the efficiency of the various sorting/searching discussed so far in the Lab, but the following table should give an idea. Array sizeLog 2 NNN Log 2 NN2N2 8382464 1287 89616,384 2568 204865,536 1,000101,00010,0001 million 100,00017100,0001.7 million10 billion


Download ppt "1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its."

Similar presentations


Ads by Google