CS Notes 6

# CS Notes 6 - o Best with linked lists o Once it is matched,...

This preview shows pages 1–2. Sign up to view the full content.

Searching Wednesday, February 02, 2011 4:19 PM Sequential (Linear) Search Successful search Worst case: O(n) Best case : O(1) Average: O(n/2)->No constants->O(n) Basic Op: Comparing target against item in data structure Failures Worst case: O(n) Best Case: O(n) Average Case: O(n) Average analysis What is the # of comparisons? o [1+2+3+…+n]/n=[n(n+1)]/2 o (n+1)/2 o Using 'real' average time to improve performance n-1 o Avg= Σ probability * position of element i=0 o Rearrange array items in decreasing order of probabilities Adaptive ways to improve search time 1. When a match is found, move item to front

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: o Best with linked lists o Once it is matched, it is likely that it will be searched again 1. Move one spot towards front o Less extreme than 1 Binary Search Only applies to sorted arrays Divide and conquer Sorted array 5 10 15 20 25 30 35 40 45 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; }...
View Full Document

## This note was uploaded on 02/29/2012 for the course 198 112 taught by Professor Venugopal during the Spring '09 term at Rutgers.

### Page1 / 2

CS Notes 6 - o Best with linked lists o Once it is matched,...

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

View Full Document
Ask a homework question - tutors are online