{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CH12-SearchingAndSorting

CH12-SearchingAndSorting - TheArtandScienceof CHAPTER 12...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Searching and Sorting C H A P T E R 1 2 “I weep for you,” the Walrus said, “I deeply sympathize.” With sobs and tears he sorted out Those of the largest size 12.1 Searching 12.2 Sorting 12.3 Assessing algorithmic efficiency 12.4 Using data files —Lewis Carroll, Through the Looking Glass, 1872
Image of page 1

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

View Full Document Right Arrow Icon
Searching This chapter 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.
Image of page 2
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; }
Image of page 3

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

View Full Document Right Arrow Icon
Simulating Linear Search skip simulation public 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)); } primes LinearSearch linearSearch(17) -> 6 linearSearch(27) -> -1 0 1 2 3 4 5 6 7 8 2 3 5 7 11 13 17 19 23 29 9 private int linearSearch(int key, int[] array) { for ( int i = 0 ; i < array.length ; i++ ) { if (key == array[i]) return i; } return -1; } 27 key array i 0 1 2 3 4 5 6 7 8 9 10 public 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)); } 5 6 7 8 3 5 7 13 17 19 23 29 9 private int linearSearch(int key, int[] array) { if (key == array[i]) return i; } 27 key array i
Image of page 4
A 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 the 286 telephone 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.
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern