Docsity

Searching Algorithms: Linear and Binary Search, Slides of Data Structures and Algorithms

Partial preview of the text

Download Searching Algorithms: Linear and Binary Search and more Slides Data Structures and Algorithms in PDF only on Docsity!

Searching

Chapter 5

Outline

Introduction

Linear search

Binary Search

Searching

Searching can be done on unsorted or sorted data

Many search techniques exist: we will consider 3:  (^) linear search (on both unsorted and sorted arrays or linked lists)  (^) binary search (on only sorted arrays)  (^) hash tables (Next Lecture)

Linear Search  (^) Known as sequential search  (^) Data may be sorted or unsorted

 unsorted and sorted arrays or linked lists

 (^) Very simple algorithm (assume searching for key K):

It runs in O(n) time (i.e., linear time) since every element is inspected in the worst case.  (^) Searching a linked list is much the same, just moving down the links instead of incrementing a counter for (i=0; i<n; i++) { if (array[i] == K) return i; } return -1; // i.e. not found

Exercise

Write the search algorithm for linked list.

Binary Search  Takes advantage of knowledge that array is sorted  Algorithm:  (^) Set low = 0 and high = n−  (^) Compare the target value to the median candidate mid = ⌊(low+high)/2⌋  (^) We consider three cases:  (^) If the target equals the median candidate, then we have found the item we are looking for, and the search terminates successfully.  (^) If the target is less than the median candidate, then we recur on the first half of the sequence, that is, on the interval of indices from low to mid−1.  (^) If the target is greater than the median candidate, then we recur on the second half of the sequence, that is, on the interval of indices from mid+1 to high.  (^) An unsuccessful search occurs if low > high, as the interval [low,high] is empty.

Binary Search-Implmentation 1 /** 2 * Returns true if the target value is found in the indicated portion of the data array. 3 * This search only considers the array portion from data[low] to data[high] inclusive. 4 */ 5 public static boolean binarySearch(int[ ] data, int target, int low, int high) { 6 if (low > high) 7 return false; // interval empty; no match 8 else { 9 int mid = (low + high) / 2; 10 if (target == data[mid]) 11 return true; // found a match 12 else if (target < data[mid]) 13 return binarySearch(data, target, low, mid − 1); // recur left of the middle 14 else 15 return binarySearch(data, target, mid + 1, high); // recur right of the middle 16 } 17 }

Binary Search- Performance

Binary search runs in O(logn) time. This is a significant improvement comparing with sequential search.  (^) if n is 1 billion, log n is only 30

Much faster than linear search of sorted arrays

Binary Search Example 2 7 6 5 4 3 2 1 0 100 91 80 66 52 35 20 10 i j Key is found Search ends i j Cycle 2 Starts i = 0 , j = 2, Mid: 1^7 6 5 4 3 2 1 100 91 80 66 52 35 20 10 i j 7 6 5 4 3 2 1 0 100 91 80 66 52 35 20 10 Cycle 3 Starts i = 0 , j = 0, Mid: 0 Apply the Binary Search algorithm to find the key value : 10 Highlight all elements you visited during the search process Cycle 1 Starts i = 0 , j = 7, Mid: 3 13

Binary Search Example 3 8 7 6 5 4 3 2 1 0 11 0 10 0 95 80 75 70 50 40 30 indices 8 7 6 5 4 3 2 1 0 11 0 10 0 95 80 75 70 50 40 30 indices Apply the Binary Search algorithm to find the key value : 50 Highlight all elements you visited during the search process Cycle 1 Starts i = 0 , j = 8, Mid: 4 Cycle 2 Starts i = 0 , j = 3, Mid: 1

i j

i j

11 0 10 0 95 80 75 70 50 40 30 indices Cycle 3 Starts i = 2 , j = 3, Mid: 2

i j

Key is found Search ends 14

Binary Search Example 5 6 5 4 3 2 1 0 250 222 208 155 120 80 40 Apply the Binary Search algorithm to find the key value : 224 Highlight all elements you visited during the search process Cycle 1 Starts i = 0 , j = 6, Mid: 3 6 5 4 3 2 1 0 250 222 208 155 120 80 40 Cycle 2 Starts i = 4 , j = 6, Mid: 5

i j

i j

250 222 208 155 120 80 40 Cycle 3 Starts i = 6 , j = 6, Mid: 6

i j

Ends the loop and search because i = 6 , j = 5 Key is not found Search ends