110 likes | 217 Views
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).
E N D
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). The Class BookIndex will be described on the next slide. Trace algorithm example specs.
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
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
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
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.
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
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:
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
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