Vous êtes sur la page 1sur 32

DFC 2023

ALGORITHM AND
DATA STRUCTURE
CHAPTER 1 :
OVERVIEW OF ALGORITHM AND DATA
STRUCTURES
SUMMARY
This topic covers on roles of algorithm and
overview of data structures.
COURSE OUTLINE
1.1 Understand Algorithm
1.1.1 Identify the characteristics of algorithm.
1.1.2 Describe the role of algorithm in problem
solving.

1.2 Understand Data Structure.


1.2.1 Define data structure.
1.2.2 Identify the types data structures.
a. Primitive and non-primitive.
b. Linear and non-linear.
c. Static and dynamic.
COURSE LEARNING OUTCOME
Explain the algorithm and various data
structures appropriately. (C2, PLO1)
1.1 UNDERSTAND ALGORITHM
1.1.1 The characteristics of algorithm

What is Algorithm?
- Algorithm is a clearly specified finite set of
instructions a computer follows to solve a
problem.
- An algorithms also can be defined as a step-by-
step procedure for solving a problem. It helps the
user arrive at the correct result in a finite number
of steps.
1.1 UNDERSTAND ALGORITHM
1.1.1 The characteristics of algorithm

Consider the following step-by-step procedures to


display the first 10 natural numbers:-
1. Set the value of counter to 1
2. Display counter
3. Increment counter by 1
4. If counter <=10, go to step 2

The preceding step-by-step procedure is an


algorithm because it produces the correct result in
a finite number of steps.
1.1 UNDERSTAND ALGORITHM
1.1.1 The characteristics of algorithm
The characteristics of a good algorithm are :-

1. Precision:
The steps are precisely stated(defined).

2. Uniqueness:
Results of each step are uniquely defined and only depend
on the input and the result of the preceding steps.

3 Finiteness:
The algorithm stops after a finite number of instructions
are executed.
1.1 UNDERSTAND ALGORITHM
1.1.1 Identify the characteristics of algorithm

The characteristics of a good algorithm are :-cont..

4. Input:
The algorithm receives input.

5. Output:
An algorithm produces at least one output.

6 Generality:
The algorithm applies to a set of inputs.
1.1 UNDERSTAND ALGORITHM
1.1.1 Identify the characteristics of algorithm

With this definition, we can identify five important


characteristics of algorithms.

1.Algorithms are well-ordered.


2.Algorithms have unambiguous operations.
3.Algorithms have effectively computable
operations.
4.Algorithms produce a result.
5.Algorithms halt in a finite amount of time.
1.1 UNDERSTAND ALGORITHM
1.1.2 Role of Algorithm in Problem Solving

- A problem can be solved by using a computer only if an


algorithm can be written for it.
- The use of algorithm can also provides many other benefits:-
i. While writing an algorithm, you identify the step-by-step
procedure, the major decision points, and the variables necessary to
solve the problem. This helps you in the development of the
corresponding program.

ii. Identification of the procedure and the decision points reduces


the problem into a series of smaller problems of more manageable
size. Therefore, problems that would be difficult or impossible to
solve as a whole can be approached as a series of small solvable sub
problems.
1.1 UNDERSTAND ALGORITHM
1.1.2 Role of Algorithm in Problem Solving

- The use of algorithm can also provides many other


benefits:-cont..

iii. With the use of an algorithm, decision making becomes a


more rational process. This is because algorithms comprise of
sub tasks, where each sub task is atomic in nature and is
supported by facts.

iv. With the use of an algorithm, the same specified steps are
used for performing the tasks. This makes the process more
consistent and reliable.
1.2 UNDERSTAND DATA STRUCTURES

1.2.1 Define Data Structure

Why need to study Data Structure?


- Multiple algorithms can be designed to solve a particular
problem.
- However, the algorithm may differ in how efficiently they
can solve the problem.
- In such a situation, an algorithm that provides the maximum
efficiency should be used for solving the problem. - Efficiency
here means that the algorithm should work in minimal time
and use minimal memory.
- One of the basic techniques for improving the efficiency of
algorithms is to structure the data that they operate on in
such a way that the resulting operations can be efficiently
performed.
1.2 UNDERSTAND DATA STRUCTURES

1.2.1 Define Data Structure

What is Data Structure?


- A data structure is a way in which the various data
elements are organized in memory with respect to each
other.
- Data can be organized in many different ways; therefore,
you can create as many data structures as you want.
- However, there are some standard data structures that
have proved useful over the years. These include arrays,
linked lists, stacks, queues, trees and sorting and
searching.
- All these data structures are designed to hold a collection
of data items.
1.2 UNDERSTAND DATA STRUCTURES

1.2.2 Identify the types Data Structures

There are 3 types of data structures:


a. Primitive and non-primitive.
b. Linear and non-linear.
c. Static and dynamic.
1.2 UNDERSTAND DATA STRUCTURES

1.2.2 Identify the types Data Structures

a. Primitive and non-primitive


Primitive Data type
- The primitive data types are the basic data types that are
available in most of the programming languages.
- The primitive data types are used to represent single values.
- Integer:This is used to represent a number without decimal
point.
- Float and Double:This is used to represent a number with
decimal point.
- Character :This is used to represent single character
- String:This is used to represent group of characters.
- Boolean:This is used represent logical values either true or
false.
1.2 UNDERSTAND DATA STRUCTURES

1.2.2 Identify the types Data Structures

a. Primitive and non-primitive cont.


Non-Primitive Data type
- The data types that are derived from primary data types
are known as non-Primitive data types.
- These data types are used to store group of values.
- The non-primitive data types are:
i.Arrays
ii.Structure
iii.linked list
iv.Stacks
v.Queue
1.2 UNDERSTAND DATA STRUCTURES
1.2.2 Identify the types Data Structures

b. Linear and non-linear


Linear
- Data elements in a liner data structure are traversed one
after the other and only one element can be directly reached
while traversing.
- Linear data structures are very easy to implement, since
the memory of the computer is also organized in a linear
fashion.
- Some commonly used linear data structures are arrays,
linked lists, stacks and queues.
1.2 UNDERSTAND DATA STRUCTURES
1.2.2 Identify the types Data Structures

b. Linear and non-linear


Non-Linear
- A data item in a nonlinear data structure could be attached
to several other data elements to reflect a special
relationship among them and all the data items cannot be
traversed in a single run.
- Data structures like multidimensional arrays, trees and
graphs are some examples of widely used nonlinear data
structures.
1.2 UNDERSTAND DATA STRUCTURES

1.2.2 Identify the types Data Structures


1.2 UNDERSTAND DATA STRUCTURES
1.2.2 Identify the types Data Structures

b. Linear and non-linear


Linear vs. Non-Linear
- Main difference between linear and nonlinear data
structures lie in the way they organize data elements. - In
linear data structures, data elements are organized
sequentially and therefore they are easy to implement in
the computers memory.
- In nonlinear data structures, a data element can be
attached to several other data elements to represent
specific relationships that exist among them. Due to this
nonlinear structure, they might be difficult to be
implemented in computers linear memory.
1.2 UNDERSTAND DATA STRUCTURES
1.2.2 Identify the types Data Structures

c. Static and dynamic cont.


Static
- With a static data structure, the size of the structure is
fixed.
- Static data structures are very good for storing a well-
defined number of data items.

Dynamic
- The data structure is allowed to grow and shrink as the
demand for storage arises.
- The programmer should also set a maximum size to help
avoid memory collisions.
1.2 UNDERSTAND DATA STRUCTURES
1.2.2 Identify the types Data Structures

Example
1.2 UNDERSTAND DATA STRUCTURES
Example (Static)
1.2 UNDERSTAND DATA STRUCTURES
Example (Dynamic)
1.2 UNDERSTAND DATA STRUCTURES
Advantage and Disadvantage of Static vs. Dynamic
1.2 UNDERSTAND DATA STRUCTURES
Declaring Structure:
- Structure is a collection of related data items using one
similar name.
- The elements of structure, known as members can be of
different data types.
- Each structure you define can have an associated structure
name which is referred to as structure tag.
- To declare a structure, use the struct statement. It
declares a new data type, with more than one member.
1.2 UNDERSTAND DATA STRUCTURES
Declaring Structure: Cont..
- Syntax:

- The most common struct declaration in C++ is to


define a new type.
- Let say, Staff Record contains Employee No, Name,
Position and Basic Salary. Write struct declaration.
1.2 UNDERSTAND DATA STRUCTURES
Declaring Structure: Cont..

- This defines a new type, StaffRecord. The order of


the fields is generally not important. Don't forget the
semicolon after the right brace. The convention is to
capitalize the first letter in any new type name.
1.2 UNDERSTAND DATA STRUCTURES
Declaring Structure: Cont..

- The new struct type can now be used to declare variables.


- Syntax:

- Example:
1.2 UNDERSTAND DATA STRUCTURES
Declaring Structure:
- Declare a structure to illustrate student details contained information such as name, Id, Class and Session and CGPA.
1.2 UNDERSTAND DATA STRUCTURES
Declaring Structure: Case Study
- Declare a structure called AddressInfo which holds an info such as address, town, state, and postcode.
1.2 UNDERSTAND DATA STRUCTURES

Declaring Structure: Cont..


- The typedef command also can be used in the structure declaration.
- Example:

typedef struct biodata {


char name[20];
int age;
char address[50];
} Bio;

Vous aimerez peut-être aussi