SlideShare a Scribd company logo
1 of 25
Download to read offline
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

What's hot

Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureRai University
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureRai University
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure NUPOORAWSARMOL
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structureeShikshak
 
Lecture 1 an introduction to data structure
Lecture 1   an introduction to data structureLecture 1   an introduction to data structure
Lecture 1 an introduction to data structureDharmendra Prasad
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures BasicsDurgaDeviCbit
 
Data structure & algorithms introduction
Data structure & algorithms introductionData structure & algorithms introduction
Data structure & algorithms introductionSugandh Wafai
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 

What's hot (20)

Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Data structure
Data structureData structure
Data structure
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Data Structure
Data StructureData Structure
Data Structure
 
Data structures
Data structuresData structures
Data structures
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
 
Data structures
Data structuresData structures
Data structures
 
Lecture 1 an introduction to data structure
Lecture 1   an introduction to data structureLecture 1   an introduction to data structure
Lecture 1 an introduction to data structure
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 
L6 structure
L6 structureL6 structure
L6 structure
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures Basics
 
Data structure
Data structureData structure
Data structure
 
Data structure & algorithms introduction
Data structure & algorithms introductionData structure & algorithms introduction
Data structure & algorithms introduction
 
Data Structure
Data StructureData Structure
Data Structure
 

Similar to Lesson 2.2 abstraction

Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Abbott
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxAmanRai352102
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Abbott
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...trangiaphuc362003181
 
Query optimization to improve performance of the code execution
Query optimization to improve performance of the code executionQuery optimization to improve performance of the code execution
Query optimization to improve performance of the code executionAlexander Decker
 
11.query optimization to improve performance of the code execution
11.query optimization to improve performance of the code execution11.query optimization to improve performance of the code execution
11.query optimization to improve performance of the code executionAlexander Decker
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01shaziabibi5
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 
IRJET- Data Mining - Secure Keyword Manager
IRJET- Data Mining - Secure Keyword ManagerIRJET- Data Mining - Secure Keyword Manager
IRJET- Data Mining - Secure Keyword ManagerIRJET Journal
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptxBilalHussainShah5
 
Hybrid approach for generating non overlapped substring using genetic algorithm
Hybrid approach for generating non overlapped substring using genetic algorithmHybrid approach for generating non overlapped substring using genetic algorithm
Hybrid approach for generating non overlapped substring using genetic algorithmeSAT Publishing House
 
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
Faculty of ScienceDepartment of ComputingFinal Examinati.docxFaculty of ScienceDepartment of ComputingFinal Examinati.docx
Faculty of ScienceDepartment of ComputingFinal Examinati.docxmydrynan
 
Data_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdf
Data_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdfData_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdf
Data_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdfNho Vĩnh
 
Algorithms.pptx
Algorithms.pptxAlgorithms.pptx
Algorithms.pptxjohn6938
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptxRaazIndia
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxKunalYadav65140
 
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxLab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxsmile790243
 

Similar to Lesson 2.2 abstraction (20)

Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
 
Query optimization to improve performance of the code execution
Query optimization to improve performance of the code executionQuery optimization to improve performance of the code execution
Query optimization to improve performance of the code execution
 
11.query optimization to improve performance of the code execution
11.query optimization to improve performance of the code execution11.query optimization to improve performance of the code execution
11.query optimization to improve performance of the code execution
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
CSE 443 (1).pptx
CSE 443 (1).pptxCSE 443 (1).pptx
CSE 443 (1).pptx
 
IRJET- Data Mining - Secure Keyword Manager
IRJET- Data Mining - Secure Keyword ManagerIRJET- Data Mining - Secure Keyword Manager
IRJET- Data Mining - Secure Keyword Manager
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptx
 
Hybrid approach for generating non overlapped substring using genetic algorithm
Hybrid approach for generating non overlapped substring using genetic algorithmHybrid approach for generating non overlapped substring using genetic algorithm
Hybrid approach for generating non overlapped substring using genetic algorithm
 
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERINGCOMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
 
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
Faculty of ScienceDepartment of ComputingFinal Examinati.docxFaculty of ScienceDepartment of ComputingFinal Examinati.docx
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
 
Data_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdf
Data_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdfData_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdf
Data_Structure_and_Algorithms_Using_C++ _ Nho Vĩnh Share.pdf
 
Algorithms.pptx
Algorithms.pptxAlgorithms.pptx
Algorithms.pptx
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
 
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxLab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
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
  • 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