searching and sorting
play

Searching and Sorting The Millennium Challenge Problems Searching - PDF document

Eric Roberts Handout #62 CS 106A March 5, 2010 Searching and Sorting The Millennium Challenge Problems Searching and Sorting Birch and Swinnerton-Dyer Conjecture Hodge Conjecture Navier-Stokes Equations P vs NP Poincar Conjecture


  1. Eric Roberts Handout #62 CS 106A March 5, 2010 Searching and Sorting The Millennium Challenge Problems Searching and Sorting Birch and Swinnerton-Dyer Conjecture Hodge Conjecture Navier-Stokes Equations P vs NP Poincaré Conjecture Riemann Hypothesis Yang-Mills Theory Rules Eric Roberts Millennium Meeting Videos CS 106A March 5, 2010 Searching Linear Search • Chapter 12 looks at two operations on arrays— searching and • The simplest strategy for searching is to start at the beginning sorting —both of which turn out to be important in a wide of the array and look at each element in turn. This algorithm range of practical applications. is called linear search . • The simpler of these two operations is searching , which is the • Linear search is straightforward to implement, as illustrated in process of finding a particular element in an array or some the following method that returns the first index at which the other kind of sequence. Typically, a method that implements value key appears in array , or - 1 if it does not appear at all: searching will return the index at which a particular element private int linearSearch(int key, int[] array) { appears, or - 1 if that element does not appear at all. The for (int i = 0; i < array.length; i++) { element you’re searching for is called the key . if (key == array[i]) return i; } • The goal of Chapter 12, however, is not simply to introduce return -1; searching and sorting but rather to use these operations to talk } about algorithms and efficiency. Many different algorithms exist for both searching and sorting; choosing the right algorithm for a particular application can have a profound effect on how efficiently that application runs. Simulating Linear Search A Larger Example • To illustrate the efficiency of linear search, it is useful to public void run() { work with a somewhat larger example. private int linearSearch(int key, int[] array) { for ( int i = 0 ; i < array.length ; i++ ) { • The example on the next slide works with an array containing if (key == array[i]) return i; many of the area codes assigned to the United States. } return -1; • The specific task in this example is to search this list to find } the area code for the Silicon Valley area, which is 650. i key array 10 27 • The linear search algorithm needs to examine each element in the array to find the matching value. As the array gets larger, the number of steps required for linear search grows in the 2 3 5 7 11 13 17 19 23 29 same proportion. 0 1 2 3 4 5 6 7 8 9 • As you watch the slow process of searching for 650 on the LinearSearch next slide, try to think of a more efficient way in which you linearSearch(17) -> 6 linearSearch(27) -> -1 might search this particular array for a given area code.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend