Lecture 1
Lecture 1
Course
AlgorithmsIntroduction
Lecture 1
Data Structures,
Algorithms
&
Performance Analysis
Objective of today’s lecture is to get familiar with these three terms which we’re going to study in this course.
Proble
m Memory
75684 3
7 6
3
4 5
75684 3 8
Memory
75684 3
Stack
Data
structures Data structure
Memory
7568 4
3
Stack
Data
structures Data structure
Memory
756 8
3
4
4
3
Stack
Data
structures Data structure
Memory
75 6
3
8
4
4 8
3
Stack
Data
structures Data structure
Memory
75
6
6
3
8
4
4 8
3
Stack
Data
structures Data structure
Memory
7
5
6
6
3
8
4 5
4 8
3
Stack
Data
structures Data structure
Memory
7
5
7 6
6
3
8
4 5
4 8
3
Stack
Data
structures Data structure
Memory
5
6
6
3
7
8
4 5
4 8
3
Stack
Data
structures Data structure
Memory
6
6
3
8
75 4
4 8
3
Stack
Data
structures Data structure
Memory
3
8
75 6 4
4 8
3
Stack
Data
structures Data structure
Memory
3
756 8 4
4
3
Stack
Data
structures Data structure
Memory
3
7568 4
3
Stack
Data
structures Data structure
Memory
75684 3
Stack
Proble
m Memory
75684 3
7 6
3
34865 7 4 5
8
Memory
75684 3
Array
Data
structures Data structure
Memory
7568 4
3 3
Array
Data
structures Data structure
Memory
756 8
3 4 3
4
Array
Data
structures Data structure
Memory
75 6
3 4 8 3
4
8
Array
Data
structures Data structure
Memory
75
6
3 4 8 6 3
4
8
Array
Data
structures Data structure
Memory
7
6
3 4 8 6 5 3
4 5
8
Array
Data
structures Data structure
Memory
7 6
3 4 8 6 5 7 3
4 5
8
Array
Data
structures Data structure
Memory
7 6
4 8 6 5 7
3 4 5
8
Array
Data
structures Data structure
Memory
7 6
8 6 5 7
34 5
8
Array
Data
structures Data structure
Memory
7 6
6 5 7
34 8 5
Array
Data
structures Data structure
Memory
7
5 7
348 6 5
Queue
Data
structures Data structure
Memory
ACBEF D
B C
D
F E
A B C DE F
A
Binary tree
Data
structures Data structure
Memory
ACBEF D
B C
D
ADE F E
C
Shortest route
from A to C A
Graph
Types of data
structures
Algorith
m
• A finite set of instructions, if followed then accomplishes a task.
• Algorithm can be written in English, C++, JAVA or can be diagrammed
like flow chart etc.
Algorithm – Criteria must be
satisfied
1. Input
2. Output
3. Definiteness: Each instruction is clear and unambiguous.
4. Finiteness: Algorithm terminates in all cases.
5. Effectiveness: In addition to definiteness each operation must be
feasible.
Algorithm vs.
Program
• Programs are not bounded to fulfill 4th (Finiteness) criteria.
• Book will use both words alternatively assuming the property also
holds for programs as well.
Algorithm –
Examples
• Selection sort
Algorithm –
Examples
• Insertion sort
Algorithm –
Examples
• Bubble sort
Algorithm –
Examples
• Linear search
Algorithm –
Examples
• Binary search
Why multiple algorithms for one
task?
• Here comes the concept of “Performance analysis/measurement”
• Performance analysis (Priori estimates)
• Space complexity
• Time complexity
• Performance measurement (Posterior testing)
Performance analysis – Time
complexity
Generics
What is a Generics?
Generics in OOP allow us to define a specification of a class or
method that can be used with any data type. When we design a
generic, the data types of the method parameters or class isn't
known - not until it is called.
Defining a Generic?
A generic class or method can be defined using angle brackets < >
and an identifier. The standard identifier used for generics is a
capital letter T i.e. <T>but any letter can be used - even a word.
Generics
If it makes sense to use an identifier other than T, feel free. It's not
a rule, just a guideline that most programmers follow.
How does this work? Well, ArrayList stores elements of type Object.
Every class extends Object and hence you can insert any object into
the ArrayList. Access element from List.
String name = (String)names.get(0);
Generics
Generics in java
While retrieving an element we need to cast it because the element
stored is of type Object. So what is the issue, you might think? Well
you can insert element of any type i.e. above List can have Integer,
Long, BigInteger, etc types in it.
Generics
Generics in java
This List is perfectly valid because elements extend Object class.
Now when we access the element from List we will run into
runtime exception i.e. ClassCastException.
String name = (String)names.get(2); // throws
ClassCastException
There is no compile time type safety for this code. In order to
provide compile time type safety and work with generic types it
was important to add generics to Java.
Exercises/
Homework
• Explore following links to get further understanding
• https://en.wikipedia.org/wiki/Data_structure
• https://en.wikipedia.org/wiki/Algorithm
• https://en.wikipedia.org/wiki/Generics_in_Java