CS notes 7

CS notes 7 - Binary Search Only applies to sorted arrays...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Binary Search Only applies to sorted arrays Divide and conquer Sorted array 5 10 15 20 25 30 35 40 45 0 1 2 3 4 5 6 7 8 low Mid High Target= 40 40!=25 40>25 Public static boolean binarySearch (int [] list, int target) { Int lo=0, hi=list.length-1; While( lo <=hi) { Int mid= (lo+hi)/2; If (target==list[mid]) { Return true; } If (target < list [mid) { Hi=mid-1; } else { Lo=mid+1; } } Return false; } Running Time Analysis using Comparison Tree Depends on list length- not the things in it Tree for array of length=9 9 1 6 0 2 5 7 3 8 Successful Search Worst case #c=7 Average #c= LOOK IN NOTEBOOK
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Problems with Binary search Need a sorted array If structure (array) has frequent inserts and deletes, o Insert in sorted array: O(n) worst case o Delete in sorted array: O(n) best case Review of searches Linear Search Binary Search (Sorted array) BST AVL Tree Search Worst Case O(n) O(log n) O(n) O(log n) Average O(n) O(log n) Best Case O(1) O(1) Binary Search Trees Monday, February 14, 2011 4:27 PM
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

CS notes 7 - Binary Search Only applies to sorted arrays...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online