0% found this document useful (0 votes)
38 views

Week1 - Introduction To Data Structures Algorithms

The document discusses topics related to data structures and algorithms in Java, including Java programming basics, object-oriented programming, and definitions of data structures and algorithms. It covers fundamental Java concepts like variables, operators, decision and loop statements, arrays, input/output, exception handling, and object-oriented principles like encapsulation, inheritance, and polymorphism. It also introduces pointers and defines data structures as specialized ways to organize and store data for efficient use.

Uploaded by

moraaloureen006
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Week1 - Introduction To Data Structures Algorithms

The document discusses topics related to data structures and algorithms in Java, including Java programming basics, object-oriented programming, and definitions of data structures and algorithms. It covers fundamental Java concepts like variables, operators, decision and loop statements, arrays, input/output, exception handling, and object-oriented principles like encapsulation, inheritance, and polymorphism. It also introduces pointers and defines data structures as specialized ways to organize and store data for efficient use.

Uploaded by

moraaloureen006
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Data Structures and Algorithms in Java

Objectives
Discuss the following topics:
• Java Programming Basics
• Object-Oriented Programming (OOP) in Java
• Definition of data structures
• Definition of Algorithms

Data Structures and Algorithms in Java


Data Structures and Algorithms in Java
Rudimentary Java
• A Java program is a sequence of statements
that have to be formed in accordance with the
predefined syntax
• A statement is the smallest executable unit in
Java
• Each statement ends with a semicolon
• Compound statements, or blocks, are marked
by delimiting them with braces, { and }

Data Structures and Algorithms in Java


Variable Declarations
• Each variable must be declared before it can be
used in a program
• It is declared by specifying its type and its name
• Variable names are strings of any length of
letters, digits, underscores, and dollar signs that
begin with a letter, underscore, or dollar sign
• A letter is any Unicode letter
• Java is case sensitive

Data Structures and Algorithms in Java


Variable Declarations (continued)
• A type of variable is either:
– One of the eight built-in basic types
– A built-in or user-defined class type
– An array

Table 1-1 Variable built-in types and their sizes


Type Size Range
boolean 1 bit true, false
char 16 bits Unicode characters
byte 8 bits [-128, 127]
short 16 bits [-32768, 32767]
int 32 bits [-2147483648, 2147483647]
long 64 bits [-9223372036854775808, 9223372036854775807]
float 32 bits [-3.4E38, 3.4E38]
double 64 bits [-1.7E308, 1.7E308]

Data Structures and Algorithms in Java


Operators
• Value assignments are executed with the
assignment operator =
• Use one at a time or string together with other
assignment operators
x = y = z = 1;
• For a prefix operator, a variable is incremented
(or decremented) first and then an operation is
performed in which the increment takes place
• For a postfix operator, autoincrement (or
autodecrement) is the last operation performed
Data Structures and Algorithms in Java
Decision Statements
• One decision statement is an if-else
statement
if (condition)
do something;
[else do something else;]

• A switch statement is shorthand for nested


if statements
switch (integer expression) {
case value1: block1; break;
. . . . . .
case valueN: blockN; break;
default: default block;
}
Data Structures and Algorithms in Java
Loops
• The first loop available in Java is the while
loop:
while (condition)
do something;

• The second loop is a do-while loop:


do
do something;
while (condition);

• The third loop is the for loop:


for (initialization; condition; increment)
do something;

Data Structures and Algorithms in Java


Arrays
• Arrays are Java objects
• There is no keyword with which all arrays are
declared
• Without keywords, subclasses cannot be
created
• Arrays are declared with empty brackets after
the name of the type or the name of the array
itself
• These two declarations are equivalent:
int[] a;
and
int a[];
Data Structures and Algorithms in Java
Input and Output
• To use the classes for reading and writing data,
the java.io package has to include the
statement:
import java.io.*;
• To print anything on the screen, use the
statements:
System.out.print(message);
System.out.println(message);
• To read one line at a time, the method
readLine() from BufferedReader is used

Data Structures and Algorithms in Java


Exception Handling
• Catching an error is possible by using the
try-catch statement
try {
do something;
} catch (exception-type exception-name) {
do something;
}
• The number of catch clauses is not limited to
one

Data Structures and Algorithms in Java


Data Structures and Algorithms in Java
ENCAPSULATION
• Fundamental to object-oriented programming (OOP) is the notion
of an object
– An objectis a data structure, combined with the operations pertinent
to that structure
• Most object-oriented languages (OOLs) define objects through
the use of a class
– A classis a template which implements the ADT defining the
objects the class creates
• Within a class, the data elements are called data members, and
the operations methods, function members, or member
functions
• The combination of data members and methods in a class is
referred to as data encapsulation
• An object, then, can also be defined as the instantiation of a
class, creating an entity that can be used in a program
• Data encapsulation binds the data structure and its operations
together in the class. The program can then focus on the
manipulation of these objects through their associated methods
Data Structures and Algorithms in Java
Encapsulation (continued)
• This approach has several advantages:
– The strong link between data and operations better
mimics real-world behavior, on which program
models are based
– Errors in implementation are confined to the methods
of a particular class in which they occur, making them
easier to detect and correct
– Details of the implementation of the object can be
hidden from other objects to prevent side effects from
occurring (principle of information-hiding =
abstraction)

Data Structures and Algorithms in Java


INHERITANCE
• Inheritance is a technique of reusing existing class
definitions to derive new classes
• These new classes (called derived classes or subclasses
or child classes) can inherit the attributes and behavior of
the pre-existing classes (called base classes or super
classes or parent classes)
• This relationship of classes through inheritance forms a
hierarchy, which can be diagrammed
• Information hiding can be extended through this hierarchy by
the access the base class allows the derived class(es)
• Derived classes can then add, delete, & modify methods and
data members in their own definitions (known as overriding)

Data Structures and Algorithms in Java


Inheritance (continued)

• The amount of access, and level of modifications, are


controlled by specifying public, protected, or privatein
the derived class header
– A derived class with public inheritance preserves the access
classes of the base class
– A derived class with protected inheritance treats public and
protected members of the base class as protected; private
members remain private
– Finally, derived classes with private inheritance treat all
members of the base class as private

Data Structures and Algorithms in Java


POINTERS
• A variable defined in a program has two important attributes
– Its content or value (what it stores)
– Its location or address (where it is)
• Normally, we access a variable’s contents by specifying the
variable’s name in an operation
• However, it is possible to store a variable’s address in
another variable. This new variable is called a pointer; it
allows us access to the original variable’s value through its
address
• A pointer is a variable whose value is the address of another
variable in memory

Data Structures and Algorithms in Java


POINTERS (CONTINUED)
• Pointers are defined much the same as other variables
– They have a type; in this case the type of variable they point to
– They have a user-defined name
– The name is preceded by an asterisk (“*”) to indicate the variable is a
pointer
• Given the declarations
int i=15,j,*p,*q;
i and j are integer variables, while p and q are pointers to integer variables
• To assign a variable’s address to a pointer, the address-of operator
(&) is placed before the variable
– Example: For pointer p to point to variable i, we write
p = &i;
•This pointer is then said to point to that variable
• To access the value a pointer points to, we have to dereference the
pointer, using the dereference operator, an asterisk (“*”)
– So *p refers to the location stored in p, i.e the address of i

Data Structures and Algorithms in Java


Polymorphism
• Polymorphism is the ability, in many OOLs, to create
objects of different types that respond to method calls
having the same name
• They differ in that they respond according to type-specific
behavior
• Which method is called depends on the time at which the
decision is made about the call. This decision is referred to
as binding, and can occur in different ways
– Static bindingdetermines the function call at compile time
– Dynamic bindingdelays the decision until run time

Data Structures and Algorithms in Java


Data Structures and Algorithms in Java
INTRODUCTION TO DATA
STRUCTURES
• A data structure is a special way of organizing
and storing data in a computer so that it can be
used efficiently.
– Data Structures are structures programmed to
store ordered data, so that various operations can
be performed on it easily. It represents the
knowledge of data to be organized in memory. It
should be designed and implemented in such a
way that it reduces the complexity and increases
the efficiency.

Data Structures and Algorithms in Java


Basic types of Data Structures
• Anything that can store data can be called as a data
structure, hence Integer, Float, Boolean, Char etc, all are
data structures. They are known as Primitive Data
Structures.
• There are some complex Data Structures, which are used
to store large and connected data. These are known as
Abstract Data Structures. Some example of Abstract
Data Structure are :
– Linked List
– Tree
– Graph
– Stack, Queue etc.
• All these data structures allow us to perform different
operations on data. We select these data structures based
on which type of operation is required. We will look into
these data structures in more details in our later lessons.

Data Structures and Algorithms in Java


Categories of data structures
• Data structures can be categorized into two:
1. Linear Data Structure
2. Non-linear Data Structure

• Linear data structures:


– Elements of Linear data structure are accessed in a sequential
manner. However, the elements can be stored in these data
structure in any order.
– Examples of linear data structure are: LinkedList, Stack, Queue
and Array

• Non-linear data structures:


– Elements of non-linear data structures are stored and accessed in
non-linear order.
– Examples of non-linear data structure are: Tree and Graph

Data Structures and Algorithms in Java


CLASSIFICATIONS OF DATA STRUCTURES
DATA
STRUCTURES

Built-in Data User Defined Data


Structures Structures

Boole
Integer Float Character an Linear Data Non-Linear Data
Structures Structures

Static Dynamic Tree Graph

Linked Queue Stack


Array
List
Data Structures and Algorithms in Java
Data structures: classifications
Characteristic Description
Linear Data items are arranged in a linear sequence. Example: Array

Non-Linear Data items are not in sequence. Example: Tree, Graph

Homogeneous All the elements are of same type. Example: Array

Non- The elements may or may not be of the same type.


Homogeneous Example: Structures
Static The sizes and structures of the associated memory locations are
fixed, at compile time. Example: Array

Dynamic They expands or shrinks depending upon the program need and
its execution. Also, their associated memory locations changes.
Example: Linked List created using pointers

Data Structures and Algorithms in Java


Advantages of Data Structures
Some of the advantages of using data structures
are:
1. Data Organization:- Data Structures provides different ways of
data organization so we have options to store the data in different
data structures based on the requirement.

2. Efficiency: The main reason we organize the data is to improve


the efficiency. We can store the data in arrays then why do we
need linked lists and other data structures? because when we
need to perform several operation such as add, delete update and
search on arrays , it takes more time in arrays than some of the
other data structures. So the fact that we are interested in other
data structures is because of the efficiency.

Data Structures and Algorithms in Java


What is an Algorithm ?
• An algorithm is a finite set of instructions or logic, written in order, to
accomplish a certain predefined task.
• Algorithm is not the complete code or program, it is just the core
logic(solution) of a problem, which can be expressed either as an
informal high-level description as pseudocode or using a flowchart.

• Every Algorithm must satisfy the following properties:


1. Input- There should be 0 or more inputs supplied externally to the
algorithm.
2. Output- There should be at least 1 output obtained.
3. Definiteness- Every step of the algorithm should be clear and well
defined.
4. Finiteness- The algorithm should have finite number of steps.
5. Correctness- Every step of the algorithm must generate a correct
output.
Data Structures and Algorithms in Java
ALGORITHM
• An algorithm is said to be efficient and fast, if it takes less time to
execute and consumes less memory space. The performance of
an algorithm is measured based on following properties :
1. Time Complexity
2. Space Complexity
• Space Complexity -Its the amount of memory space required by
the algorithm, during the course of its execution. An algorithm
generally requires space for following components :
– Instruction Space: space required to store the executable version
of the program.
– Data Space: space required to store all the constants and variables.
– Environment Space: space required to store the environment
information needed to resume the suspended function.
1. Time Complexity - is a way to represent the amount of time
required by the program to run till its completion
Data Structures and Algorithms in Java
BASIC OPERATIONS OF DATA
STRUCTURES
• Some specific operations process all data in the
data structures. These operations:
1. Traversing
2. Searching
3. Insertion
4. Deletion
5. Sorting
6. Merging

Data Structures and Algorithms in Java


CLASS EXERCISE
• Write an algorithm (pseudocode) to check
whether a number is a prime number or not.

Data Structures and Algorithms in Java


SOLUTION TO CLASS
EXERCISE
1. Accept a number from the user. Name it as num.
2. Declare an integer variable i.
3. Assign a value 2 to i.
4. Repeat until i > num/2:
a.If num % i = 0:
i. Display “The number is not prime”
ii.Exit
b.i = i + 1
5.Display “The number is prime”.

Data Structures and Algorithms in Java


SUMMARY
• Computer science deals with solving a variety of
problems by using computers.
– To solve a given problem by using computers, one
needs to create a program
• A program = algorithm + data structure

• An algorithm that provides the maximum efficiency


should be used for solving the problem.
– The efficiency of an algorithm can be improved by
using an appropriate data structure.
• Data structures help in creating programs that are
simple, reusable, and easy to maintain.
Data Structures and Algorithms in Java

You might also like