SlideShare a Scribd company logo
Lesson 2.2 – Array Cont.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
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
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
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
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
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
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
Guessing a Number Table
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
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
Java Code for an ordered array
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
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
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
The OrdArray Class
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
The OrdArray Class
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
The OrdArray Class
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
The OrdArray Class
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
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
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
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
The classDataArray.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
The classDataArray.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
The classDataArray.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
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.)
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
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

More Related Content

PDF
Lesson 1 overview
PDF
Lesson 4 stacks and queues
PDF
PDF
Lesson 5 link list
PDF
Lesson 3 simple sorting
PDF
Lesson 6 recursion
PPTX
هياكلبيانات
PPTX
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Lesson 1 overview
Lesson 4 stacks and queues
Lesson 5 link list
Lesson 3 simple sorting
Lesson 6 recursion
هياكلبيانات
Data structure,abstraction,abstract data type,static and dynamic,time and spa...

What's hot (20)

PPT
Basic data-structures-v.1.1
PPTX
Data structures using C
PPTX
Computer Science-Data Structures :Abstract DataType (ADT)
PDF
Data structure using c++
PPTX
Bsc cs ii dfs u-1 introduction to data structure
PPTX
Data structure
PPT
Chapter 11 ds
PPTX
Mca ii dfs u-1 introduction to data structure
PPTX
Introduction to data structure
PPTX
Data Structure
PDF
Data structures
PPT
Introduction of data structure
PPT
Data structures
PPSX
Lecture 1 an introduction to data structure
PDF
Data Structure Basics
PPT
L6 structure
PDF
Data structures Basics
PDF
Data structure
PPTX
Data structure & algorithms introduction
PPS
Data Structure
Basic data-structures-v.1.1
Data structures using C
Computer Science-Data Structures :Abstract DataType (ADT)
Data structure using c++
Bsc cs ii dfs u-1 introduction to data structure
Data structure
Chapter 11 ds
Mca ii dfs u-1 introduction to data structure
Introduction to data structure
Data Structure
Data structures
Introduction of data structure
Data structures
Lecture 1 an introduction to data structure
Data Structure Basics
L6 structure
Data structures Basics
Data structure
Data structure & algorithms introduction
Data Structure
Ad

Similar to Lesson 2.2 abstraction (20)

DOC
Data structure lecture 2
PPTX
ppt on arrays in c programming language.pptx
PPTX
java framwork for HIBERNATE FRAMEWORK.pptx
PDF
Data structure lecture 2 (pdf)
PPT
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
PDF
Query optimization to improve performance of the code execution
PDF
11.query optimization to improve performance of the code execution
PPT
YDP_API&MS_UNIT_IIIii8iiiiiiiii8iiii.ppt
PPT
YDP_API&MS_UNIT_hiii detail notes to understand api.ppt
PPTX
Any Which Array But Loose
PPT
Data structures cs301 power point slides lecture 01
DOCX
Data structure and algorithm.
PPTX
CSE 443 (1).pptx
PDF
IRJET- Data Mining - Secure Keyword Manager
PPTX
Interview preparation for programming.pptx
PPTX
Introduction-to-Arrays-in-Java . Exploring array
PDF
Hybrid approach for generating non overlapped substring using genetic algorithm
PDF
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
DOCX
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
PDF
Data_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdf
Data structure lecture 2
ppt on arrays in c programming language.pptx
java framwork for HIBERNATE FRAMEWORK.pptx
Data structure lecture 2 (pdf)
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
Query optimization to improve performance of the code execution
11.query optimization to improve performance of the code execution
YDP_API&MS_UNIT_IIIii8iiiiiiiii8iiii.ppt
YDP_API&MS_UNIT_hiii detail notes to understand api.ppt
Any Which Array But Loose
Data structures cs301 power point slides lecture 01
Data structure and algorithm.
CSE 443 (1).pptx
IRJET- Data Mining - Secure Keyword Manager
Interview preparation for programming.pptx
Introduction-to-Arrays-in-Java . Exploring array
Hybrid approach for generating non overlapped substring using genetic algorithm
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
Data_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdf
Ad

More from MLG College of Learning, Inc (20)

PPTX
PPTX
PC111-lesson1.pptx
PPTX
PC LEESOON 6.pptx
PPTX
PC 106 PPT-09.pptx
PPTX
PPTX
PPTX
PPTX
PC 106 Slide no.02
PPTX
PPTX
PPTX
PC 106 Slide 1.pptx
PDF
Db2 characteristics of db ms
PDF

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Structure & Organelles in detailed.
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Institutional Correction lecture only . . .
PDF
Basic Mud Logging Guide for educational purpose
PDF
Classroom Observation Tools for Teachers
PPTX
Lesson notes of climatology university.
PDF
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
01-Introduction-to-Information-Management.pdf
TR - Agricultural Crops Production NC III.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial disease of the cardiovascular and lymphatic systems
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O5-L3 Freight Transport Ops (International) V1.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
GDM (1) (1).pptx small presentation for students
Renaissance Architecture: A Journey from Faith to Humanism
102 student loan defaulters named and shamed – Is someone you know on the list?
PPH.pptx obstetrics and gynecology in nursing
Cell Structure & Organelles in detailed.
Complications of Minimal Access Surgery at WLH
Institutional Correction lecture only . . .
Basic Mud Logging Guide for educational purpose
Classroom Observation Tools for Teachers
Lesson notes of climatology university.
O7-L3 Supply Chain Operations - ICLT Program

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
  • 13. The OrdArray Class 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
  • 14. The OrdArray Class 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
  • 15. The OrdArray Class 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
  • 16. The OrdArray Class 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
  • 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
  • 20. The classDataArray.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
  • 21. The classDataArray.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
  • 22. The classDataArray.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
  • 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