340 likes | 473 Views
Chapter 10. Applications of Arrays and Strings. Chapter Objectives. Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement the binary search algorithm Become aware of the class Vector
E N D
Chapter 10 Applications of Arrays and Strings
Chapter Objectives • Learn how to implement the sequential search algorithm • Explore how to sort an array using the selection sort algorithm • Learn how to implement the binary search algorithm • Become aware of the class Vector • Learn more about manipulating strings using the class String
List Processing • List: a set of values of the same type • Basic operations performed on a list: • Search list for given item • Sort list • Insert item in list • Delete item from list
Search • Necessary components to search a list: • Array containing the list • Length of the list • Item for which you are searching • After search completed: • If item found, report “success”, return location in array • If item not found, report “failure”
Sorting a List • Selection sort • Sorting algorithm • List is sorted by selecting list element and moving it to its proper position • Algorithm finds position of smallest element and moves it to top of unsorted portion of list • Repeats process above until entire list is sorted
Binary Search • Can only be performed on a sorted list • Uses divide and conquer technique to search list • If L is a sorted list of size n, to determine whether an element is in L, the binary search makes at most 2 * log2n + 2 key comparisons • (Faster than a sequential search)
Binary Search Algorithm • Search item is compared with middle element of list • If search item < middle element of list, search is restricted to first half of the list • If search item > middle element of list, search second half of the list • If search item = middle element, search is complete
Vectors • The class Vector can be used to implement a list • Unlike an array, the size of a Vector object can grow/shrink during program execution • You do not need to worry about the number of data elements in vector
Vectors • Every element of a Vector object is a reference variable of the type Object • To add an element into a Vector object • Create appropriate object • Store data into object • Store address of object holding data into Vector object element
Programming Example: Election Results • Input: two files • File 1: candidates’ names • File 2: voting data • Voting Data Format: • candidate_name region# number_of_votes_for_this_candidate
Programming Example: Election Results • Output: election results in a tabular form • each candidate’s name • number of votes each candidate received in each region • total number of votes each candidate received
Programming Example:Election Results (Solution) The solution includes: • Reading the candidates’ names into the array candidateName • A two-dimensional array consisting of the votes by Region • An array consisting of the total votes parallel to the candidateName array
Programming Example:Election Results (Solution) • Sorting the array candidatesName • Processing the voting data • Calculating the total votes received by each candidate • Outputting the results in tabular form
Programming Example: Pig Latin Strings • If string begins with a vowel “-way” is appended to it • If first character is not a vowel • Add “-” to end • Rotate characters until the first character is a vowel • Append “ay” • Input: string • Output: string in pig Latin
Programming Example: Pig Latin Strings (Solution) • Methods: isVowel, rotate, pigLatinString • Use methods to: • Get the string (str) • Find the pig Latin form of str by using the method pigLatinString • Output the pig Latin form of str
Chapter Summary • Lists • Searching lists • Sequential searching order • Binary Search • Sorting lists • Selection sort
Chapter Summary • Programming examples • The class Vector • Members of the class Vector • The class String • Additional methods of the class String