19searchsort_3014

19searchsort_3014 - Lecture File 19 COP 3014 January 17,...

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

View Full Document Right Arrow Icon
Lecture File 19 COP 3014 January 17, 2008 A. Ford Tyson 1 COP 3014 Lecture File 19 ± Searching and Sorting One- Dimensional Array 1 Dimensional Arrays –linear search –binary search –selection sort Copyright 1997-present, Ann Ford Tyson Searching ± search: scan a collection of data looking for a particular value – retrieve the information 2 retrieve the information – modify/update it –pr int it – delete it – locate related information –etc. Searching: examples ± Using a univeristy library online catalog – look for a book with title "Pride and Prejudice" – which library has it? more than one? are any copies available? where? 3 – are any copies available? where? – what is the catalog number? etc. ± On the Internet – search for all sites which refer to "job hunting"
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture File 19 COP 3014 January 17, 2008 A. Ford Tyson 2 Example: Area Codes and Corresponding Locations ± Assume we have two parallel arrays of size N, one with area codes and the other with the corresponding locations 4 ± if you find the index of the area code in the first array, you can use that index to find the location in the 2nd array ± We will consider two search algorithms – linear search – binary search areaCodes [0] areaCodes [1] 202 203 Wash. D.C. Connecticut locations [0] 5 areaCodes [N- 1] 802 areaCodes (int) Vermont locations [N-1] locations (string) Linear Search ± Linear Search algorithm look at 1st value and test for match if match stop otherwise 6 if match, stop, otherwise look at 2nd value and test for match if match, stop, otherwise look at 3rd value etc. .. also stop if hit end of array
Background image of page 2
January 17, 2008 A. Ford Tyson 3 202 203 401 413 index == 0 == index to 1st item in the array being searched index == 1 index == 2 Let N == 8. Search for 607 (linear) 7 516 607 717 802 index == 3 index == 4 index == 5 found with 6 comparison s Linear Search p.2 ± example call LinearSearch (NUM_AREA_CODES, areaCodes, 8 codeWanted, found, foundAt); ± if (found) cout << "Location is " << locations [foundAt]; else cout << "Area code not found."; Linear Search Function void LinearSearch (int NUM_AREA_CODES, // number of codes const int areaCodes [ ], // array of area codes 9 int codeWanted, // code to search for bool& found, // true if we find it
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/14/2011 for the course COP 3014 taught by Professor Tyson during the Fall '10 term at FSU.

Page1 / 12

19searchsort_3014 - Lecture File 19 COP 3014 January 17,...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online