DOC PREVIEW
Stanford CS 106A - Searching And Sorting

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Searching and SortingPowerPoint PresentationOnce upon a time . . .The Millennium Challenge ProblemsSlide 5SearchingLinear SearchSimulating Linear SearchA Larger ExampleLinear Search (Area Code Example)The Idea of Binary SearchBinary Search (Area Code Example)Implementing Binary SearchEfficiency of Linear SearchEfficiency of Binary SearchComparing Search EfficienciesSortingThe Selection Sort AlgorithmSimulating Selection SortThe Efficiency of Selection SortQuadratic GrowthSorting Punched CardsThe Radix Sort AlgorithmSimulating Radix SortComparing N 2 and N log NThe EndSearching and SortingEric RobertsCS 106AMarch 5, 2010Once upon a time . . .The Millennium Challenge ProblemsSearching and SortingSearching•Chapter 12 looks at two operations on arrays—searching and sorting—both of which turn out to be important in a wide range of practical applications.•The simpler of these two operations is searching, which is the process of finding a particular element in an array or some other kind of sequence. Typically, a method that implements searching will return the index at which a particular element appears, or -1 if that element does not appear at all. The element you’re searching for is called the key.•The goal of Chapter 12, however, is not simply to introduce 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.Linear Search•The simplest strategy for searching is to start at the beginning of the array and look at each element in turn. This algorithm is called linear search.•Linear search is straightforward to implement, as illustrated in the following method that returns the first index at which the value key appears in array, or -1 if it does not appear at all:private int linearSearch(int key, int[] array) { for (int i = 0; i < array.length; i++) { if (key == array[i]) return i; } return -1;}Simulating Linear Search skip simulationpublic void run() { int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; println("linearSearch(17) -> " + linearSearch(17, primes)); println("linearSearch(27) -> " + linearSearch(27, primes));}primesLinearSearchlinearSearch(17) -> 6linearSearch(27) -> -10 1 2 3 4 5 6 7 82 3 5 7 11 13 17 19 23 299private int linearSearch(int key, int[] array) { for ( int i = 0 ; i < array.length ; i++ ) { if (key == array[i]) return i; } return -1;}27key arrayi0123456private int linearSearch(int key, int[] array) { for ( int i = 0 ; i < array.length ; i++ ) { if (key == array[i]) return i; } return -1;}27key arrayi012345678910public void run() { int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; println("linearSearch(17) -> " + linearSearch(17, primes)); println("linearSearch(27) -> " + linearSearch(27, primes));}0 1 2 3 4 5 6 7 82 3 5 7 11 13 17 19 23 299private int linearSearch(int key, int[] array) { for ( int i = 0 ; i < array.length ; i++ ) { if (key == array[i]) return i; } return -1;}27key arrayi10A Larger Example•To illustrate the efficiency of linear search, it is useful to work with a somewhat larger example.•The example on the next slide works with an array containing many of the area codes assigned to the United States.•The specific task in this example is to search this list to find the area code for the Silicon Valley area, which is 650.•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 same proportion.•As you watch the slow process of searching for 650 on the next slide, try to think of a more efficient way in which you might search this particular array for a given area code.Linear Search (Area Code Example)6500 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2122 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 4344 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 6566 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 8788 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285201 202 203 205 206 207 208 209 210 212 213 214 215 216 217 218 219 224 225 228 229 231234 239 240 248 251 252 253 254 256 260 262 267 269 270 276 281 283 301 302 303 304 305307 308 309 310 312 313 314 315 316 317 318 319 320 321 323 325 330 331 334 336 337 339347 351 352 360 361 364 385 386 401 402 404 405 406 407 408 409 410 412 413 414 415 416417 419 423 424 425 430 432 434 435 440 443 445 469 470 475 478 479 480 484 501 502 503504 505 507 508 509 510 512 513 515 516 517 518 520 530 540 541 551 559 561 562 563 564567 570 571 573 574 575 580 585 586 601 602 603 605 606 607 608 609 610 612 614 615 616617 618 619 620 623 626 630 631 636 641 646 660 661 662 678 682 701 702 703 704706 707 708 712 713 714 715 716 717 718 719 720 724 727 731 732 734 740 754 757 760 762763 765 769 770 772 773 774 775 779 781 785 786 801 802 803 804 805 806 808 810 812 813814 815 816 817 818 828 830 831 832 835 843 845 847 848 850 856 857 858 859 860 862 863864 865 870 878 901 903 904 906 907 908 909 910 912 913 914 915 916 917 918 919 920 925928 931 936 937 940 941 947 949 951 952 954 956 959 970 971 972 973 978 979 980 985 989651The Idea of Binary Search•The fact that the area code array is in ascending order makes it possible to find a particular value much more efficiently.•The key insight is …


View Full Document

Stanford CS 106A - Searching And Sorting

Download Searching And Sorting
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Searching And Sorting and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Searching And Sorting 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?