C++ Programming:. From Problem Analysis

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 21: Graphs.
Advertisements

FIRST COURSE Outlook Tutorial 1 Communicating with Outlook 2007.
Chapter 6: User-Defined Functions I
Excel Tutorial 2 Formatting a Workbook
FIRST COURSE Excel Tutorial 3 Working with Formulas and Functions.
Excel Tutorial 1 Getting Started with Excel
E-Commerce: The Second Wave Fifth Annual Edition
Guide to Networking Essentials Fifth Edition
Chapter 10: Applications of Arrays and the class vector
CCNA Guide to Cisco Networking Fundamentals Fourth Edition
Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Chapter 12: Inheritance and Composition
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.
Microsoft Office 2007 Access Integration Feature Sharing Data Among Applications.
Concepts in Enterprise Resource Planning Fourth Edition Chapter One Business Functions and Business Processes.
Network+ Guide to Networks 6 th Edition Chapter 9 In-Depth TCP/IP Networking.
Solving Problems with Computers
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.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 19: Searching and Sorting Algorithms
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
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 McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
C++ for Engineers and Scientists Third Edition
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.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 8 Arrays and Strings
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Searching and Sorting Algorithms.
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: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 211 Data Structures Lecture 13
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
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.
CHAPTER 10 ARRAYS II Applications and Extensions.
Data Structures Using Java1 Chapter 9 Sorting Algorithms.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
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.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Chapter 16: Searching, Sorting, and the vector Type.
Java Programming: Guided Learning with Early Objects Chapter 8 Applications of Arrays (Sorting and Searching) and Strings.
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Arrays
Applications of Arrays
Presentation transcript:

C++ Programming:. From Problem Analysis C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type

Objectives In this chapter, you will: Learn how to implement the sequential search algorithm 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, Fourth Edition

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

Searching To search a list, you need: After the search is completed The list (array) containing the list List length Item to be found After the search is completed If found: Report “success” Location where the item was found If not found, report “failure” C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Sequential Search Sequential search: search a list for an item Compare search item with other elements until either: Item is found List has no more elements left Average number of comparisons made by the sequential search equals half the list size Good only for very short lists C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Sequential Search (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Bubble Sort Suppose list is a list of n elements In n-1 iterations compare elements list[index] and list[index + 1] If list[index] > list[index + 1], then swap them C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Bubble Sort (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Bubble Sort (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Bubble Sort (continued) For a list of length n, on average, a bubble sort makes n(n–1)/2 key comparisons C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Selection Sort Selection sort: rearrange list by selecting an element and moving it to its proper position Find the smallest (or largest) element and move it to the beginning (end) of the list C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Selection Sort (continued) On successive passes, locate the smallest item in the list starting from the next element C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Selection Sort (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Insertion Sort The insertion sort algorithm sorts the list by moving each element to its proper place C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Insertion Sort (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Insertion Sort (continued) Average key comparisons: (n2 + 3n – 4)/4 C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Sequential Search on an Ordered List On average, searches half the list C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Sequential Search on an Ordered List (continued) Search was unsuccessful C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Binary Search Binary search can be applied to sorted lists Uses the “divide and conquer” technique Compare search item to middle element If search item is less than middle element, restrict the search to the lower half of the list Otherwise search the upper half of the list C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Binary Search (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Binary Search (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Binary Search (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Performance of Binary Search Every iteration cuts size of search list in half If list L has 1000 items At most 11 iterations are needed to search for x Every iteration makes two key comparisons Binary search makes at most 22 key comparisons to determine if x is in L Sequential search makes 500 key comparisons (average) to determine if x is in L for the same size list C++ Programming: From Problem Analysis to Program Design, Fourth Edition

vector Type (class) C++ provides vector type to implement a list Variables declared with vector type are called: Vector container Vector Vector object Object Unlike arrays, vector size can increase and decrease during execution C++ Programming: From Problem Analysis to Program Design, Fourth Edition

vector Type (class) (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

vector Type (class) (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example: Election Results Student council of your local university will hold presidential election soon For reasons of confidentiality, election committee wants to computerize the voting The committee needs a program to analyze the data and report the winner The university has four major divisions and each division has several departments C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example: Election Results (continued) For the purpose of the election, the divisions are labeled as Region 1 - Region 4 Each department in each division manages its own voting process and directly reports the votes to the election committee The voting is reported in the following form: candidateName regionNumber numberOfVotesForTheCandidate C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example: Election Results (continued) The data are provided in two files One file has candidate names seeking the president’s post (unordered) Each line of second file consists of voting results in the following form: candidateName regionNumber numberOfVotesForThisCandidate C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example: Input and Output For example, assume the input file looks like: Mia 2 34 Mickey 1 56 Donald 2 56 Mia 1 78 Danny 4 29 Ashley 4 78 First line indicates that Mia received 34 votes from Region 2 Output consists of election results in tabular form as described and identifies the winner C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example: Problem Analysis Program must organize voting data by region Calculate total number of votes received by each candidate and total votes cast Candidate names must be alphabetized Data types Candidate name: string Number of votes: int C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example: Problem Analysis (continued) Need three parallel arrays C++ Programming: From Problem Analysis to Program Design, Fourth Edition

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

Programming Example: Process Voting Data For each entry in voteDat.txt: Get a candidateName, regionNumber, numberOfVotesForTheCandidate Find the row number in candidatesName corresponding to this candidate This gives the corresponding row number in the array votesByRegion for this candidate C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example: Process Voting Data (continued) For each entry in voteDat.txt (continued): Find the column in votesByRegion corresponding to the regionNumber Update the appropriate entry in votesByRegion by adding numberOfVotesForTheCandidate C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example: Function printResults Initialize sumVotes, largestVotes, winLoc to 0 For each row in each array if (largestVotes < tVotes[i]) { largestVotes = tVotes[i]; winLoc = i; } sumVotes = sumVotes + tVotes[i]; Output from corresponding rows of arrays Output the final lines of output C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Main Algorithm: Function main Declare the variables Open the input file candDat.txt If input file does not exist, exit program Read data from candDat.txt into the array candidatesName Sort the array candidatesName Close candDat.txt C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Main Algorithm: Function main (continued) Open the input file voteDat.txt If input file does not exist, exit program Initialize votesByRegion and totalVotes Process voting data and store results in votesByRegion Calculate total votes received by each candidate and store results in totalVotes Print the heading Print the results C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Summary List: set of elements of the same type List length: number of elements Sequential search algorithm: Search for an item, starting at first element Compare search item with other elements Stop when item is found, or list has no more elements left to be compared Searches half the list (average) Good only for very short lists C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Summary (continued) Bubble sort sorts by moving the largest elements toward the bottom Selection sort sorts by finding the smallest (or largest) element and moving it to the beginning (end) of the list Binary search is much faster than sequential search, but requires an ordered list C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Summary (continued) C++ provides vector type to implement lists Vector size can increase or decrease Vector object must specify the type of element the vector object stores First element in vector is at location 0 Vector class includes various functions C++ Programming: From Problem Analysis to Program Design, Fourth Edition