Chapter 10: Applications of Arrays and the class vector

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Repaso: Unidad 2 Lección 2
1 A B C
Simplifications of Context-Free Grammars
Variations of the Turing Machine
TK1924 Program Design & Problem Solving Session 2011/2012
AP STUDY SESSION 2.
1
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
David Burdett May 11, 2004 Package Binding for WS CDL.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Create an Application Title 1Y - Youth Chapter 5.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Services and Training Provider Details Chapter 4.
Add Governors Discretionary (1G) Grants Chapter 6.
CALENDAR.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
The 5S numbers game..
1.
Media-Monitoring Final Report April - May 2010 News.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Abstract Data Types and Algorithms
C++ Programming:. From Problem Analysis
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
Briana B. Morrison Adapted from William Collins
1 The Royal Doulton Company The Royal Doulton Company is an English company producing tableware and collectables, dating to Operating originally.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Biology 2 Plant Kingdom Identification Test Review.
Chapter 1: Expressions, Equations, & Inequalities
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
Before Between After.
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
Subtraction: Adding UP
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Types of selection structures
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
1 Titre de la diapositive SDMO Industries – Training Département MICS KERYS 09- MICS KERYS – WEBSITE.
Essential Cell Biology
Converting a Fraction to %
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CSE20 Lecture 15 Karnaugh Maps Professor CK Cheng CSE Dept. UC San Diego 1.
Clock will move after 1 minute
famous photographer Ara Guler famous photographer ARA GULER.
PSSA Preparation.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 16: Searching, Sorting, and the vector Type
Presentation transcript:

Chapter 10: Applications of Arrays and the class vector C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 10: Applications of Arrays and the class vector

Objectives In this chapter, you will: Explore how to sort an array using the bubble sort, selection sort, and insertion sort algorithms Learn how to implement the binary search algorithm Become familiar with the vector type C++ Programming: From Problem Analysis to Program Design, Fifth Edition

List Processing List: a collection of values of the same type Basic list operations: Search the list for a given item Sort the list Insert an item in the list Delete an item from the list Print the list C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Searching Sequential search algorithm C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Searching (cont'd.) List with 1000 elements Search item is the second item Sequential search makes two key comparisons Search item is the 900th item Sequential search makes 900 key comparisons Search item is not in the list Sequential search makes 1000 key comparisons C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Searching (cont'd.) Sequential search Not very efficient for large lists On average, number of key comparisons equal to half the size of the list Does not assume that the list is sorted C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Bubble Sort list[0]...list[n - 1] List of n elements, indexed 0 to n - 1 C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Bubble Sort (cont'd.) Series of n - 1 iterations Successive elements list[index] and list[index + 1] of list are compared If list[index] > list[index + 1] Elements list[index] and list[index + 1] are swapped Smaller elements move toward the top Larger elements move toward the bottom C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Bubble Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Bubble Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Bubble Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Bubble Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Bubble Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Bubble Sort (cont'd.) List of length n n = 1000 Exactly n(n - 1) / 2 key comparisons On average n(n - 1) / 4 item assignments n = 1000 500,000 key comparisons 250,000 item assignments C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Selection Sort Rearrange the list by selecting an element in the list and moving it to its proper position Finds the location of the smallest element in the unsorted portion of the list Moves it to the top of the unsorted portion of the list C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Selection Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Selection Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Selection Sort (cont'd.) In the unsorted portion of the list: Find the location of the smallest element Move the smallest element to the beginning of the unsorted list C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Selection Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Selection Sort (cont'd.) List of length n Exactly n(n - 1) / 2 key comparisons 3(n - 1) item assignments n = 1000 500,000 key comparisons 3000 item assignments C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Insertion Sort Sorts the list by moving each element to its proper place C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Insertion Sort (cont'd.) Consider the element list[4] First element of unsorted list list[4] < list[3] Move list[4] to proper location At list[2] C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Insertion Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Insertion Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Insertion Sort (cont'd.) During the sorting phase Array containing the list is divided into two sublists: sorted and unsorted C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Insertion Sort (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Insertion Sort (cont'd.) List of length n About (n2 + 3n – 4) / 4 key comparisons About n(n – 1) / 4 item assignments n = 1000 250,000 key comparisons 250,000 item assignments C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Binary Search Much faster than a sequential search List must be sorted “Divide and conquer” Compare search item with middle element Less than middle: search only upper half of list More than middle: search only lower half of list C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Binary Search (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Binary Search (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Binary Search (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Performance of Binary Search L is a sorted list of size 1024 Every iteration of the while loop cuts the size of the search list by half At most, 11 iterations to determine whether x is in L Binary search will make 22 comparisons at most L has1048576 elements Binary search makes 42 item comparisons at most C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Performance of Binary Search (cont'd.) List of length n Maximum number comparisons 2log2n + 2 C++ Programming: From Problem Analysis to Program Design, Fifth Edition

vector type (class) Only a fixed number of elements can be stored in an array Inserting and removing elements causes shifting vector type implements a list vector container vector vector object object C++ Programming: From Problem Analysis to Program Design, Fifth Edition

vector type (class) (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

vector type (class) (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

vector type (class) (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

vector type (class) (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Election Results Presidential election for the student council of your local university Write a program to analyze the data and report the winner Four major divisions labeled as Region 1, Region 2, Region 3, and Region 4 Each division has several department Each department manages its own voting process and directly reports the results to the election committee C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Election Results (cont'd.) Voting is reported in the following form: Desired output: C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Election Results (cont'd.) Assume that six candidates are running Program can be modified to accommodate any number of candidates Data is provided in two files: candData.txt consists of the names of candidates voteData.txt each line consists of voting results One entry per line C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Input and Output Input: Two files, one containing the candidates’ names and the other containing the voting data Output: election results in a tabular form and the winner C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Problem Analysis Program must organize the voting data by region Program must calculate total number of votes received by each candidate as well as the total votes cast in the election Names of the candidates must appear in alphabetical order C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Problem Analysis (cont'd.) Data type of a candidate’s name and number of votes are different Separate arrays Use a two-dimensional array to hold the next four columns of the output One-dimensional array to hold the total votes received by each candidate C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Problem Analysis (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Algorithm Design Read the candidates’ names into the array candidatesName Sort the array candidatesName Initialize the arrays votesByRegion and totalVotes Process the voting data Calculate the total votes received by each candidate Output the results C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Function getCandidatesName() C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Function sortCandidateName() C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Function initialize() C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Process Voting Data Get a candidateName, regionNumber, and numberOfVotesForTheCandidate Find the row number in the array candidatesName that corresponds to this candidate This gives the corresponding row number in the array votesByRegion for this candidate Find the column in the array votesByRegion that corresponds to the regionNumber C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Process Voting Data (cont'd.) Update the appropriate entry in the array votesByRegion by adding the numberOfVotesForTheCandidate C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Function binSearch() C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Function processVotes() C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Function addRegionsVote() C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Function printHeading() C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Function printResults() Initialize sumVotes, largestVotes, and winLoc to 0 For each row in each array: Output the final lines of output C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Main Algorithm Declare the variables Open the input file candData.txt If the input file does not exist, exit the program Read the data from the file candData.txt into the array candidatesName Sort the array candidatesName Close the file candData.txt and clear the input stream C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Main Algorithm (cont'd.) Open the input file voteData.txt If the input file does not exist, exit the program Initialize the arrays votesByRegion and totalVotes Process the voting data and store the results in the array votesByRegion C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Programming Example: Main Algorithm (cont'd.) Calculate the number of total votes received by each candidate and store the results in the array totalVotes Print the heading Print the results C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Summary List Sorting algorithms Binary search vector type Set of elements of the same type Sorting algorithms Bubble sort Selection sort Insertion sort Binary search Compare performance to sequential search vector type C++ Programming: From Problem Analysis to Program Design, Fifth Edition