Chapter 9: Advanced Array Manipulation

Slides:



Advertisements
Similar presentations
Understanding the Need for Sorting Records
Advertisements

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.
Programming Logic and Design, Second Edition, Comprehensive
Chapter 7: Control Breaks Programming Logic and Design, Third Edition Comprehensive.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
Programming Logic and Design, Third Edition Comprehensive
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Writing a Complete Program
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
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
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Programming Logic and Design, Third Edition Comprehensive
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Logic and Design Seventh Edition
Programming Logic and Design, Second Edition, Comprehensive
Chapter 9: Advanced Array Concepts
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 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 8 Arrays and Strings
Searching and Sorting Chapter Sorting Arrays.
Array Processing.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 12 Manipulating Larger Quantities of Data.
Chapter 6: Arrays: Lists and Tables
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
C++ for Engineers and Scientists Second Edition Chapter 11 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.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
11 Chapter 111 Sequential File Merging, Matching, and Updating Programming Logic and Design, Second Edition, Comprehensive 11.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
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.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9: Sorting and Searching Arrays
An Introduction to Programming with C++ Sixth Edition
Arrays 2.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
Programming Logic and Design Fourth Edition, Comprehensive
Starting Out with Programming Logic & Design
Writing a Complete Program
Programming Logic and Design Fifth Edition, Comprehensive
Presentation transcript:

Chapter 9: Advanced Array Manipulation Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Objectives After studying Chapter 9, you should be able to: Describe the need for sorting data Swap two values in computer memory Use a bubble sort Use an insertion sort Programming Logic and Design, Third Edition Comprehensive

Objectives (continued) Use a selection sort Use indexed files Use a linked list Use multidimensional arrays Programming Logic and Design, Third Edition Comprehensive

Understanding the Need for Sorting Records Sequential order: Records are arranged one after another on the basis of the value in some field Example: Employee records stored in numeric order by Social Security number or department number Employee records stored in alphabetic order by last name or department name The data records need to be sorted, or placed in order, based on the contents of one or more fields Programming Logic and Design, Third Edition Comprehensive

Understanding the Need for Sorting Records (continued) You can sort data in either: ascending order, arranging records from lowest to highest value within a field, or descending order, arranging records from highest to lowest value When computers sort data, they always use numeric values when making comparisons between values Programming Logic and Design, Third Edition Comprehensive

Understanding How to Swap Two Values Many sorting techniques have been developed A concept that is central to most sorting techniques involves swapping two values When you swap the values stored in two variables, you reverse their positions You set the first variable equal to the value of the second You set the second variable equal to the value of the first Programming Logic and Design, Third Edition Comprehensive

A Module that Swaps Two Values Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Using a Bubble Sort Bubble sort, the simplest sorting technique to understand, arranges records in either ascending or descending order Items in a list compare with each other in pairs when an item is out of order, it swaps values with the item below it Ascending bubble sort: after each adjacent pair of items in a list has been compared once, the largest item in the list will have “sunk” to the bottom Programming Logic and Design, Third Edition Comprehensive

Mainline Logic for the Score-Sorting Program Programming Logic and Design, Third Edition Comprehensive

The Housekeeping()Module for the Score-Sorting Program Programming Logic and Design, Third Edition Comprehensive

Refining the Bubble Sort by Using a Variable for the Array Size Keep in mind that when performing a bubble sort, you need to perform one fewer pair comparisons than you have elements You also pass through the list of elements one fewer times than you have elements In Figure 9-7, you sorted a five-element loop, so you performed the inner loop while x was less than 5 and the outer loop while y was less than 5 Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Refining the Bubble Sort by Using a Variable for the Array Size (continued) When performing a bubble sort on an array, you: compare two separate loop control variables with a value that equals the number of elements in the list If the number of elements in the array is stored in a variable named numberOfEls, the general logic for a bubble sort is shown in Figure 9-8 To use this logic, you must declare numberOfEls along with the other variables in the housekeeping() module Programming Logic and Design, Third Edition Comprehensive

Generic Bubble Sort Module Using a Variable for Number of Elements Programming Logic and Design, Third Edition Comprehensive

A Complete Score-Sorting Program that Prints the Sorted Scores Programming Logic and Design, Third Edition Comprehensive

Sorting a List of Variable Size In the score-sorting program in Figure 9-9, a numberOfEls variable was initialized to the number of elements to be sorted near the start of the program—within the housekeeping()module Sometimes, you don’t want to initialize the numberOfEls variable at the start of the program You might not know how many array elements there are Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive The Housekeeping()Module for a Score-Sorting Program that Accommodates a Variable-Size Input File Programming Logic and Design, Third Edition Comprehensive

Sorting a List of Variable Size (continued) Rather than initializing numberOfEls to a fixed value, you can: count the input scores, then give numberOfEls its value after you know how many scores exist When you count the input records and use the numberOfEls variable, it does not matter if there are not enough scores to fill the array However, it does matter if there are more scores than the array can hold Programming Logic and Design, Third Edition Comprehensive

Sorting a List of Variable Size (continued) Every array must have a finite size It is an error to try to store data past the end of the array Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Flowchart and Pseudocode for housekeeping()that Prevents Overextending the Array Programming Logic and Design, Third Edition Comprehensive

Refining the Bubble Sort by Reducing Unnecessary Comparisons As illustrated in Figure 9-8, when performing the sorting module for a bubble sort, you pass through a list, make comparisons and swap values if two values are out of order On each pass through the array, you can afford to stop your pair comparisons one element sooner Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Refining the Bubble Sort by Reducing Unnecessary Comparisons (continued) Thus, after the first pass through the list, there is no longer a need to check the bottom element After the second pass, there is no need to check the two bottom elements You can avoid comparing these already-in-place values by creating a new variable, pairsToCompare,and setting it equal to the value of numberOfEls – 1 Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Flowchart and Pseudocode for sortScores()Module Using pairsToCompare Variable Programming Logic and Design, Third Edition Comprehensive

Refining the Bubble Sort by Eliminating Unnecessary Passes (continued) Another improvement that could be made: reduce the number of passes through the array If array elements are so badly out of order that they are in reverse order, then it takes many passes through the list to place it in order it takes one fewer passes than the value in numberOfEls to complete all the comparisons and swaps needed to get the list in order Programming Logic and Design, Third Edition Comprehensive

Refining the Bubble Sort by Eliminating Unnecessary Passes (continued) The bubble sort module (Figure 9-12) would pass through the array list four times make four sets of pair comparisons It would always find that each score[x]is not greater than the corresponding score[x+1] no switches would ever be made Programming Logic and Design, Third Edition Comprehensive

Refining the Bubble Sort by Eliminating Unnecessary Passes (continued) The scores would end up in the proper order, but they were in the proper order in the first place therefore, a lot of time would be wasted Programming Logic and Design, Third Edition Comprehensive

Refining the Bubble Sort by Eliminating Unnecessary Passes (continued) Possible remedy: Add a flag variable that you set to a “continue” value on any pass through the list in which any pair of elements is swapped (even if just one pair that holds a different “finished” value when no swaps are made (all elements in the list are already in the correct order) Figure 9-13 illustrates a module that sorts scores and uses a switchOccurred flag Programming Logic and Design, Third Edition Comprehensive

Bubble Sort with switchOccurred Flag Programming Logic and Design, Third Edition Comprehensive

Using an Insertion Sort When using an insertion sort, you also look at each pair of elements in an array When an element is smaller than the one before it (for an ascending sort), this element is “out of order” As soon as you locate such an element, search the array backward from that point to see where an element smaller than the out-of-order element is located Programming Logic and Design, Third Edition Comprehensive

Using an Insertion Sort (continued) At this point, you open a new position for the out- of-order element by moving each subsequent element down one position inserting the out-of-order element into the newly opened position Programming Logic and Design, Third Edition Comprehensive

Sample Insertion Sort Module Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Using a Selection Sort Ascending selection sort: The first element in the array is assumed to be the smallest Its value is stored in a variable for example, smallest Its position in the array, 1, is stored in another variable for example, position Programming Logic and Design, Third Edition Comprehensive

Sample Selection Sort Module Programming Logic and Design, Third Edition Comprehensive

Using a Selection Sort (continued) When you index records, you store a list of key fields paired with the storage address for the corresponding data record When you use an index, you can store records on a random-access storage device, such as a disk, from which records can be accessed in any logical order Each record can be placed in any physical location on the disk, and you can use the index as you would use the index in the back of a book Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Using a Selection Sort As pages in a book have numbers, computer memory and storage locations have addresses Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Using Linked Lists Another way to access records in a desired order, even though they might not be physically stored in that order, is to create a linked list In its simplest form, creating a linked list involves creating one extra field in every record of stored data This extra field holds the physical address of the next logical record Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Linked Customer List Programming Logic and Design, Third Edition Comprehensive

Using Multidimensional Arrays An array that represents a single list of values is a single-dimensional array For example, an array that holds five rent figures that apply to five floors of a building can be displayed in a single column Programming Logic and Design, Third Edition Comprehensive

Using Multidimensional Arrays (continued) If you must represent values in a table or grid that contains rows and columns instead of a single list, then you might want to use a multidimensional array—specifically, a two- dimensional array Programming Logic and Design, Third Edition Comprehensive

Using Multidimensional Arrays (continued) Programming Logic and Design, Third Edition Comprehensive

Determining Rent Using No Array Programming Logic and Design, Third Edition Comprehensive

Using Multidimensional Arrays (continued) Some languages allow multidimensional arrays containing three levels, or three-dimensional arrays, in which you access array values using three subscripts Programming Logic and Design, Third Edition Comprehensive

Rent-Determining Program Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Summary When the sequential order of data records is not the order desired for processing or viewing, the data need to be sorted in ascending or descending order based on the contents of one or more fields You can swap two values by creating a temporary variable to hold one of the values In a bubble sort, items in a list are compared in pairs when an item is out of order, it swaps with the item below it Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Summary (continued) When performing a bubble sort on an array, you compare two separate loop control variables with a value that equals the number of elements in the list When using an insertion sort, you also look at each pair of elements in an array You can use an index to access data records in a logical order that differs from their physical order Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Summary (continued) Using an index involves identifying a key field for each record Creating a linked list involves creating an extra field within every record to hold the physical address of the next logical record You use a multidimensional array when locating a value in an array that depends on more than one variable Programming Logic and Design, Third Edition Comprehensive