Download presentation
Presentation is loading. Please wait.
Published byTiffany Powers Modified over 9 years ago
1
Chapter 10 Applications of Arrays and Strings
2
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
3
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
4
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”
5
Search
6
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
7
Selection Sort
8
Sequential Ordered Search
9
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)
10
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
11
Binary Search
12
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
13
Members of the class Vector
14
Methods of the class Vector
17
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
18
Vector StringList: After Adding Four Strings
19
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
20
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
21
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
22
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
23
Programming Example: Election Results
25
Additional String Methods
29
Effects of some String Methods
30
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
31
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
32
Programming Example: Pig Latin Strings (Sample Runs)
33
Chapter Summary Lists Searching lists –Sequential searching order –Binary Search Sorting lists –Selection sort
34
Chapter Summary Programming examples The class Vector –Members of the class Vector The class String –Additional methods of the class String
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.