CIS3023: Programming Fundamentals for CIS Majors II
Download
1 / 13

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


  • 87 Views
  • Uploaded on

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

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

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
Cis3023 programming fundamentals for cis majors ii summer 2010

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 Slides28 May 2010

GaneshViswanathan


Searching
Searching

  • 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]


Searching1
Searching

  • 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)


The search problem
The Search Problem

  • 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

  • Else “not found”: return -1


Linear search
Linear Search

  • // 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

  • }


Linear search1
Linear Search

  • Advantages:

  • easy and straightforward algorithm.

  • array can be in any order.

  • Disadvantages:

  • 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


Binary search
Binary Search

  • Searching a sorted array

  • Fast and efficient


Binary search1
Binary Search

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


Binary search2
Binary Search

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


Binary search i
Binary Search I

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

if (left > right)

return -1; // “not found”

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


Binary search ii
Binary Search II

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”

}

}

return -1; // “not found”

}

Iteration


Binary search3
Binary Search

  • Advantages:

  • fast and efficient!

  • Disadvantages:

  • array elements have to be in sorted order.

  • For an array of 1 million elements, linear search takes 500,000 comparisons to find the key, binary search takes only 20 comparisons!


Search
Search

Linear search in arbitrary array:

Reduce search region by 1 in each step

Binary search in sorted array:

Reduce search region by half in each step