Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 10: Applications of Arrays and the class vector
C++ Programming:. From Problem Analysis
Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Topic 24 sorting and searching arrays "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be."
Chapter 9: Searching, Sorting, and Algorithm Analysis
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Data Structures Using Java1 Chapter 8 Search Algorithms.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
C++ for Engineers and Scientists Third Edition
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Using Arrays and File Handling
Chapter 16: Searching, Sorting, and the vector Type.
Data Structures Using C++1 Chapter 9 Search Algorithms.
Chapter 8 Arrays and Strings
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 14: Searching and Sorting
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
ALGORITHMS.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Merge Sort Presentation By: Justin Corpron. In the Beginning… John von Neumann ( ) Stored program Developed merge sort for EDVAC in 1945.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 16: Searching, Sorting, and the vector Type.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Java Programming: Guided Learning with Early Objects Chapter 8 Applications of Arrays (Sorting and Searching) and Strings.
Merge Sort.
Chapter 16: Searching, Sorting, and the vector Type
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
C++ Programming:. Program Design Including
Standard Version of Starting Out with C++, 4th Edition
Principles of Computing – UFCFA3-30-1
Principles of Computing – UFCFA3-30-1
Data Structures Using C++ 2E
Applications of Arrays
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 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”

Search

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

Selection Sort

Sequential Ordered 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 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

Binary Search

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

Members of the class Vector

Methods of the class 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

Vector StringList: After Adding Four Strings

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: Election Results

Additional String Methods

Effects of some String Methods

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

Programming Example: Pig Latin Strings (Sample Runs)

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