chapter 10 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 10 PowerPoint Presentation
Download Presentation
Chapter 10

Loading in 2 Seconds...

play fullscreen
1 / 34

Chapter 10 - PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on

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

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

PowerPoint Slideshow about 'Chapter 10' - graiden-guerrero


An Image/Link below is provided (as is) to download presentation

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

Chapter 10

Applications of Arrays and Strings

chapter objectives
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 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
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
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
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
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
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
vectors1
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
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 results1
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
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 solution1
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
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
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
Chapter Summary
  • Lists
  • Searching lists
    • Sequential searching order
    • Binary Search
  • Sorting lists
    • Selection sort
chapter summary1
Chapter Summary
  • Programming examples
  • The class Vector
    • Members of the class Vector
  • The class String
    • Additional methods of the class String