Lecture # 1 Data Structures and Algorithm
Lecture # 1 Data Structures and Algorithm
Introduction to Data
Structure
Linked list
queue
tree stack
Linear Data Structures 1
0
thanks to www.tutorialspoint.com
Interface represents the set of operations
that a data structure supports.
Implementation
Implementation provides the internal
representation of a data structure. It also
provides the definition of algorithms used
in the operation of data structure.
Data Structure 1
3
Operations
Traversing
accessing and processing each record
exactly once.
Searching
finding the location of the record with key
value.
Inserting
Adding new record to the structure.
Deleting
Removing a record from the structure.
Characteristics of Data Structure1
4
Correctness
Data structure implementation should implement its
thanks to www.tutorialspoint.com
interface correctly.
Time Complexity
Running time or the execution time of operations of data
structure must be as small as possible.
Space Complexity
Memory usage of a data structure operation should be
as little as possible.
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must meet.
2. Determine the basic operations that must be
supported. Quantify the resource
constraints for each operation.
3. Select the data structure that best meets
these requirements.
1
6
Introduction to
Algorithms
A PRECISE RULE (OR SET OF RULES)
SPECIFYING HOW TO SOLVE SOME PROBLEM.
Algorithms 1
7
An algorithm is a clearly defined set of instructions to
be followed to solve a problem. Algorithms are
thanks to www.tutorialspoint.com
generally created independent of underlying
programming languages.
OR
An algorithms is a well defined list of finite steps for
solving a particular problem.
Major purpose is to develop efficient algorithm for the
processing of data.
The time and space are two major measures to check
the efficiency of an algorithm.
1
8
From the data structure point of view, following
are some important categories of algorithms
Search − Algorithm to search an item in a data
structure
Sort − Algorithm to sort items in a certain order
Insert − Algorithm to insert item in a data
structure
Update − Algorithm to update an existing item in
a data structure
Delete − Algorithm to delete an existing item
from a data structure
Characteristics of an Algorithm
1
9
Not all procedures can be called an algorithm. An algorithm
should have the following characteristics:
thanks to www.tutorialspoint.com
Unambiguous − Algorithm should be clear and unambiguous.
Each of its steps (or phases), and their inputs/outputs should
be clear and must lead to only one meaning.
Input − An algorithm should have 0 or more well-defined
inputs.
Output − An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
Finiteness − Algorithms must terminate after a finite number
of steps.
Feasibility − Should be feasible with the available resources.
Independent − An algorithm should have step-by-step
directions, which should be independent of any programming
code.
Example 2
0
Problem − Design an algorithm to add two numbers
and display the result.
thanks to www.tutorialspoint.com
Step 1 – START
Step 2 – declare three integers a, b & c
Step 3 – define values of a & b
Step 4 – add values of a & b
Step 5 – store output of Step 4 to c
Step 6 – print c
Step 7 – STOP
Example 2
1
thanks to www.tutorialspoint.com
and display the result.
Steps:
Algorithm consists of a sequence of numbered steps.
Each step describes one task to be performed.
Comments:
Used to explain the purpose of a step
Variable names:
Name of variable is chosen according the value it hold
Operator:
Arithmetic (+,-,*,/) Relational(<,<=,>,>= etc.) and
logical( AND, OR, NOT)
Assignment statement:
the statement used “=” assignment operator
Sub Algorithm
2- Exit
• (Traversing a Linear Array) Here LA is a linear Array
with lower bound LB and upper bound UB. This algo
travers LA applying a PROCESS to each element of
LA
• Step 1
[Initialize] Set k=LB
• Step 2
Repeat Step 3 and 4 while K<=UB
• Step 3
Applying PROCESS to LA[k]
• Step 4
[Increment Counter] Set K=K+1
[End of Step 2 Loop]
Step 5 Exit.
Example
8 , 4 , 19, 2 , 7 , 13 , 5 , 16
2 , 4 , 5 , 7 , 8 , 13 , 16 , 19
Bubble sort cont.
Suppose that the list of numbers A[1], A[2],…A[N] is in memory,
working of bubble sort is as follow
Step-1: Compare A[1] and A[2] and arrange them in desired
order, so that A[1] < A[2].
Then compare A[2] and A[3] and arrange them , so that A[2] < A[3].
Continue until we compare A[N-1] with A[N] and arrange them , so that
A[N-1] < A[N].
(Binary Search)BINARY(DATA,LB,UB,ITEM,LOC)
Here DATA is sorted array with lower bound LB
and upper bound UB and ITEM is a given type
of information. The variables BEG,END, and
MID denote respectively, the beginning, end
and middle location of a segment of elements
of DATA. This algorithm finds the location LOC
of ITEM in DATA.
1- [initialize variables] Set BEG=LB, END=UB LOC=-1
and MID=INT((BEG+END))/2
2- Repeat step 3 and 4 while (BEG < = END) and
DATA[MID] != ITAEM
3- If ITEM<DATA[MID] then
Set END=MID-1
else
Set BEG= MID+1
[End of IF]
4- Set MID=INT((BEG+END))/2
[End of Step 2 loop]
5- If DATA[MID]=ITEM
then
Set LOC= MID
[End of IF]
6- Exit