Programming Logic and Design Fourth Edition, Comprehensive

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

Understanding the Need for Sorting Records
Chapter 9: Advanced Array Manipulation
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)
Sorting. Sorting Considerations We consider sorting a list of records, either into ascending or descending order, based upon the value of some field of.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
COPYRIGHT 2003: Dr. David Scanlan, CSUS OBJECTIVES: Explain the need for arrays. Coding an array. Basic algorithms: Largest, Smallest, Sum, Standard Deviation,
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
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.
Chapter 9: Arrays and Strings
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
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
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
Chapter 16: Searching, Sorting, and the vector Type.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
1 Arrays 2: Sorting and Searching Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 8 Arrays and Strings
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
Searching and Sorting Chapter Sorting Arrays.
Array Processing.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 12 Manipulating Larger Quantities of Data.
Chapter 6: Arrays: Lists and Tables
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
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.
3 – SIMPLE SORTING ALGORITHMS
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
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.
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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 16: Searching, Sorting, and the vector Type.
Chapter 9: Sorting and Searching Arrays
An Introduction to Programming with C++ Sixth Edition
Arrays 2.
Microsoft Visual Basic 2005: Reloaded Second Edition
Programming Logic and Design Fourth Edition, Comprehensive
Presentation transcript:

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

Objectives Describe the need for sorting data Swap two values in computer memory Use a bubble sort Use an insertion sort Use a selection sort Programming Logic and Design, Fourth Edition, Comprehensive

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

Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field (e.g., SSN, employee ID) Random order: records are in the order in which they were added Sorting: placing the records in order, based on the values in one or more fields Ascending order: arranged from lowest to highest Descending order: arranged from highest to lowest Programming Logic and Design, Fourth Edition, Comprehensive

Understanding the Need for Sorting Records (continued) Median value: the middle item when values are listed in order Mean value: arithmetic average Computer always sorts based on numeric values Character data is sorted by its numeric code value “A” is less than “B” Whether “A” is greater than “a” is system-dependent Programming Logic and Design, Fourth Edition, Comprehensive

Understanding How to Swap Two Values Swapping two values: reversing their position Use a temporary variable to hold one value Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort Bubble sort: Items in a list are compared in pairs If an item is out of order, it swaps places with the item below it In an ascending sort, after a complete pass through the list, largest item has “sunk” to the bottom and smallest item has “bubbled” to the top Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) Developing the application: Mainline logic Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) Developing the application: housekeeping() module Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) Developing the application: switchValues() module Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) Developing the application: When to call switchValues() module Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) Start with x = 0, compare first pair and swap With x = 1, compare next pair and swap Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) With x = 2, compare next pair, but no swap needed With x = 3, compare last pair, and swap Largest value has “sunk” to the bottom Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Using a Bubble Sort (continued) Use nested loops for sorting an array Inner loop makes the pair comparisons Greatest number of comparisons is one less than the number of array elements Outer loop controls the number of times to process the list One less than the number of array elements Programming Logic and Design, Fourth Edition, Comprehensive

Refining the Bubble Sort by Using a Constant for the Array Size Store the number of array elements in a constant Makes code more readable If number of elements changes later, only one change to the program is needed Programming Logic and Design, Fourth Edition, Comprehensive

Refining the Bubble Sort by Using a Constant for Array Size (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Refining the Bubble Sort by Using a Constant for Array Size (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Sorting a List of Variable Size Use a variable to hold the number of array elements Declare the array with a large fixed size Count the number of elements when the file is read into the array Use the count to control the loops when sorting, ignoring unused slots in the array Programming Logic and Design, Fourth Edition, Comprehensive

Sorting a List of Variable Size (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Sorting a List of Variable Size (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Refining the Bubble Sort by Reducing Unnecessary Comparisons After the first pass through the array: Largest item must be at the end of array Second largest item must be at the second-to-last position in the array Not necessary to compare those two values again On each subsequent pass through the array, stop the pair comparisons one element sooner Programming Logic and Design, Fourth Edition, Comprehensive

Refining the Bubble Sort by Reducing Unnecessary Comparisons (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Refining the Bubble Sort by Eliminating Unnecessary Passes Need one fewer pass than the number of elements to completely sort the array If array is somewhat ordered already, can reduce the number of passes Use a flag variable to indicate if there were any swaps during a single pass If no swaps, the array is completely sorted Programming Logic and Design, Fourth Edition, Comprehensive

Refining the Bubble Sort by Eliminating Unnecessary Passes (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Using an Insertion Sort Bubble sort is one of the least efficient sorting methods Insertion sort usually requires fewer comparisons Technique: Compare a pair of elements If an element is smaller than the previous one, search the array backward from that point and insert this element at the proper location Programming Logic and Design, Fourth Edition, Comprehensive

Using an Insertion Sort (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Using an Insertion Sort (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Using a Selection Sort Selection sort: uses two variables to store the smallest value and its position in the array Technique: Store first element value and its position in variables Compare value of first element to value of next element If next element is smaller, put its value and position in the variables Continue until end of array, at which time the smallest value and its position are in the variables Swap the first element value and position with the element and position stored in the variables Programming Logic and Design, Fourth Edition, Comprehensive

Using a Selection Sort (continued) Technique (continued): Start at the second element in the array and repeat the process Continue until all elements except the last have been designated as the starting point After making one less pass than the number of elements, the array is sorted Programming Logic and Design, Fourth Edition, Comprehensive

Using a Selection Sort (continued) Programming Logic and Design, Fourth Edition, Comprehensive

Using Indexed Files When a large data file with thousands of records needs to be accessed in sorted order, usually only one field determines the sorting Key field: the field whose contents make the record unique Indexing records: create a list of key fields paired with their corresponding positions in the file Sorting the indexes is faster than physically sorting the actual records Random-access storage device: disk or other device from which records can be accessed in any order Programming Logic and Design, Fourth Edition, Comprehensive

Using Indexed Files (continued) Address: location within computer memory or storage Every data record on disk has an address Index: holds physical addresses and key field values Data file is in physical order, but index is sorted in logical order When a record is removed from an indexed file, does not have to be physically removed, just deleted from the index file Programming Logic and Design, Fourth Edition, Comprehensive

Using Linked Lists Linked list: requires one extra field in every record to hold the physical address of the next logical record Programming Logic and Design, Fourth Edition, Comprehensive

Using Linked Lists (continued) To add a new record: Search the linked list for the correct logical location Break the current link, and insert the new record by linking the previous record to the new record and linking the new record to the next record Programming Logic and Design, Fourth Edition, Comprehensive

Using Linked Lists (continued) Two-way linked list: Two fields are added to the data record One holds address of previous logical record One holds address of next logical record List can be accessed in either a forward or backward direction Programming Logic and Design, Fourth Edition, Comprehensive

Using Multidimensional Arrays Single-dimensional (or one-dimensional) array: represents a single list of values Multidimensional array: A list with two or more related values in each position Two-dimensional array: Represents values in a table or grid containing rows and columns Requires two subscripts Programming Logic and Design, Fourth Edition, Comprehensive

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

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

Summary Sort data records in ascending or descending order based on the contents of one or more fields Swap two values by creating a temporary variable to hold one of the values Bubble sort compares pairs and swaps pairs to obtain the desired order Eliminate unnecessary comparisons in each pass and eliminate unnecessary passes to improve bubble sort Insertion sort compares pairs, and moves the out-of-place element to its proper place within the part of the array already processed Programming Logic and Design, Fourth Edition, Comprehensive

Summary (continued) Ascending selection sort uses two variables to hold the smallest value and its position in the array Index can be used to access records in logical order without physically sorting the data file Linked lists use an extra field to hold the address of the next record in the logical order Programming Logic and Design, Fourth Edition, Comprehensive