1 / 18

Ordered Array

Ordered Array. Definition. An array in which the data items are arranged in order of ascending key values —that is, with the smallest value at index 0, and each cell holding a value larger than the cell below.

ramona
Download Presentation

Ordered Array

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. Ordered Array

  2. Definition • An array in which the data items are arranged in order of ascending key values—that is, with the smallest value at index 0, and each cell holding a value larger than the cell below. • When we insert an item into this array, the correct location must be found for the insertion: just above a smaller value and just below a larger one. Then all the larger values must be moved up to make room. • Why would we want to arrange data in order? One advantage is that we can speed up search times dramatically using a binary search. • In the ordered array, if we’ve choose not to allow duplicates, this decision speeds up searching somewhat but slows down insertion.

  3. Linear Search • Linear searches operate in much the same way as the searches in the unordered array in the Array applet: The red arrow steps along, looking for a match. The difference is that in the ordered array, the search quits if an item with a larger key is found. • Deletion works much the same as it did in the Array applet, shifting items with higher index numbers down to fill in the hole left by the deletion. In the ordered array, however, the deletion algorithm can quit partway through if it doesn’t find the item, just as the search routine can.

  4. Binary Search • The payoff for using an ordered array comes when we use a binary search. This kind of search is much faster than a linear search, especially for large arrays.

  5. Binary Search (cont’) • If you used a linear search, guessing first 1, then 2, then 3, and so on, finding the number would take you, on the average, 50 guesses. In a binary search each guess divides the range of possible values in half, so the number of guesses required is far fewer.

  6. Binary Search with the find() Method public int find(long searchKey) { intlowerBound = 0; intupperBound = nElems-1; intcurIn; while(true) { curIn = (lowerBound + upperBound ) / 2; if(a[curIn]==searchKey) return curIn; // found it else if(lowerBound > upperBound) return nElems; // can’t find it else // divide range { if(a[curIn] < searchKey) lowerBound = curIn + 1; // it’s in upper half else upperBound = curIn - 1; // it’s in lower half } // end else divide range } // end while } // end find()

  7. Binary Search with the find() Method (cont’)

  8. Advantages of Ordered Arrays • What have we gained by using an ordered array? • Search times are much faster than in an unordered array. • Insertion takes longer because all the data items with a higher key value must be moved up to make room. • Deletions are slow in both ordered and unordered arrays because items must be moved down to fill the hole left by the deleted item. • Ordered arrays are therefore useful in situations in which searches are frequent, but insertions and deletions are not. An ordered array might be appropriate for a database of company employees, for example. Hiring new employees and laying off existing ones would probably be infrequent occurrences compared with accessing an existing employee’s record for information, or updating it to reflect changes in salary, address, and so on. • A retail store inventory, on the other hand, would not be a goodcandidate for an ordered array because the frequent insertions and deletions, as items arrived in the store and were sold, would run slowly.

  9. Storing Objects • In the Java examples so far, we’ve stored primitive variables of type long in our data structures. Storing such variables simplifies the program examples, but it’s not representative of how you use data storage structures in the real world. • Usually, the data items (records) you want to store are combinations of many fields. • For a personnel record, you would store last name, first name, age, Social Security number, and so forth. For a stamp collection, you would store the name of the country that issued the stamp, its catalog number, condition, current value, and so on.

  10. Comparing the Method

  11. Summary • Arrays in Java are objects, created with the new operator. • Unordered arrays offer fast insertion but slow searching and deletion. • Wrapping an array in a class protects the array from being inadvertently altered. • A class interface is composed of the methods (and occasionally fields) that the class user can access. • A class interface can be designed to make things simple for the class user. • A binary search can be applied to an ordered array. • Linear searches require time proportional to the number of items in an array. • Binary searches require time proportional to the logarithm of the number of items.

More Related