SlideShare a Scribd company logo
Data Structures and
Algorithms
Week 1: Intro to Data Structures and Algorithms
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology, Bangkok
Join our g+ community
Faculty of Information Technology, Thai -
Nichi Institute of Technology
2
https://bit.ly/2skCIK0
DSA 107 – A Road Map
Attendance
(10%)
Mid Exam
(40%)
How GPA
works for
DSA 107?
Final Exam
(50%)
Faculty of Information Technology, Thai -
Nichi Institute of Technology
3
Textbooks
Faculty of Information Technology, Thai -
Nichi Institute of Technology
4
What is Program
• A Set of Instructions
• Data Structures + Algorithms
• Data Structure = A Container stores Data
• Algoirthm = Logic + Control
Lecture series for Semester 2, Data
Science and Analytics
5
Functions of Data Structures
• Add
• Index
• Key
• Position
• Priority
• Search
• Change
• Delete
Lecture series for Semester 2, Data
Science and Analytics
6
Common Data Structures
• Array
• Stack
• Queue
• Linked List
• Tree
• Heap
• Hash Table
• Priority Queue
Lecture series for Semester 2, Data
Science and Analytics
7
How many Algorithms?
• Countless
Lecture series for Semester 2, Data
Science and Analytics
8
Algorithm Strategies
• Greedy
• Divide and Conquer
• Dynamic Programming
• Exhaustive Search
Lecture series for Semester 2, Data
Science and Analytics
9
Which Data Structure or Algorithm is
better?
• Must Meet Requirement
• High Performance
• Low RAM footprint
• Easy to implement
• Encapsulated
Lecture series for Semester 2, Data
Science and Analytics
10
Chapter 1 Basic Concepts
• Overview: System Life Cycle
• Algorithm Specification
• Data Abstraction
• Performance Analysis
• Performance Measurement
Lecture series for Semester 2, Data
Science and Analytics
11
1.1 Overview: system life cycle (1/2)
• Good programmers regard large-scale computer
programs as systems that contain many complex
interacting parts.
• As systems, these programs undergo a
development process called the system life cycle.
Lecture series for Semester 2, Data
Science and Analytics
12
1.1 Overview (2/2)
• We consider this cycle as consisting of
five phases.
• Requirements
• Analysis: bottom-up vs. top-down
• Design: data objects and operations
• Refinement and Coding
• Verification
• Program Proving
• Testing
• Debugging
Lecture series for Semester 2, Data
Science and Analytics
13
1.2 Algorithm Specification
• 1.2.1 Introduction
• An algorithm is a finite set of instructions that accomplishes a
particular task.
• Criteria
• input: zero or more quantities that are externally supplied
• output: at least one quantity is produced
• definiteness: clear and unambiguous
• finiteness: terminate after a finite number of steps
• effectiveness: instruction is basic enough to be carried out
• A program does not have to satisfy the finiteness criteria.
Lecture series for Semester 2, Data
Science and Analytics
14
1.2 Algorithm Specification
• Representation
• A natural language, like English or Chinese.
• A graphic, like flowcharts.
• A computer language, like C++, Java etc.
• Algorithms + Data structures = Programs
• Sequential search vs. Binary search
Lecture series for Semester 2, Data
Science and Analytics
15
1.3 Data abstraction (1/4)
• Data Type
A data type is a collection of objects and a set of
operations that act on those objects.
• For example, the data type int consists of the objects {0, +1, -1,
+2, -2, …, INT_MAX, INT_MIN} and the operations +, -, *, /, and
%.
• The data types of Java
• The basic data types: char, int, float and double etc
• The group data types: array and arraytype data type
• The user-defined types
Lecture series for Semester 2, Data
Science and Analytics
16
1.3 Data abstraction (2/4)
• Abstract Data Type
• An abstract data type(ADT) is a data type
that is organized in such a way that
the specification of the objects and
the operations on the objects is separated from
the representation of the objects and
the implementation of the operations.
• We know what is does, but not necessarily how it will do it.
Lecture series for Semester 2, Data
Science and Analytics
17
1.3 Data abstraction (3/4)
• Specification vs. Implementation
• An ADT is implementation independent
• Operation specification
• function name
• the types of arguments
• the type of the results
• The functions of a data type can be classify into several
categories:
• creator / constructor
• transformers
• observers / reporters
Lecture series for Semester 2, Data
Science and Analytics
18
1.3 Data abstraction (4/4)
• Example [Abstract data type Natural_Number]
::= is defined as
Lecture series for Semester 2, Data
Science and Analytics
19
1.4 Performance analysis (1/4)
• Criteria
• Is it correct?
• Is it readable?
• …
• Performance Analysis (machine independent)
• space complexity: storage requirement
• time complexity: computing time
• Performance Measurement (machine dependent)
Lecture series for Semester 2, Data
Science and Analytics
20
1.4 Performance analysis (2/4)
• 1.4.1 Space Complexity:
S(P)=C+SP(I)
• Fixed Space Requirements (C)
Independent of the characteristics
of the inputs and outputs
• instruction space
• space for simple variables, fixed-size structured variable, constants
• Variable Space Requirements (SP(I))
depend on the instance characteristic I
• number, size, values of inputs and outputs associated with I
• recursive stack space, formal parameters, local variables, return
address
Lecture series for Semester 2, Data
Science and Analytics
21
1.4 Performance analysis (5/4)
• 1.4.2 Time Complexity:
T(P)=C+TP(I)
• The time, T(P), taken by a program, P, is the sum of its
compile time C and its run (or execution) time, TP(I)
• Fixed time requirements
• Compile time (C), independent of instance characteristics
• Variable time requirements
• Run (execution) time TP
• TP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n)
Lecture series for Semester 2, Data
Science and Analytics
22
1.4 Performance analysis (6/4)
• A program step is a syntactically or semantically
meaningful program segment whose execution
time is independent of the instance
characteristics.
• Example
(Regard as the same unit machine independent)
• abc = a + b + b * c + (a + b - c) / (a + b) + 4.0
• abc = a + b + c
• Methods to compute the step count
• Introduce variable count into programs
• Tabular method
• Determine the total number of steps contributed by each
statement step per execution × frequency
• add up the contribution of all statements
Lecture series for Semester 2, Data
Science and Analytics
23
Array ADT – Java Implementation
Lecture series for Semester 2, Data
Science and Analytics
24
An Array-Based Implementation
of the ADT List
public class ListArrayBased
implements ListInterface {
private static final int MAX_LIST = 50;
private Object items[];
// an array of list items
private int numItems;
// number of items in list
Lecture series for Semester 2, Data
Science and Analytics
25
An Array-Based Implementation
of the ADT List
public ListArrayBased() {
items = new Object[MAX_LIST];
numItems = 0;
} // end default constructor
Lecture series for Semester 2, Data
Science and Analytics
26
An Array-Based Implementation
of the ADT List
public boolean isEmpty() {
return (numItems == 0);
} // end isEmpty
public int size() {
return numItems;
} // end size
Lecture series for Semester 2, Data
Science and Analytics
27
Insertion into Array
What happens if you want to insert an item at a specified
position in an existing array?
• Write over the current contents at the given index (which might not
be appropriate), or
• The item originally at the given index must be moved up one
position, and all the items after that index must shuffled up
Lecture series for Semester 2, Data
Science and Analytics
28
An Array-Based Implementation
of the ADT List
public void add(int index, Object item)
throws ListException,
ListIndexOutOfBoundsException {
if (numItems >= MAX_LIST) {
throw new ListException("ListException on add:"+
" out of memory");
} // end if
if (index < 1 || index > numItems+1) {
// index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on add");
} // end if
Lecture series for Semester 2, Data
Science and Analytics
29
An Array-Based Implementation
of the ADT List
// make room for new element by shifting all items at
// positions >= index toward the end of the
// list (no shift if index == numItems+1)
for (int pos = numItems; pos >= index; pos--) {
items[pos] = items[pos-1];
} // end for
// insert new item
items[index-1] = item;
numItems++;
} //end add
Lecture series for Semester 2, Data
Science and Analytics
30
Removal from Arrays
What happens if you want to remove an item from a specified
position in an existing array?
• Leave gaps in the array, i.e. indices that contain no elements, which
in practice, means that the array element has to be given a special
value to indicate that it is “empty”, or
• All the items after the (removed item’s) index must be shuffled
down
Lecture series for Semester 2, Data
Science and Analytics
31
An Array-Based Implementation
of the ADT Listpublic void remove(int index)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems) {
// delete item by shifting all items at
// positions > index toward the beginning of the list
// (no shift if index == size)
for (int pos = index+1; pos <= size(); pos++) {
items[pos-2] = items[pos-1];
} // end for
numItems--;
}
else { // index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on remove");
} // end if
} // end remove Lecture series for Semester 2, Data
Science and Analytics
32
An Array-Based Implementation
of the ADT List
public Object get(int index)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems) {
return items[index-1];
}
else { // index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on get");
} // end if
} // end get
Lecture series for Semester 2, Data
Science and Analytics
33
An Array-Based Implementation -
Summary
• Good things:
• Fast, random access of elements
• Very memory efficient, very little memory is required
other than that needed to store the contents (but see
bellow)
• Bad things:
• Slow deletion and insertion of elements
• Size must be known when the array is created and is
fixed (static)
Lecture series for Semester 2, Data
Science and Analytics
34
Recommended Reading
https://www.quora.com/Does-a-data-scientist-need-
to-know-algorithms-and-data-structures-as-well-as-
a-software-engineer
Lecture series for Semester 2, Data
Science and Analytics
35
Next Week
Linked List ADT
Doubly Linked List
Circular Linked List
Implementation in Java
Lecture series for Semester 2, Data
Science and Analytics
36

More Related Content

PDF
Data Structures and Algorithm - Week 9 - Search Algorithms
PDF
Data Structures and Algorithm - Week 11 - Algorithm Analysis
PDF
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
PDF
Data Structures and Algorithm - Week 3 - Stacks and Queues
PDF
Week 2 - Data Structures and Algorithms
PDF
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
PDF
Data Structures and Algorithm - Week 5 - AVL Trees
PDF
Data Structures and Algorithm - Week 6 - Red Black Trees
Data Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
Data Structures and Algorithm - Week 3 - Stacks and Queues
Week 2 - Data Structures and Algorithms
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
Data Structures and Algorithm - Week 5 - AVL Trees
Data Structures and Algorithm - Week 6 - Red Black Trees

What's hot (20)

PPTX
Data Structure and Algorithms
PDF
geekgap.io webinar #1
PPTX
Introduction to data structure and algorithms
PPS
Data Structure
PDF
Data structures and algorithm analysis in java
PPT
Data Structures 6
PPT
Data Structures 8
PPT
Data Structures 7
PPTX
Basic of Data Structure - Data Structure - Notes
PPT
Data Structures & Algorithms
PDF
Binary Sort
PDF
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
PPT
Stacks in algorithems & data structure
PPT
Storage struct
PPTX
Wrokflow programming and provenance query model
PPTX
Lecture 01
PPTX
Lecture 01 Intro to DSA
PPTX
Searching techniques with progrms
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPTX
Data structure and algorithm using java
Data Structure and Algorithms
geekgap.io webinar #1
Introduction to data structure and algorithms
Data Structure
Data structures and algorithm analysis in java
Data Structures 6
Data Structures 8
Data Structures 7
Basic of Data Structure - Data Structure - Notes
Data Structures & Algorithms
Binary Sort
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
Stacks in algorithems & data structure
Storage struct
Wrokflow programming and provenance query model
Lecture 01
Lecture 01 Intro to DSA
Searching techniques with progrms
4. Recursion - Data Structures using C++ by Varsha Patil
Data structure and algorithm using java
Ad

Similar to Week 1 - Data Structures and Algorithms (20)

PPT
PPT
dsa3.ppt
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
PPT
data structurer and algorithm EE 2204.ppt
PPT
DAta structure and algorithm presentation
PPT
dsa.ppt
PPT
dsa.ppt
PPT
dsa (1).ppt
PPT
dsa.ppt
PDF
Data Structures 01
PPT
Data Structure And Algorithm Presentation
PDF
M v bramhananda reddy dsa complete notes
PPT
Data Structures and Algorithms en anglais
PDF
Data Structures
PPT
data structures algorithm-AVL tresss.ppt
PPT
Merriam-Webster's Definition merriam.ppt
PPTX
Data Structures - Lecture 1 - Unit 1.pptx
PPTX
Data structure basic concepts and sparse
PDF
1-5 ADS Notes.pdf
PDF
Data Structures Notes 2021
dsa3.ppt
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
data structurer and algorithm EE 2204.ppt
DAta structure and algorithm presentation
dsa.ppt
dsa.ppt
dsa (1).ppt
dsa.ppt
Data Structures 01
Data Structure And Algorithm Presentation
M v bramhananda reddy dsa complete notes
Data Structures and Algorithms en anglais
Data Structures
data structures algorithm-AVL tresss.ppt
Merriam-Webster's Definition merriam.ppt
Data Structures - Lecture 1 - Unit 1.pptx
Data structure basic concepts and sparse
1-5 ADS Notes.pdf
Data Structures Notes 2021
Ad

More from Ferdin Joe John Joseph PhD (20)

PDF
Invited Talk DGTiCon 2022
PDF
Week 12: Cloud AI- DSA 441 Cloud Computing
PDF
Week 11: Cloud Native- DSA 441 Cloud Computing
PDF
Week 10: Cloud Security- DSA 441 Cloud Computing
PDF
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
PDF
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
PDF
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
PDF
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
PDF
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
PDF
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
PDF
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
PDF
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
PDF
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
PDF
Hadoop in Alibaba Cloud
PDF
Cloud Computing Essentials in Alibaba Cloud
PDF
Transforming deep into transformers – a computer vision approach
PDF
Week 11: Programming for Data Analysis
PDF
Week 10: Programming for Data Analysis
PDF
Week 9: Programming for Data Analysis
PDF
Week 8: Programming for Data Analysis
Invited Talk DGTiCon 2022
Week 12: Cloud AI- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Hadoop in Alibaba Cloud
Cloud Computing Essentials in Alibaba Cloud
Transforming deep into transformers – a computer vision approach
Week 11: Programming for Data Analysis
Week 10: Programming for Data Analysis
Week 9: Programming for Data Analysis
Week 8: Programming for Data Analysis

Recently uploaded (20)

PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Global journeys: estimating international migration
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
Launch Your Data Science Career in Kochi – 2025
PPTX
Database Infoormation System (DBIS).pptx
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPT
Quality review (1)_presentation of this 21
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Moving the Public Sector (Government) to a Digital Adoption
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
IB Computer Science - Internal Assessment.pptx
Supervised vs unsupervised machine learning algorithms
Business Ppt On Nestle.pptx huunnnhhgfvu
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
.pdf is not working space design for the following data for the following dat...
1_Introduction to advance data techniques.pptx
Global journeys: estimating international migration
Galatica Smart Energy Infrastructure Startup Pitch Deck
Introduction to Knowledge Engineering Part 1
IBA_Chapter_11_Slides_Final_Accessible.pptx
Launch Your Data Science Career in Kochi – 2025
Database Infoormation System (DBIS).pptx
Fluorescence-microscope_Botany_detailed content
STUDY DESIGN details- Lt Col Maksud (21).pptx
Quality review (1)_presentation of this 21
Reliability_Chapter_ presentation 1221.5784
Moving the Public Sector (Government) to a Digital Adoption

Week 1 - Data Structures and Algorithms

  • 1. Data Structures and Algorithms Week 1: Intro to Data Structures and Algorithms Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology, Bangkok
  • 2. Join our g+ community Faculty of Information Technology, Thai - Nichi Institute of Technology 2 https://bit.ly/2skCIK0
  • 3. DSA 107 – A Road Map Attendance (10%) Mid Exam (40%) How GPA works for DSA 107? Final Exam (50%) Faculty of Information Technology, Thai - Nichi Institute of Technology 3
  • 4. Textbooks Faculty of Information Technology, Thai - Nichi Institute of Technology 4
  • 5. What is Program • A Set of Instructions • Data Structures + Algorithms • Data Structure = A Container stores Data • Algoirthm = Logic + Control Lecture series for Semester 2, Data Science and Analytics 5
  • 6. Functions of Data Structures • Add • Index • Key • Position • Priority • Search • Change • Delete Lecture series for Semester 2, Data Science and Analytics 6
  • 7. Common Data Structures • Array • Stack • Queue • Linked List • Tree • Heap • Hash Table • Priority Queue Lecture series for Semester 2, Data Science and Analytics 7
  • 8. How many Algorithms? • Countless Lecture series for Semester 2, Data Science and Analytics 8
  • 9. Algorithm Strategies • Greedy • Divide and Conquer • Dynamic Programming • Exhaustive Search Lecture series for Semester 2, Data Science and Analytics 9
  • 10. Which Data Structure or Algorithm is better? • Must Meet Requirement • High Performance • Low RAM footprint • Easy to implement • Encapsulated Lecture series for Semester 2, Data Science and Analytics 10
  • 11. Chapter 1 Basic Concepts • Overview: System Life Cycle • Algorithm Specification • Data Abstraction • Performance Analysis • Performance Measurement Lecture series for Semester 2, Data Science and Analytics 11
  • 12. 1.1 Overview: system life cycle (1/2) • Good programmers regard large-scale computer programs as systems that contain many complex interacting parts. • As systems, these programs undergo a development process called the system life cycle. Lecture series for Semester 2, Data Science and Analytics 12
  • 13. 1.1 Overview (2/2) • We consider this cycle as consisting of five phases. • Requirements • Analysis: bottom-up vs. top-down • Design: data objects and operations • Refinement and Coding • Verification • Program Proving • Testing • Debugging Lecture series for Semester 2, Data Science and Analytics 13
  • 14. 1.2 Algorithm Specification • 1.2.1 Introduction • An algorithm is a finite set of instructions that accomplishes a particular task. • Criteria • input: zero or more quantities that are externally supplied • output: at least one quantity is produced • definiteness: clear and unambiguous • finiteness: terminate after a finite number of steps • effectiveness: instruction is basic enough to be carried out • A program does not have to satisfy the finiteness criteria. Lecture series for Semester 2, Data Science and Analytics 14
  • 15. 1.2 Algorithm Specification • Representation • A natural language, like English or Chinese. • A graphic, like flowcharts. • A computer language, like C++, Java etc. • Algorithms + Data structures = Programs • Sequential search vs. Binary search Lecture series for Semester 2, Data Science and Analytics 15
  • 16. 1.3 Data abstraction (1/4) • Data Type A data type is a collection of objects and a set of operations that act on those objects. • For example, the data type int consists of the objects {0, +1, -1, +2, -2, …, INT_MAX, INT_MIN} and the operations +, -, *, /, and %. • The data types of Java • The basic data types: char, int, float and double etc • The group data types: array and arraytype data type • The user-defined types Lecture series for Semester 2, Data Science and Analytics 16
  • 17. 1.3 Data abstraction (2/4) • Abstract Data Type • An abstract data type(ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations. • We know what is does, but not necessarily how it will do it. Lecture series for Semester 2, Data Science and Analytics 17
  • 18. 1.3 Data abstraction (3/4) • Specification vs. Implementation • An ADT is implementation independent • Operation specification • function name • the types of arguments • the type of the results • The functions of a data type can be classify into several categories: • creator / constructor • transformers • observers / reporters Lecture series for Semester 2, Data Science and Analytics 18
  • 19. 1.3 Data abstraction (4/4) • Example [Abstract data type Natural_Number] ::= is defined as Lecture series for Semester 2, Data Science and Analytics 19
  • 20. 1.4 Performance analysis (1/4) • Criteria • Is it correct? • Is it readable? • … • Performance Analysis (machine independent) • space complexity: storage requirement • time complexity: computing time • Performance Measurement (machine dependent) Lecture series for Semester 2, Data Science and Analytics 20
  • 21. 1.4 Performance analysis (2/4) • 1.4.1 Space Complexity: S(P)=C+SP(I) • Fixed Space Requirements (C) Independent of the characteristics of the inputs and outputs • instruction space • space for simple variables, fixed-size structured variable, constants • Variable Space Requirements (SP(I)) depend on the instance characteristic I • number, size, values of inputs and outputs associated with I • recursive stack space, formal parameters, local variables, return address Lecture series for Semester 2, Data Science and Analytics 21
  • 22. 1.4 Performance analysis (5/4) • 1.4.2 Time Complexity: T(P)=C+TP(I) • The time, T(P), taken by a program, P, is the sum of its compile time C and its run (or execution) time, TP(I) • Fixed time requirements • Compile time (C), independent of instance characteristics • Variable time requirements • Run (execution) time TP • TP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n) Lecture series for Semester 2, Data Science and Analytics 22
  • 23. 1.4 Performance analysis (6/4) • A program step is a syntactically or semantically meaningful program segment whose execution time is independent of the instance characteristics. • Example (Regard as the same unit machine independent) • abc = a + b + b * c + (a + b - c) / (a + b) + 4.0 • abc = a + b + c • Methods to compute the step count • Introduce variable count into programs • Tabular method • Determine the total number of steps contributed by each statement step per execution × frequency • add up the contribution of all statements Lecture series for Semester 2, Data Science and Analytics 23
  • 24. Array ADT – Java Implementation Lecture series for Semester 2, Data Science and Analytics 24
  • 25. An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object items[]; // an array of list items private int numItems; // number of items in list Lecture series for Semester 2, Data Science and Analytics 25
  • 26. An Array-Based Implementation of the ADT List public ListArrayBased() { items = new Object[MAX_LIST]; numItems = 0; } // end default constructor Lecture series for Semester 2, Data Science and Analytics 26
  • 27. An Array-Based Implementation of the ADT List public boolean isEmpty() { return (numItems == 0); } // end isEmpty public int size() { return numItems; } // end size Lecture series for Semester 2, Data Science and Analytics 27
  • 28. Insertion into Array What happens if you want to insert an item at a specified position in an existing array? • Write over the current contents at the given index (which might not be appropriate), or • The item originally at the given index must be moved up one position, and all the items after that index must shuffled up Lecture series for Semester 2, Data Science and Analytics 28
  • 29. An Array-Based Implementation of the ADT List public void add(int index, Object item) throws ListException, ListIndexOutOfBoundsException { if (numItems >= MAX_LIST) { throw new ListException("ListException on add:"+ " out of memory"); } // end if if (index < 1 || index > numItems+1) { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on add"); } // end if Lecture series for Semester 2, Data Science and Analytics 29
  • 30. An Array-Based Implementation of the ADT List // make room for new element by shifting all items at // positions >= index toward the end of the // list (no shift if index == numItems+1) for (int pos = numItems; pos >= index; pos--) { items[pos] = items[pos-1]; } // end for // insert new item items[index-1] = item; numItems++; } //end add Lecture series for Semester 2, Data Science and Analytics 30
  • 31. Removal from Arrays What happens if you want to remove an item from a specified position in an existing array? • Leave gaps in the array, i.e. indices that contain no elements, which in practice, means that the array element has to be given a special value to indicate that it is “empty”, or • All the items after the (removed item’s) index must be shuffled down Lecture series for Semester 2, Data Science and Analytics 31
  • 32. An Array-Based Implementation of the ADT Listpublic void remove(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { // delete item by shifting all items at // positions > index toward the beginning of the list // (no shift if index == size) for (int pos = index+1; pos <= size(); pos++) { items[pos-2] = items[pos-1]; } // end for numItems--; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on remove"); } // end if } // end remove Lecture series for Semester 2, Data Science and Analytics 32
  • 33. An Array-Based Implementation of the ADT List public Object get(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { return items[index-1]; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on get"); } // end if } // end get Lecture series for Semester 2, Data Science and Analytics 33
  • 34. An Array-Based Implementation - Summary • Good things: • Fast, random access of elements • Very memory efficient, very little memory is required other than that needed to store the contents (but see bellow) • Bad things: • Slow deletion and insertion of elements • Size must be known when the array is created and is fixed (static) Lecture series for Semester 2, Data Science and Analytics 34
  • 36. Next Week Linked List ADT Doubly Linked List Circular Linked List Implementation in Java Lecture series for Semester 2, Data Science and Analytics 36