1 / 11

Tracing and Evaluating algorithms in java

Steven Fox – Chapter 2 Sections 2.1.4 & 2.1.5. Tracing and Evaluating algorithms in java. Chapter 2 Section 2.1.4. Trace algorithms in java. As an example, we will consider the useful process of maintaining a full index to a data file (or another array).

kevork
Download Presentation

Tracing and Evaluating algorithms in java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Steven Fox – Chapter 2 Sections 2.1.4 & 2.1.5 Tracing and Evaluating algorithms in java

  2. Chapter 2 Section 2.1.4 Trace algorithms in java

  3. As an example, we will consider the useful process of maintaining a full index to a data file (or another array). The Class BookIndex will be described on the next slide. Trace algorithm example specs.

  4. A Class has the following data members: Public class BookIndex { // instance variables or data members private String bookNum; // book reference number private int pos; // book details location in main file // This class also has accessor and mutator methods. } • A Class then declares an array of these objects: BookIndex[] bookIndexes = new BookIndex[500]; Bookindex class

  5. This structure is used in a library containing fiction and non-fiction books. There are several shelf units labeled A-H. Each shelf unit has four shelves in it. Structure

  6. The BOOKNUM field has the following structure: • The first character indicates a fiction or non-fiction book • The second character indicates the shelf unit letter (A to H) • The third indicates the number of the shelf (1 is bottom, 4 is top) • The last three numbers indicate a subject code • Example a book number: NG2023 • The POS field indicates where the rest of the details of the book are located in a direct access data file. • When a new book is added, an entry is made at the end of the data file. The book number is then inserted in the correct position in the array. Booknum field structure

  7. Sample Data A new book, with reference number FA2139 is added to the end of the data file at a position contained in an identifier newPos. This book now needs to be added to the array. Consider the following algorithm.

  8. private void insert(String bookNum, intnewPos) { inti = newPos; //pointer to the array // Shuffle up array entries from top end to entry position. // This will find the next smallest to bookNum (or first element). while ( (i>0) && (bookNum.compareTo(bookIndexes[i].getBookNum()) < 0)) { bookIndexes[i] = bookIndexes[i-1]; i = i-1; } //Check boundary position at i = 0 if (i != 0) { i = i + 1; //insert above smaller entry } bookIndexes[i] = new BookIndex(bookNum, newPos); } Insert Method

  9. Assuming that the array is as shown in the table and the next vacant location in the file is 7, let’s trace the insert method. Tracing the Insert Method At this point, the array looks like:

  10. The array looks like: and the value of i is 4. Therefore, we execute: if (i != 0) { i = i + 1; // insert above smaller entry } bookIndexes[i] = new BookIndex(bookNum, newPos); Thus, bookNum FA2139 and the value 7 are inserted into the correct position (5) in the array. Executing

  11. When you evaluate algorithms in Java, you are essentially tracing them with virtual data. An example is an algorithm that takes an array NAMES and sorts it so that names are at the front (Bob) and other values are at the end (XXXX). To evaluate this, take sample arrays with this type of data and run through the algorithm. After multiple traces, you have evaluated the algorithm. Evaluate algorithms in java

More Related