Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.

Slides:



Advertisements
Similar presentations
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Advertisements

Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Advanced Array Manipulation
Repetition Control Structures School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 9, Friday 3/07/2003)
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Programming Logic and Design, Third Edition Comprehensive
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Chapter 3: The Efficiency of Algorithms
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
Chapter 3: The Efficiency of Algorithms
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will –Learn about arrays One-dimensional arrays Two-dimensional arrays –Learn about searching.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 8 Arrays and Strings
Programming Logic and Design Fifth Edition, Comprehensive
Searching and Sorting Chapter Sorting Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
Array Processing.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
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,
CSC 211 Data Structures Lecture 13
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
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.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
Master File Update Processing. Objectives On completing this section you should be able to: w Distinguish between online processing and batch processing.
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Sequential Processing to Update a File Please use speaker notes for additional information!
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Chapter 5: Arrays: Lists and Tables
Chapter 3: The Efficiency of Algorithms
Searching and Sorting Arrays
Searching an Array or Table
Searching and Sorting Arrays
CHAPTER 9 SORTING & SEARCHING.
Presentation transcript:

Array Applications

Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design an algorithm that searches a table using a binary search.

Objectives (Continued) Design an algorithm that makes use of one or more program switches. Design an algorithm that sorts values in a table.

We Have Seen How To... organize collections of similar data items into a group instead of referring to them each as unique, individual items. use subscript notation to refer to specific elements of a group. design algorithms to process sets of values (smallest, average, etc.).

Table Lookups Loading a table - reading a table (multi- dimensional array) into computer storage. Table load usually occurs in the initialization module. Once loaded, the table can be referred to again and again during processing. One example might be a table of wage classes and pay rates.

Table Lookup (Continued) Input record contains employee pay class and hours worked. Look for wage class in table to find pay rate for employee pay class. This process of searching for a value in a table is called table lookup.

Table Lookup Example Input a two-dimensional table of products and prices. Then, input records will contain an item number and quantity. If item found, compute price. If not, print error message.

Solution Structure chart: OVERALL CONTROL A000 PROCESS TABLE LOOKUP B010 INPUT REFRENCE TABLE B000

Overall Control (A000) Start Input reference table (B000) READ NUMBER, QUANT IF NUMBER = THEN Write ‘No data for me to look up!’ ELSE DOWHILE NUMBER <> Process table lookup (B010) READ NUMBER, QUANT ENDDO WRITE ‘End of job’ ENDIF Stop

Input Reference Table B000 Enter ROW = 1 EOF = 0 DOUNTIL EOF = 1 READ ITEM#/PRICE(ROW,1), ITEM#/PRICE(ROW,2) IF ITEM#/PRICE(ROW,1) = THEN NCNT = ROW - 1 EOF = 1 ELSE IF ROW = 200 THEN NCNT = 200 EOF = 1 ELSE ROW = ROW + 1 ENDIF ENDDO Return

Item#/Price Table After input, the table looks something like this: Item#/Price Table COLUMNS Rows ……… 199unusedunused 200unusedunused

Table Lookup (Continued) DOUNTIL in the Input Reference Table module (B000) controls loading of the table. Note the use of nested IFTHENELSE structures because we are not sure how many rows of data will be loaded. (We do not know ahead of time how many items we have.) The program allows for up to 200 items and their prices in the table. If less than 200 items are input, a trailer record with an item number of is used.

Table Lookup (Continued) If the 200th row is reached, the special value EOF is set to 1. Likewise, if we reach the trailer record before row 200, we subtract one from the row number (so the trailer record is not counted) and again set EOF to 1.

Program Switches In this case, the DOUNTIL loop is controlled by the variable named EOF. EOF is a program switch - a value set up to deal with special conditions that may arise during processing. Implementation varies depending on need. In this case, EOF = 0 (off) at start and then “turned on” (set to 1) when there are no more records to be loaded into the table.

Table Load Processing No matter how we terminate the table load (trailer record or 200 records, whichever comes first) the variable NCNT is set to the number of rows in the table before EOF is set. We can then use NCNT in subsequent processing of the table (such as searching).

Back to Overall Control After table is loaded, we return to the overall control module (A000) and begin processing input records for inquiries against our table. We use a trailer record again (item number of 99999) to indicate the end of the inquiries. We also make sure there really are inquiries to process.

Process Table Lookup B010 Enter ROW = 1 DONE = 0 DOWHILE DONE = 0 IF ITEM#/PRICE(ROW,1) = NUMBER THEN PRICE = ITEM#/PRICE(ROW,2) * QUANT WRITE NUMBER, QUANT, ITEM#/PRICE(ROW,2), PRICE DONE = 1 ELSE ROW = ROW + 1 IF ROW > NCNT THEN WRITE ‘No data available for’, NUMBER DONE = 1 (ELSE) ENDIF ENDDO Return

Process Table Lookup Main processing is controlled by a DOWHILE loop. One of two conditions will stop execution of the loop (by changing the switch DONE to 1): –Input item number matches ITEM#/PRICE(ROW,1). –The end of the table is reached (NCNT rows processed) without a match.

Process Table Lookup (Continued) Nested IFTHENELSE control structures test both conditions (table entry matches input, or we have processed NCNT rows without a match). If a match is found, information is printed and DONE is set to 1 (to stop the loop). If no match is found, test for number of rows processed (ROW > NCNT).

Process Table Lookup (Continued) If the item number in (ROW,1) does not match the input, we increment ROW by 1, then check its value against NCNT. If ROW > NCNT we did not find a match and a message is written to that effect. DONE is set to 1 to stop the loop. Otherwise, we go on to the next row.

Binary Searches We need a more sophisticated search routine when: –frequency of use of table entries is evenly distributed over the table. –the number of entries in the table is very large. –processing efficiency or high system performance is necessary. Binary search is commonly employed for these situations.

Binary Search (Continued) In order for binary search to work correctly, the values in the table must be in ascending or descending order by one or more fields common to all records. This field is called a key field. Binary refers to the fact that the effective size of the table is halved repeatedly. Search begins at or near middle of table.

Binary Search (Continued) If not a match, search continues at or near the middle of the upper or lower half of the table which would contain the item. Other half of table is ignored. Process continues, halving the size of the table remaining. Eventually we either find a match or we cannot halve the table any more.

Binary Search Example A company has 70 sales offices, each identified by a unique two-digit code. However, when reports are produced the sales office location is to be used instead of its code. Office codes and locations are input to the program in ascending order and stored in separate lists. (That is, we load two separate lists.) These lists are then used to locate an office code which is also input (for report processing).

Binary Search Solution OVERALL CONTROL A000 PROCESS INPUT B020 SEARCH TABLE B010 INPUT TABLE B000 Structure Chart:

Overall Control A000 Start Input Table (B000) IF VALID-TABLE = “YES” THEN Read INCODE IF INCODE = 00 THEN WRITE “No codes to process” ELSE DOWHILE INCODE <> 0 Search Table (B010) Process a detail record (B020) Read INCODE ENDDO ENDIF …

Overall Control (Continued) ELSE Write ‘Table not in sequence.’ ENDIF Stop

Input Table (B000) B000 Enter READ CODE(1), LOCATION(1) SUB = 1 VALID-TABLE = “YES” DOWHILE SUB < 70 SUB = SUB + 1 READ CODE(SUB), LOCATION(SUB) IF CODE(SUB-1) > CODE(SUB) THEN VALID-TABLE = “NO” SUB = 70 (ELSE) ENDIF ENDDO Return

Search Table (B010) B010 Enter FOUND = “NO” FIRST = 1 LAST = 70 DOWHILE FIRST < LAST AND FOUND = “NO” MID = INT((FIRST + LAST) / 2) IF INCODE = CODE(MID) THEN FOUND = “YES” SAVECODE = CODE(MID) SAVELOCATION = LOCATION(MID) ELSE …

Search Table (B010) (Continued) IF INCODE < CODE(MID) LAST = MID – 1 ELSE FIRST = MID + 1 ENDIF ENDDO Return

Sorting Lists In the binary search example we checked the input for the table to make sure the input values were in order. We could have input them in any order and then used a sort algorithm to put the table entries in order. Before we look at sorting, though, we need to look at exchanging two values in the computer’s memory - because that’s what sorting algorithms do.

Exchanging Values Exchanging Values Incorrectly: 5353 NUM1 NUM2 Step 1.NUM1 = NUM2 2.NUM2 = NUM1

Exchanging Values (Incorrectly) Step 1. NUM1 NUM Step 2. NUM1 NUM

Exchanging Values Without Arrays Step 1. TEMP = NUM1 2. NUM1 = NUM2 3. NUM2 = TEMP (Illustrated on page 266 and on board)

Exchanging Values With Arrays LIST TEMP Step 1.TEMP = LIST(4) 2.LIST(4) = LIST(3) 3.LIST(3) = TEMP See illustration on page 267

Sort Example Problem: Sort a list of four values into ascending order, and output the sorted list. Solution: Read the items into a group (list). The value in position 1 of the list is compared to the value in position 2. If the first value is larger than the second one, they are interchanged. Then positions 3, 4, …, n are compared to the first value and exchanged if necessary. At this point the first pass is complete and position 1 contains the smallest value.

Sort Example (Continued) We begin with the second position and compare positions 3, 4, …, n to position 2. This is the second pass. At the end of the second pass, position 2 is known to contain the second-smallest value. Continue with this method until we have completed n-1 passes. At that point the list will be in sorted order. Structure chart follows...

Structure Chart for Sort OVERALL CONTROL A000 INPUT ARRAY B000 SORT ARRAY B010 OUTPUT ARRAY B020 EXCHANGE VALUES C000

Overall Control A000 Start Input Array (B000) Sort Array (B010) Output Array (C000) Stop

Sort Array (B010) B010 Enter TOT = 4 PASS = 1 DOUNTIL PASS = TOT SUB = PASS + 1 DOUNTIL SUB > TOT IF LIST(PASS) > LIST(SUB) THEN Exchange Values (C000) (ELSE) ENDIF SUB = SUB + 1 ENDDO PASS = PASS + 1 ENDDO Return

Exchange Values (C000) C000 Enter TEMP = LIST(SUB) LIST(SUB) = LIST(PASS) LIST(PASS) = TEMP Return

Sort Example This example uses four values, but with slight modification could be used to sort more than four values. This is not the only algorithm for sorting - there are in fact many ways to sort lists of numbers, some of which are more efficient than others. It is almost a certainty that you will see more sort problems if you take more programming classes!

Assignment 5 Chapter 11 Page 272 Exercise 12