This preview shows pages 1–6. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 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 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. 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 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 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 1 2 3 4 5 6 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 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)); } 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 10 A Larger Example • To illustrate the efficiency of linear search, it is useful to work with a somewhat larger example....
View
Full
Document
This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.
 Fall '08
 SAHAMI,M
 Sort, Through the Looking Glass

Click to edit the document details