Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Lesson 2.2 abstraction
1. Lesson 2.2 – Array Cont.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
2. The User’s Life Made Easier
In lowArray.java (Listing 2.2), the code in main() to search for an item
required eight lines; in highArray.java, it requires only one. The class
user, the HighArrayApp class, need not worry about index numbers
or any other array details. Amazingly, the class user doesn’t even
need to know what kind of data structure the HighArray class is using
to store the data. The structure is hidden behind the interface. In
fact, in the next section, we’ll see the same interface used with a
somewhat different data structure.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 2
3. Abstraction
The process of separating the how from the what—how an operation is
performed inside a class, as opposed to what’s visible to the class user—is called
abstraction. Abstraction is an important aspect of software engineering. By
abstracting class functionality, we make it easier to design a program because
we don’t need to think about implementation details at too early a stage in the
design process.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 3
4. ORDERED ARRAY
In the ordered array we’ve chosen not to allow duplicates.
As we saw earlier, this decision speeds up searching
somewhat but slows down insertion.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 4
5. Linear Search
Linear search is the default. 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.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 5
6. 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.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 6
7. The Guess-a-Number Game
Binary search uses the same approach you did as a kid (if you were smart) to
guess a number in the well-known children’s guessing game. In this game, a
friend asks you to guess a number she’s thinking of between 1 and 100. When
you guess a number, she’ll tell you one of three things: Your guess is larger than
the number she’s thinking of, it’s smaller, or you guessed correctly. To find the
number in the fewest guesses, you should always start by guessing 50. If your
friend says your guess is too low, you deduce the number is between 51 and
100, so your next guess should be 75 (halfway between 51 and 100). If she says
it’s too high, you deduce the number is between 1 and 49, so your next guess
should be 25.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 7
8. Guessing a Number Table
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
9. Java Code for an Ordered Array
Let’s examine some Java code that implements an ordered
array. We’ll use the OrdArray class to encapsulate the array
and its algorithms. The heart of this class is the find()
method, which uses a binary search to locate a specified
data item. We’ll examine this method in detail before
showing the complete program.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 9
10. Java Code for an ordered array
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
11. The method begins by setting the lowerBound and upperBound variables to the first and last
occupied cells in the array. Setting these variables specifies the range where the item we’re
looking for, searchKey, may be found. Then, within the while loop, the current index, curIn, is set
to the middle of this range.
If we’re lucky, curIn may already be pointing to the desired item, so we first check if this is true.
If it is, we’ve found the item, so we return with its index, curIn.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 11
12. The OrdArray Class
In general, the orderedArray.java program is similar to highArray.java (Listing 2.3). The main
difference is that find() uses a binary search, as we’ve seen.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 12
17. Advantages of Ordered Arrays
What have we gained by using an ordered array? The major advantage is that search times are
much faster than in an unordered array. The disadvantage is that 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.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 17
18. Example of Person class
In Java, a data record is usually
represented by a class object. Let’s
examine a typical class used for storing
personnel data. Here’s the code for the
Person class:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 18
19. The classDataArray.java Program
The program that makes use of the Person class is similar to the highArray.java program (Listing 2.3)
that stored items of type long. Only a few changes are necessary to adapt that program to handle
Person objects. Here are the major changes:
The type of the array a is changed to Person.
The key field (the last name) is now a String object, so comparisons require the equals() method
rather than the == operator. The getLast() method of Person obtains the last name of a Person object,
and equals() does the comparison:
if( a[j].getLast().equals(searchName) ) // found item?
The insert() method creates a new Person object and inserts it in the array, instead of inserting a long
value.
The main() method has been modified slightly, mostly to handle the increased quantity of output. We
still insert 10 items, display them, search for 1 item, delete 3 items, and display them all again. Listing
2.5 shows the complete classDataArray.java program.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 19
23. The classDataArray.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 23
The classDataArray.java
program shows that
class objects can be
handled by data storage
structures in much the
same way as primitive
types. (Note that a
serious program using
the last name as a key
would need to account
for duplicate last
names, which would
complicate the
programming as
discussed earlier.)
24. 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.
• The logarithm to the base B of a number A is (roughly) the number of times you can divide A by B
before the result is less than 1.
• 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.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 24
25. PROGRAMMING PROJECTS
2.1 To the HighArray class in the highArray.java program (Listing 2.3), add a method called
getMax() that returns the value of the highest key in the array, or –1 if the array is empty. Add
some code in main() to exercise this method. You can assume all the keys are positive numbers.
2.2 Modify the method in Programming Project 2.1 so that the item with the highest key is not
only returned by the method, but also removed from the array. Call the method removeMax().
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 25