Handout 11

Handout 11 - Mehran Sahami CS103B Handout#11 Analyzing...

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

View Full Document Right Arrow Icon
Mehran Sahami Handout #11 CS103B January 23, 2009 Analyzing Searching and Sorting In examining methods to analyze both recursive and non-recursive algorithms, it is only fitting to spend a little time looking at a variety of common algorithms and their corresponding algorithmic complexity. Below we review a number of algorithms (focused primarily on searching and sorting), explaining what they do and their time complexity. Searching for Elements in Arrays There are two common algorithms that are used to search an array to see if it contains a particular element. The first, which we examine presently, is a simple linear search of the list. The second algorithm is only applicable to use on sorted lists, but runs much more quickly than linear search. This algorithm, known as binary search, makes use of the fact that the list it is searching is sorted to vastly improve its time complexity. Linear Search The C++-like pseudo-code for linear search is given below. /* * Function: LinearSearch * a is the array to search, key is the element to look for. * LinearSearch returns the index of element matching key * if one is found, and 1 otherwise */ int LinearSearch(int a[], int n, int key) { for(int i = 0; i < n; i++) { if (a[i] == key) return i } return 1 } It is very easy to see that the running time of Linear search is O(n) since it has a single for loop that iterates up to n times, and in the worst case, all elements of the array must be examined. Binary Search This algorithm, which you are likely already familiar with, is easily explained in terms of searching a dictionary for a word. In a dictionary, words are sorted alphabetically. For simplicity, let us assume there is only one page for all words starting with each letter. Let us assume we wish to search for a word starting with some particular letter.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 We open the dictionary at some page mid-way through, let us say a page on which words start with M . If the value of our letter is M , then we have found what we are looking for and the word is on the current page. If the value of our letter is less than M , we know that the word would be found in the first half of the book (i.e. we should search for the word in the pages preceding the current page). If the value of our letter is greater than M , we should search the pages following the current page. In either case, the effective size of the dictionary to be searched is reduced to about half the original size. We repeat the process in the appropriate half, opening to somewhere in the middle of that section and checking again. As the process is repeated, the effective size of the dictionary to be searched reduces by about half at each step until the word is found on a current page. Binary search essentially follows this approach.
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.

This note was uploaded on 09/26/2009 for the course CS 103B taught by Professor Sahami,m during the Winter '08 term at Stanford.

Page1 / 11

Handout 11 - Mehran Sahami CS103B Handout#11 Analyzing...

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