Chapter 10 Sorting and Searching 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 10-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 10 - 1 Chapter 10 Sorting and Searching
Chapter 10 Objectives After you have read and studied this chapter, you should be able to e Perform linear and binary search algorithms on small arrays. e Determine whether a linear or binary search is more effective for a given situation Perform selection and bubble sort algorithms e Describe the heapsort algorithm and show how its performance is superior to the other two algorithms e Apply basic sorting algorithms to sort an array of objects C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 10-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 10 - 2 Chapter 10 Objectives After you have read and studied this chapter, you should be able to Perform linear and binary search algorithms on small arrays. Determine whether a linear or binary search is more effective for a given situation. Perform selection and bubble sort algorithms. Describe the heapsort algorithm and show how its performance is superior to the other two algorithms. Apply basic sorting algorithms to sort an array of objects
Searching r When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly r We will use an array of integers to present searching algorithms Here's the problem statement: Given a value X return the index of X in the array if suchX exists. Otherwise, return NOT_ FOUND(1). We assume there are no duplicate entries in the array r We will count the number of comparisons the algorithms make to analyze their performance. The ideal searching algorithm will make the least possible number of comparisons to locate the desired data r Two separate performance analyses are normally done, one for successful search and another for unsuccessful search C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 10-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 10 - 3 Searching When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly. We will use an array of integers to present searching algorithms. Here’s the problem statement: Given a value X, return the index of X in the array, if such X exists. Otherwise, return NOT_FOUND (-1). We assume there are no duplicate entries in the array. We will count the number of comparisons the algorithms make to analyze their performance. The ideal searching algorithm will make the least possible number of comparisons to locate the desired data. Two separate performance analyses are normally done, one for successful search and another for unsuccessful search
Search result Unsuccessful search: search( 45 NOT FOUND Successful search: search( 12 4 number 0 234567 8 23175901244388477 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 10-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 10 - 4 Search Result number 23 17 5 90 12 44 38 0 1 2 3 4 5 6 7 8 84 77 Unsuccessful Search: Successful Search: search( 45 ) NOT_FOUND search( 12 ) 4
Linear search r Search the array from the first to the last position in linear progression public int linearSearch int[] number, int searchvalue int loc hile( loc number length & number [loc searchvalue) i 1oc++; number length)[ //Not found eturn NOT FOUND else return loc; //Found, return the position C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 10-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 10 - 5 Linear Search Search the array from the first to the last position in linear progression. public int linearSearch ( int[] number, int searchValue ) { int loc = 0; while ( loc < number.length && number[loc] != searchValue ) { loc++; } if ( loc == number.length) { //Not found return NOT_FOUND; } else { return loc; //Found, return the position } }