Chapter 10

1 / 34

# Chapter 10 - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Chapter 10' - graiden-guerrero

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 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
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
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:
Programming Example: Election Results
• Output: election results in a tabular form
• each candidate’s name
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
• 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
• 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