1 / 13

# CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 - PowerPoint PPT Presentation

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Searching. “ Some things Man was never meant to know. For everything else, there’s Google! ”. Course Lecture Slides 28 May 2010. Ganesh Viswanathan. Searching. Querying for something

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CIS3023: Programming Fundamentals for CIS Majors II Summer 2010' - harmon

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Summer 2010

Searching

“ Some things Man was never meant to know. For everything else, there’s Google! ”

Course Lecture Slides28 May 2010

GaneshViswanathan

• Querying for something

• Searching an array for a particular value is a common problem

• Example: Where is 100 in this array?

n=10

A[1]

A[0]

A[n-1]

• Querying for something

• Searching an array for a particular value is a common problem

• Example: Where is 100 in this array?

n=10

A[1]

A[0]

A[n-1]

Must return location in the array (index=4)

• Let A be the array to be searched

• n - the number of elements

• k- the search target (or key)

• Question: Does k occur in A?

• If k appears in A[0], A[1], …, A[n-1]: “found” determine its indexi.

• that is, find i such that A[i] == k

• // Performs linear search of array for key, from start index to end index.

• intlinearSearch (int[ ] A, int key, int start, int end) {

• for (inti = start; i < end; i++) {

• if (key == A[i]) {

• return i; // “found key”, return index

• }

• }

• return -1; // “key not found”, return -1

• }

• easy and straightforward algorithm.

• array can be in any order.

• slow and inefficient!

• Given an array of n elements, it examines

• n/2 elements on average for value in array,

• nelements for value not in array

• Searching a sorted array

• Fast and efficient

• 1. get the middle element;

• 2. if middle element equals searched value, then the algorithm stops (return index);

• 3. otherwise, two cases are possible:

• * 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.

• * 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.

left

A[n/2]

right

if (A[n/2] == key) return “found”

else if (A[n/2] > key) search in left

else search in the right

intbinarySearch(int[] array, int key, int left, int right) {

if (left > right)

int middle = (left + right) / 2;

if (array[middle] == key)

return middle; // “found”

elseif (array[middle] > key)

returnbinarySearch(array, key, left, middle-1);

else

returnbinarySearch(array, key, middle+1, right);

}

Recursion

intbinarySearch(int[] array, int key, int left, int right){

while (left < right) {

int middle = (left + right)/2; // Compute mid point

if (array[mid] > key) {

right = mid; // repeat search in bottom half

} else if (array[mid] < key) {

left = mid + 1; // Repeat search in top half

} else {

return mid; // “found”

}

}

}

Iteration

• fast and efficient!