An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 12 Manipulating Larger Quantities of Data.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Understanding the Need for Sorting Records
Chapter 9: Advanced Array Manipulation
Programming Logic and Design Sixth Edition
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
Advanced Topics Object-Oriented Programming Using C++ Second Edition 13.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Chapter 9: Arrays and Strings
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Programming Logic and Design Fourth Edition, Comprehensive
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 9: Advanced Array Concepts
Lists in Python.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
Chapter 8 Arrays and Strings
Chapter 19: Searching and Sorting Algorithms
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Searching and Sorting Chapter Sorting Arrays.
Array Processing.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 6: Arrays: Lists and Tables
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or.
Data Structure and Algorithms
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chapter 7.
Chapter 9: Sorting and Searching Arrays
An Introduction to Programming with C++ Sixth Edition
Arrays 2.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 5: Arrays: Lists and Tables
Starting Out with Programming Logic & Design
Programming Logic and Design Fifth Edition, Comprehensive
Presentation transcript:

An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 12 Manipulating Larger Quantities of Data

Objectives In this chapter, you will learn about: The need for sorting data The bubble sort algorithm Sorting data in parallel arrays Sorting objects The insertion sort algorithm Multidimensional arrays Indexed files and linked lists 2An Object-Oriented Approach to Programming Logic and Design

Understanding the Need for Sorting Data Data record storage – Always in some sort of order – One record first, another second, and so on Sequential order – Records arranged one after another based on value of a particular field Records stored in random order – Still in some order – Probably not desirable for processing or viewing 3An Object-Oriented Approach to Programming Logic and Design

Understanding the Need for Sorting Records (cont’d) Sorting – Placing data in order – Sort is based on contents of one or more fields Types of sorting – Ascending order Arranging records from lowest to highest value – Descending order Arranging records from highest to lowest value 4An Object-Oriented Approach to Programming Logic and Design

Understanding the Need for Sorting Data (cont’d) Examples of situations in which sorting is needed – Student records In ascending order by student ID number Goal: descending order by credit hours – Customer records In ascending order by customer number Goal: descending order by amount owed – Salesperson records In alphabetical order by last name Goal: sort based on annual sales amount 5An Object-Oriented Approach to Programming Logic and Design

Understanding the Need for Sorting Data (cont’d) Computers sort data by comparing numeric values Alphabetic sorts (numeric sorts) Everything stored in computer as zeros and ones – In every popular computing coding scheme “B” numerically one greater than “A” “y” numerically one less than “z – “A” greater or smaller than “a”? Ultimately computer-dependent Use input data with consistent capitalization Convert data to consistent capitalization before sort 6An Object-Oriented Approach to Programming Logic and Design

Understanding the Need for Sorting Data (cont’d) Writing sort program – Not likely needed Can purchase prewritten or “canned” sorting programs Database software provides built-in sort methods Benefit of understanding sorting process – Provides ability to write special-purpose sort – Improves array manipulation skills 7An Object-Oriented Approach to Programming Logic and Design

Using the Bubble Sort Algorithm Simplest sorting technique to understand Possible record arrangements – Ascending or descending order List items compared in pairs – Encounter out of order item Swap value with item below Ascending bubble sort – After each adjacent item pair compared once Largest item “sinks” to bottom Smallest item “rises” to top Sometimes called “sinking sort” 8An Object-Oriented Approach to Programming Logic and Design

Using the Bubble Sort Algorithm (cont’d) Algorithm – List of instructions that accomplish a task – Bubble Sort: example of a sorting algorithm Swapping two values – Central to most sorting techniques – Reverse positions Set first variable equal to value of the second Set second variable equal to value of the first Temporary variable created – Holds copy of one value to prevent data loss 9An Object-Oriented Approach to Programming Logic and Design

Using the Bubble Sort Algorithm (cont’d) 10An Object-Oriented Approach to Programming Logic and Design Program segment that swaps two values Figure 12-1

Figure An Object-Oriented Approach to Programming Logic and Design Declares constant to hold array’s size Declares an array to hold five scores main() method calls three methods o Input scores o Sort array o Display sorted test scores Sorts five student test scores in ascending order

Figure An Object-Oriented Approach to Programming Logic and Design Figure 12-3 illustrates fillArray() method – Receives parameters for array and its size – Subscript initialized to zero – Each array element filled – Returns control to main program after five scores are stored in array

Figure 12-4 Figure 12-4 illustrates an incomplete sortArray() method – Sorts array elements by making series of comparisons of adjacent element values – Swaps as necessary using swap() method – Uses constant COMPS for loop control 13An Object-Oriented Approach to Programming Logic and Design

Using the Bubble Sort Algorithm (cont’d) 14An Object-Oriented Approach to Programming Logic and Design Illustrates swap() method Swaps any two adjacent elements in the score array Figure 12-5

Figure An Object-Oriented Approach to Programming Logic and Design Using the Bubble Sort Algorithm (cont’d) Figure 12-6 illustrates steps accomplished in the first loop of the bubble Sort

16An Object-Oriented Approach to Programming Logic and Design Figure 12-7 illustrates pseudocode and flowchart for complete sortArray() logic Figure 12-7

Using a Bubble Sort (cont’d) General Rules for making comparisons with bubble sort – Greatest number of pair comparisons is one less than array size (use inner loop) – Number of times to process the list is one less than array size (use outer loop) – Example: To sort a 10-element array, make nine comparisons on each of nine rotations through the loop, executing the score comparison statement 81 times 17An Object-Oriented Approach to Programming Logic and Design

Figure An Object-Oriented Approach to Programming Logic and Design Figure 12-8 illustrates displayArray() method which displays the sorted array contents

Sorting a List of Variable Size Sorted list size may vary – Array does not need to be full – Count the number of scores as they are inputted into the array Store count value in variable – fillArray() method passes number of scores entered back to calling program – Methods manipulate only the array elements the user entered 19An Object-Oriented Approach to Programming Logic and Design

20 Figure 12-9 illustrates tracking number of elements stored in array Figure 12-9

An Object-Oriented Approach to Programming Logic and Design21 sortArray() method in SortScores2 class Figure 12-9 (cont’d)

An Object-Oriented Approach to Programming Logic and Design22 swap() method SortScores2 class Figure 12-9 (cont’d)

23An Object-Oriented Approach to Programming Logic and Design Pseudocode for SortScores2 class Figure 12-9 (cont’d)

Reduce Unnecessary Comparisons – Stop pair comparisons One element sooner on each pass through array Avoids comparing already-in-place values – Figure Creates new variable: pairsToCompare After each pass through list, variable reduced by 1 24An Object-Oriented Approach to Programming Logic and Design Figure 12-10

Refining the Bubble Sort to Reduce Unnecessary Passes Improve bubble sort – Reduce number of passes through the array Many passes required if array elements are badly out of order Few passes may be required if elements are nearly in order Once elements are in order, no swaps occur in subsequent passes – Possible remedy: Add flag variable Set to “continue” if any element pairs swapped Set to “finished” if no swaps made “Finished” indicates all elements correctly sorted 25An Object-Oriented Approach to Programming Logic and Design

26 Figure illustrates a method that sorts scores and uses a switchOccurred flag Figure 12-11

Sorting Data Stored in Parallel Arrays Data in parallel arrays contain related data in the same relative position in each array As one array is sorted, related data in other arrays must be moved Figure illustrates parallel arrays containing student names and test scores 27An Object-Oriented Approach to Programming Logic and Design Figure 12-12

Figure An Object-Oriented Approach to Programming Logic and Design Sorting Data Stored in Parallel Arrays (cont’d) Figure shows the swap() module for a program that sorts name array values and moves score array values accordingly

Sorting Objects One approach to sorting arrays of objects – Make comparisons on an attribute of the objects Use the method in the class that “gets” that attribute 29An Object-Oriented Approach to Programming Logic and Design – Figure shows a Student class that contains getIdNumber() method – Comparison determines whether two adjacent elements are out of order stuArray[x].getIdNumber() > stuArray[x+1].getIdNumber() Figure 12-14

Sorting Objects (cont’d) Second approach to sorting arrays of objects 30An Object-Oriented Approach to Programming Logic and Design – Comparison determines whether two adjacent elements are out of order: stuArray[x].isGreater(stuArray[x+1] – Method can be used in any sorting algorithm that works with simple numeric values –Add a method to the class similar to isGreater() method shown in Figure Figure 12-15

Using the Insertion Sort Algorithm Look at each list element one at a time If element is out of order relative to any items earlier in the list – Move each earlier item down one position – Insert test element Similar to technique used to sort objects manually 31An Object-Oriented Approach to Programming Logic and Design

Figure shows pseudocode and flowchart for insertionSort() method – Performs an ascending insertion sort using a five- element array named score 32An Object-Oriented Approach to Programming Logic and Design Figure 12-16

Using the Insertion Sort Algorithm (cont’d) Figure illustrates steps used in the insertion sort 33An Object-Oriented Approach to Programming Logic and Design Figure 12-17

Using Multidimensional Arrays Arrays – Series or list of values – Have same name and data type – Values referenced by subscript (index) One-dimensional (single-dimensional) array – Elements accessed using single subscript 34An Object-Oriented Approach to Programming Logic and Design

Using Multidimensional Arrays (cont’d) Table 12-1 shows rent amounts for apartments on different floors (0 – basement) Can locate value in an array with one subscript 35An Object-Oriented Approach to Programming Logic and Design

Using Multidimensional Arrays (cont’d) Two-dimensional arrays – Used to represent values in a table or a grid – Contains two dimensions: height and width Table 12-2 – Rent schedule based on floor and number of bedrooms An Object-Oriented Approach to Programming Logic and Design36

Using Multidimensional Arrays (cont’d) 37An Object-Oriented Approach to Programming Logic and Design Figure – Shows how the one-dimensional and two-dimensional rent arrays might appear in computer memory Figure 12-18

Using Multidimensional Arrays (cont’d) Declaring arrays – One-dimensional Single set of square brackets follows array type and name – Two-dimensional Two sets of square brackets follows array type and name First set of brackets denotes number of rows, second set denotes number of columns 38An Object-Oriented Approach to Programming Logic and Design

Figure An Object-Oriented Approach to Programming Logic and Design Figure shows the pseudocode and flowchart for a program that displays rent for apartments based on floor location and number of bedrooms

Figure Using Multidimensional Arrays (cont’d) Three-dimensional arrays – Uses three subscripts to access value Row Column Page 40An Object-Oriented Approach to Programming Logic and Design Shows rental fees for multiple multi-story apartment buildings according to number of bedrooms

Using Indexed Files and Linked Lists Sorting large numbers of data records takes considerable time and computer memory Physical order – “Real” order of storage – Example: index cards with friends’ names in a stack Logical order – A virtual order based on any criterion – Usually more efficient than physical order to store and access records 41An Object-Oriented Approach to Programming Logic and Design

Using Indexed Files Index – Involves identifying a key field for each record Key field has contents that make the record unique Examples: product number or employee number – Store a list of key fields paired with address of corresponding data record Use index to find records in order based on their addresses – Allows use of a random-access storage device Each record can be placed in any physical location on disk Example: Figure An Object-Oriented Approach to Programming Logic and Design

Figure Using Indexed Files (cont’d) 43An Object-Oriented Approach to Programming Logic and Design Figure stores an index on a portion of a disk Address in index refers to other locations on disk Removing a record from an indexed file – Record does not have to be physically removed – Its reference can be deleted from the index

Using Linked Lists Linked list – Structure in memory that has one extra data field in each record – Field contains memory address of next item in list Add new record to a linked list – Search through list for correct logical location of new record 44An Object-Oriented Approach to Programming Logic and Design Each record contains a nextCustAddress field

Using Linked Lists (cont’d) Steps to add new customer to the list in Table 12-3 (Name: Newberg and ID number: 245) – Create variable named currentAddress and store the address of the first record in the list: (0000) – Compare new customer’s ID: 245 with current record’s ID – Since 245 > 111, save the first customer’s address in a variable named saveAddress – Then store the address of the next logical customer (7200) in the currentAddress variable 45An Object-Oriented Approach to Programming Logic and Design

Using Linked Lists (cont’d) Steps to add new customer to the list in Table 12-3 (cont’d) (Name: Newberg and ID number: 245) – Examine second customer record (one at address 7200) – Compare Newberg’s ID: 245 with ID stored at currentAddress : 222 – Value 245 is higher, so save the current address: (7200), in saveAddress, and store its nextCustAddress field: (4400) in the currentAddress variable 46An Object-Oriented Approach to Programming Logic and Design

Using Linked Lists (cont’d) Steps to add new customer to the list in Table 12-3 (cont’d) (Name: Newberg and ID number: 245) – Compare Newberg’s ID: 245 with 333, which is the ID at currentAddress (4400) – Value 245 is lower, so customer 245 should logically precede customer 333 – Set nextCustAddress field in Newberg’s record to (4400), which is the address of customer 333 – Set nextCustAddress field of the record located at saveAddress to Newberg’s address (8400) 47An Object-Oriented Approach to Programming Logic and Design

Using Linked Lists (cont’d) 48An Object-Oriented Approach to Programming Logic and Design To remove the record of customer 333 from linked list – Change Newberg’s nextCustAddress field to the value in Silver’s nextCustAddress field More sophisticated linked lists – Store two additional fields One stores address of next record; the other, the previous record Allows list to be accessed either forward or backward Shows table updated with new customer

Summary Data records sorted based on field contents – Ascending or descending order Bubble sort – List items compared in pairs Swap with item below – Ascending sort: largest items “sink” to bottom Improve bubble sort – Eliminate unnecessary comparisons – Eliminate unnecessary passes 49An Object-Oriented Approach to Programming Logic and Design

Summary (cont’d) Two-dimensional arrays – Have rows and columns of values – Two subscripts are used to access an element Many languages support arrays with even more dimensions Index or linked list – Access data records in a logical order that differs from physical order – Index uses a key field and physical address – Linked list uses an extra field containing next record’s address 50An Object-Oriented Approach to Programming Logic and Design