Você está na página 1de 19

Introduction to Data Structures And Algorithms

Introduction To Data Structures And Algorithms 1


At the end of this chapter, you will be familiar with the concepts of data structures and
algorithms.

SCOPE
1.1 Introduction

1.2 Abstract Data Type

1.3 Algorithms
1.3.1 Analysis of Algorithms
1.3.2.1 Space Complexity
1.3.2.2 Time Complexity
1.3.2.3 Asymptotic Notation

1.4 Goals of Data Structures

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 1
Introduction to Data Structures And Algorithms

1.1 Introduction
Computers help us in solving many real life problems. We know that computers can work
with greater speed and accuracy than human beings. What actually does a computer do?
A very simple answer to this question is that a computer stores data and reproduces it as
information as and when required. Representation of data should be in a proper format so
that accurate information can be produced at high speed. In this course, we will study the
various ways in which data can be represented. Efficient storage and retrieval of data is
important in computing. In this course we will study and implement various data
structures.
Organized data is known as information. Let us consider a few examples and try to
understand what data structures are. You all must have seen a pile of plates in a
restaurant. Whenever a plate is required, the plate on the top is removed. Similarly, if a
plate is added to the pile, it is kept on the top of the pile. There is a definite process
involved in the storage and retrieval of the plates. If the rack is empty there will be no
plates available. Similarly if the rack is full then there will be no place for more plates.
Similar process is applied with stacks. Stack is a data structure which stores data at the
top, this operation is known as push. It retrieves data from the top, the operation is known
as pop. If the stack is empty then the pop operation raises an error while push operation
cannot be performed if the stack is full. Stack is shown in fig 1.1

Top

Fig 1.1
Stack (pile of plates)

You must have seen a queue at the bus stop. There is a well-defined method for a person
to enter the queue as well as leave the queue to get into the bus. To enter a queue a person
stands at the end of the queue and the person at the start of the queue leaves the queue to
enter the bus. Similarly, we have queue as a data structure in which a data element is
added at the rear end and removed from the front end. Fig 1.2 shows the structure of
queue.

front(leave the queue) rear (enter the queue)

Fig 1.2
Queues

Chapter 1
2 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

In a train, separate compartments are joined together. A new compartment can be added
at the end, at the beginning or in between the existing train compartments. The same
method is followed to remove a compartment. Linked lists in data structures follow
similar approach. An element can be inserted and deleted from any position in the linked
list. A list can be shown as in figure 1.3.

Fig 1.3
Linked list (train compartments)

There are other data structures that we will study in this course, for example graphs, trees
etc. As we have seen in the above examples data structures stores a given set of data
according to certain rules. These rules help in organizing the data.

You have studied programming languages before. This example will use some concepts
from it to explain why data structures are essential. Data can be stored in various ways.
Usually the representation chosen depends on the nature of the problem and not on the
data. Consider a program, which requires storing marks of five students for a single
subject. The simplest way to store them will be to use five integer variables. We assume
that the roll numbers are from one to five. Now I wish to write a program, which can give
me the marks of any roll number given as input. Is it possible to write an efficient
program to do this task if the numbers are stored as variables?

a b c d e

Fig 1.4
Marks stored in five different variables

The data, in this case the marks, is stored, but it cannot be reproduced as information
efficiently. Now we take an array of integers with five elements. The above data is stored
in the array. An array will have a name ‘a’ and each individual element can be accessed
using index. Therefore, the marks for the first roll number will be stored as a[0], for the
second roll number as a[1] and so on.

There is a relation between the roll number and the array index. Now it is much easier to
access the marks according to the roll number. This could not be achieved using
variables, as there was no relation between the data. It was not possible to relate marks
and roll number.

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 3
Introduction to Data Structures And Algorithms

a[0] a[1] a[2] a[3] a[4]

Fig 1.5
Marks stored as an array

The above example can be slightly modified so that now we will store the marks of three
subjects per roll number. We can take three independent arrays to store them. We can
access the individual marks for each roll number from the respective arrays. We find that
the arrays contain marks of different subjects for the same roll number. It would be easier
to handle the marks if these three arrays were grouped together.

a[0] a[1] a[2] a[3] a[4]

b[0] b[1] b[2] b[3] b[4]

c[0]c[1] c[2] c[3] c[4]

Fig 1.6
Three arrays to store the marks of three subjects

Now we need a data structure that stores the above information in logical relation with
each other so that it can be retrieved with higher speed. Structures in C store various
fields together, so now we can store the marks of three subjects in the structure and then
make an array of five elements. The above example shows that the method in which data
should be represented depends on the problem to be solved. The structure definition will
be as follows.

struct record
{
int a[5];
int b[5];
int c[5];
};

In the above problem, we have used three different types of representations for the data.
They are all built using the basic data structures. There are certain rules associated with
each data structure that restrict the storage and retrieving of data in an ordered format.
We have seen examples like stacks and queues, which restrict the entry and exit of its

Chapter 1
4 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

elements according to certain rules. Stacks allow entry and exit of elements at the top
position only, while in queues an element is added at the end and removed from the front.
Arrays and structures are built in data structures in most of the programming languages.
Data can be stored in many other ways using other type of data structures. Thus we can
define data structure as a collection of data elements whose organization is characterized
by accessing functions that are used to store and retrieve individual data elements. Data
structures are represented at the logical level using a tool called Abstract Data Type
(ADT) and actually implemented using algorithms.

Abstract Data Type’s show how exactly the data structure behaves. What are the various
operations required for the data structure? It can be called as the design of the data
structure. Using the Abstract Data Type, it can be implemented using various algorithms.
Abstract Data Type clearly states the nature of the data to be stored and the rules of the
various operations to be performed on the data. For example, the Abstract Data Type of a
stack will clearly state that a new element will be inserted at the top and an element can
be removed only from the top of the stack. It does not specify how these rules should be
implemented. It specifies what are the requirements of a stack.

The implementation of a data structure is done with the help of algorithms. An algorithm
is a logical sequence of discrete steps that describe a complete solution to a given
problem in a finite amount of time. A task can be carried out in various ways, hence
there are more than one algorithm which can be used to implement a given data structure.
This means that we will have to analyze algorithms to select the best suited for the
application. In this book, we will study the representation, implementation and analysis
of basic data structures.

1.2 Abstract Data Type

Before we move to abstract data type let us understand what data type is. Most of the
languages support basic data types viz. integer, real, character etc. At machine level, the
data is stored as strings containing 1’s and 0’s. Every data type interprets the string of bits
in different ways and gives different results. In short, data type is a method of interpreting
bit patterns.

Every data type has a fixed type and range of values it can operate on. For example, an
integer variable can hold values between the min and max values allowed and carry out
operations like addition, subtraction etc. For character data type, the valid values are
defined in the character set and the operations performed are like comparison, conversion
from one case to another etc. There are fixed operations, which can be carried out on
them. We can formally define data types as a formal description of the set of values and
operations that a variable of a given type may take.

That was about the inbuilt data types. One can also create user defined data types, decide
the range of values as well as operations to be performed on them. The first step towards
creating a user defined data type or a data structure is to define the logical properties. A
tool to specify the logical properties of a data type is Abstract Data Type (ADT).

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 5
Introduction to Data Structures And Algorithms

Data abstraction can be defined as separation of the logical properties of the organization
of programs' data from its implementation. This means that it states what the data should
be like. It does not consider the implementation details. ADT is the logical picture of a
data type; in addition, the specifications of the operations required to create and
manipulate objects of this data type.

In the next section, we will study the method to represent an ADT. While defining an
ADT, we are not concerned with time and space efficiency or any other implementation
details of the data structure. ADT is just a useful guideline to use and implement the data
type.

An ADT has two parts:

(a) Value definition


(b) Operation definition.

Value definition is again divided into two parts:

(1) Definition clause

(2) Condition clause

As the name suggests the definition clause states the contents of the data type and
condition clause defines any condition that applies to the data type. Definition clause is
mandatory while condition clause is optional. For example, an integer variable can
contain only integer values while a character variable can contain only a valid character
value.

To understand the above clauses let us consider the ADT representation of integer data
type. In the value definition the definition clause will state that it can contain any number
between minimum integer value and the maximum integer value allowed by the computer
while the condition clause will impose a restriction saying none of the values will have a
decimal point.

In operation definition, there are three parts:

(1) Function

(2) Precondition

(3) Postcondition

The function clause defines the role of the operation. If we consider the addition
operation in integers the function clause will state that two integers can be added using
this function .In general, precondition specifies any restrictions that must be satisfied
before the operation can be applied. This clause is optional. If we consider the division
operation on integers then the precondition will state that the divisor should not be zero.
So any call for divide operation, which does not satisfy this condition, will not give the
desired output.

Chapter 1
6 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

Precondition specifies any condition that may apply as a pre-requisite for the operation
definition. There are certain operations that can be carried out if certain conditions are
satisfied. For example, in case of division operation the divisor should never be equal to
zero. Only if this condition is satisfied the division operation is carried out. Hence, this
becomes a precondition. In that case & (ampersand) should be mentioned in the operation
definition.

Postcondition specifies what the operation does .One can say that it specifies the state
after the operation is performed. In the addition operation, the post condition will give the
addition of the two integers.

ADT

Value Definition Operation Definition

Definition Condition Function Pre Post


Clause Clause Condition Condition
Fig 1.7
Components of ADT

As an example, let us consider the representation of integer data type as an ADT. We will
consider only two operations addition and division.

Value definition

(1) Definition clause: The values must be in between the minimum and maximum values
specified for the particular computer.

(2) Condition clause: Values should not include decimal point.

Operations

(1) add(a,b)
Function: add the two integers a and b.
Precondition: no precondition.
Postcondition: output = a+b

(2) Div(a,b)
Function: Divide a by b.
Precondition: b != 0
Postcondition: output = a/b.

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 7
Introduction to Data Structures And Algorithms

There are two ways of implementing a data structure viz. static and dynamic. In static
implementation, the memory is allocated at the compile time. If there are more elements
than the specified memory then the program crashes. In dynamic implementation, the
memory is allocated as and when required during run time.

Any type of data structure will have certain basic operations to be performed on its data
like insert, delete, modify, sort, search etc depending on the requirement. These are the
entities that decide the representation of data and distinguish data structures from each
other.
Let us see why user defined data structures are essential. Consider a problem where we
need to create a list of elements. Any new element added to the list must be added at the
end of the list and whenever an element is retrieved, it should be the last element of the
list. One can compare this to a pile of plates kept on a table. Whenever one needs a plate,
the last one on the pile is taken and if a plate is to be added on the pile, it will be kept on
the top. The description wants us to implement a stack. Let us try to solve this problem
using arrays.
We will have to keep track of the index of the last element entered in the list. Initially it
will be set to –1.Whenever we insert an element into the list we will increment the index
and insert the value into the new index position .To remove an element, the value of
current index will be the output and the index will be decremented by one. In the above
representation, we have satisfied the insertion and deletion conditions.

Using arrays we could handle our data properly, but arrays do allow access to other
values in addition to the top most one. We can insert an element at the end of the list but
there is no way to ensure that insertion will be done only at the end. This is because array
as a data structure allows access to any of its values. At this point we can think of another
representation, a list of elements where one can add at the end, remove from the end and
elements other than the top one are not accessible. As already discussed this data
structure is called as STACK. The insertion operation is known as push and removal as
pop. You can try to write an ADT for stacks. We have covered stacks in detail in Chapter
3.

Another situation where we would like to create a data structure is while working with
complex numbers. The operations add, subtract division and multiplication will have to
be created as per the rules of complex numbers. The ADT for complex numbers is given
below. Only addition and multiplication operations are considered here, you can try to
write the remaining operations.
The ADT for complex numbers is as follows:

Value Definition:

Definition Clause: This data type has values a +ib where a and b are integers and i is
equal to under root of –1.

Condition Clause: i term should be present.

Chapter 1
8 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

Operations

1) add

Function: add, add two complex numbers a1 + ib1 and a2 + ib2.

Precondition: no Precondition.

Post condition: (a1 + a2) + i(b1 + b2).

2) multiply

Function: multiply two complex numbers a1 + ib1 and a2 + ib2.

Precondition: no Precondition.

Postcondition: a1a2 +ia1b2 + ib1a2 + i2b1b2.

i.e. a1a2 +ia1b2 + ib1a2 - b1b2.

1.3 Algorithms

In the last section, we considered the abstract form of data structures. Now it is also
essential to implement the abstract form in order to use the data type. Implementation is
nothing but writing functions for the various operations. Then these functions can be used
whenever required .In fact a library of data structures can be created. To write these
functions we will have to specify instructions step-by-step. When these instructions are
not language specific, it can be called as an algorithm. For example, the algorithm to add
two numbers is given below:

1) Input the first number and store in a variable.


2) Input the second number and store in a variable.
3) Add the two numbers and store the result in a variable.
4) Display the result.

Any given task can be performed in various ways. There is more than one algorithm to
implement a given operations .In this section we will see how to choose the best method
according to our need. This does not mean that the other methods are not useful. Every
method or algorithm has its own advantages and disadvantages regarding the resources
required. Due to this, it is essential to analyze the algorithms. This analysis helps us to
select a proper algorithm. There are various methods to analyze algorithms.

1.3.1 Analysis of Algorithms

By now the need of analyzing algorithms must be pretty clear. Two main factors on
which the performance of a program depends are the amount of computer memory

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 9
Introduction to Data Structures And Algorithms

consumed and the time required to execute it successfully. There are two ways in which
we can analyze the performance of an algorithm. One of them is to carry out experiments
with the algorithm by actually executing it, using various data sets and recording the
space and time required. This data can be analyzed to find the performance of an
algorithm. This method can be used only after successful implementation. Another
method, which can be used, is the analytical method. We can approximately find out the
space and time required before implementation.

1.3.2.1 Space Complexity

Space complexity of a program is the amount of memory required to execute a program


successfully. This estimation of space can be used to identify the amount of memory
required by the implementation. This information can be used while allocating memory
in multi-user computer systems or while executing it on a system with less memory
resources. In addition, we can find out the size of the largest problem that a program can
solve in a given environment.

Let us consider the various factors on which space complexity depends. You all must be
familiar with static allocation and dynamic allocation of memories. In static allocation,
the memory required by the program is known at compile time while in dynamic
allocation it can increase during execution. Programs using recursion need space
depending on the number of recursive calls made. Each recursive function needs stack
space to store local variables and formal parameters. Based on the above discussion we
can divide the space needed by a program into two parts:

(1) A static part, which is independent of the instance characteristic. This includes the
space required to store the code. Again, the space required to store the code is
compiler and machine dependent. Other components are constants, variables,
complex data types etc.

(2) A dynamic part, which consists of components whose memory requirements, depends
on the instance of the problem being solved. Dynamic memory allocations and
recursion are few components of this type. The factors, which can help us in
determining the size of the problem instance, are number of inputs, outputs etc.

Example

(a) Let us consider a small function, which adds all the elements in a given array, and
analyzes its space complexity.

int sum( int a[] , int n)


{
int value = 0;
for (int i = 0; i<n;i++)
value = value + a[i];
return value;
}

Chapter 1
10 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

In this function, we require space for a, i, value, n. All of them are static components and
hence require a fixed space. The total space required by this function would be equal to
the sum of all the above components.

(b) Next, let us consider the example to calculate factorial. We will write the function
using recursion.

int factorial ( int n)


{
if (n <= 1) return 1;
else return n * factorial (n-1);
}

In this function space required does not depend on the number of inputs but is a function
of n. the recursion depth will be at the most n and at least 1. Each recursive call will need
a stack space of two bytes to store the return address. Hence the memory required can be
calculated using the function 4* max {n, 1}.

1.3.2.2 Time Complexity

Time complexity of a program is the amount of time required to execute successfully.


Time can be useful in knowing the maximum time limit of the program to be executed. In
addition, we can test the response time of the program .It is very essential to have a good
response time for interactive applications like text editors etc.

Before analyzing an algorithm for time, we should know the machine and compiler on
which it is executed as compilation and execution time depends on them. If we consider a
conventional computer then we can assume that instructions of a program are carried out
one at a time and the major time is consumed by the operations carried out in the
algorithm.

There are two steps to be carried out during analysis of an algorithm. First step is to
determine the operations present in the algorithm. The various types of operations are
addition, subtraction, multiplication, division, comparison, assignment of values etc. All
the above operations take fixed time to complete i.e. their time is bound by a constant.
There are certain operations like comparison of strings, which are dependent on the
length of the string and hence require variable time. The second step is to determine data
sets, which cause the algorithm to exhibit the possible patterns of behavior. This step is
useful to find out the best, average and worst case behaviors.

There are two methods in which computing time can be analyzed viz. priori and
posteriori analysis. In priori analysis, we find an analytical function, which binds the
computing time of the algorithm. In a posteriori analysis, we work with the actual
statistics collected by execution of programs. We will consider how priori analysis can be
done. We have to find out the execution time required by each program statement. For
this we should know the number of time each program statement is executed and the
individual time taken for execution .The product of two will be the total execution time of

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 11
Introduction to Data Structures And Algorithms

each statement while sum of all these products will give the total time. Time required by
each program statement is computer and compiler dependent. So we can do priori
analysis only by finding out how many times each statement is executed i.e. the
frequency count.

Examples

Let us consider a few examples and calculate their frequency counts.

(a) a = b + c;

(b) for(i=0;i<n;i++)
a = b + c;

(c) for ( i=0; i<n; i++)


for(j=0; j<n j++)
a = b + c;

In example (a) the statement is executed once so frequency count is 1. In example (b) the
line is execute n times while in example (c) it is executed n2 times. These frequencies
give the order of magnitude. The order of magnitude of an algorithm is the sum of
frequencies of all its statements. There are various mathematical notations, which help us
in analysis using the order of magnitude. In the next section we will see how this order of
magnitude is used to determine the time complexity of an algorithm.

1.3.2.3. Asymptotic Notations

We have studied time and space complexity in the previous section. These two
complexities are used during the analysis of algorithms. Algorithm analysis helps us to
choose the best algorithm for a given application. In the previous section we saw how
time and space complexity is calculated. Time and space complexity is measured in terms
of asymptotic notations. For example let us consider a program which stores n elements.

void store( )
{
int i, n;
printf(“enter the number of elements “);
scanf(“%d”, &n);
for(i = 0; i < n; i++)
{

// store the elements

}
}

In the above program space is required to store the executable code and to store the ‘n’
number of elements. The memory that is required to store the executable code is static.
The memory required to store the ‘n’ elements depends on the value of ‘n’.

Chapter 1
12 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

The time required to execute the code also depends on the value of ‘n’. In the above
program we have two executable statements printf and scanf. Let us assume there are x
statements in the for loop. Then the time required to execute the program will be equal to
x * n + 2.

‘n’ is the instance characteristics. Asymptotic notation can describe the time and space
complexity accurately for large instance characteristics. Asymptotic notation will provide
us with upper and/or lower time and space bounds for a given algorithm. However, what
do you mean by upper and lower bounds? Upper bound will give the maximum time or
maximum space required for a program. Lower bound will give the minimum time /
space required.

Big Oh notation

Definition: f (n) = O(g(n)) (read as “f of n equals big oh of g of n”) iff (if and only
if) there exist two positive constants c and n0 such that | f(n)| <= c|g(n)| for all n
>= n0 .

Big oh notation provides an upper bound for the function f. Consider f(n) to be the
computing time of some algorithm where n is the number of inputs, outputs, their sum or
any one of them. When we say that an algorithm has computing time O(g(n)) we mean
that if the algorithm is run on any computer on the same type of data but for increasing
values of n, the resulting time will always be less than some constant time g(n).

Let us consider an example to understand the above concept

(1) Linear Functions: Consider f (n) = 5n + 2. When n is atleast 2, 5n + 2 <= 5n +


n <= 6n. So f(n) = O(n). Thus f(n) is bounded from above by a linear function. We
can arrive to the same conclusion in other ways. For example, 5n + 2 <= 10n for n>0.
Therefore we can also satisfy the definition of big oh by selecting c=10 and n0 equal to
any integer greater than zero.

(2) Quadratic Function: Suppose that f (n) = 5n2 + 4n + 2. We see that for n >= 2,
f (n) <= 5n2 + 5n. Now we note that for n>=5, 5n <= n2. Hence for n >= n0 = 5, f
(n) <= 5n2 + n2 = 11n2. Therefore f (n) = O (n2).

Omega Notation

Definition: f (n) = omega (g (n)) (read as “f of n is omega of g of n”) iff positive


constants c and n0exists such that f (n) >= g n0 for all n, n >= n0.

Omega notation provides the lower bound for function f. f(n) = omega(g(n)) means
f is atleast c times the function g except when n is smallest than n0.

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 13
Introduction to Data Structures And Algorithms

For example suppose we have a linear function f(n) = 3n + 2. Now 3n + 2 > 3n for
all n, hence f(n) = omega (n). If we consider a quadratic function f (n) = 5n2 + 2n
+ 2 then 5n2 + 2n +2 > 5n2 for n >= 0, hence we can say f (n0) = omega (n2).

Theta Notation

Definition: f(n0) = theta(g(n)) ( read as “f of n is theta of g of n”) if positive


constants c1 and c2 and an (n0) exists such that c1 g(n) <= f(n) <= c2 g(n) for all
n, n >=n0.

F (n) = theta(g(n)) means f lies between c1 times the function g and c2 times the
function g except possibly when n is smaller than (n0). Here c1 and c2 are positive
constants. Thus g is both a lower and upper bound for a constants factor c on the value of
f for all suitably large n.

1.4 Goals of Data Structures

Data structure is a collection of data elements whose organization is characterized by


accessing functions that are used to store and retrieve individual data elements. The study
of data structures involves two major goals. The first goal is to identify and develop
useful mathematical entities, operations and determine what classes of problems can be
solved by using these entities and operations. A matrix is one such mathematical entity
that is used to solve various problems. The second goal is to determine representations
for abstract entities and implement them. Stack is a good example of the second goal.

The first goal aims at using high level data types as a tool to solve problems, while the
second one tries to implement the data type using the already existing data types. Usually
it is impossible to implement a mathematical model completely using hardware and
software. For example Integers have infinite number of values but on computer it is
between some finite range. One should know what are the limitations associated with a
particular implementation. One can implement the same data type in many possible ways.
Each representation will have its own strength and weakness. One should know the
possible tradeoffs that are involved while working for a specific application.

Example

You have studied programming languages and are familiar with C. Array is one of the
invaluable data structures in C. It is made up of the existing data types in the language.
Let us see how array is built up in C. We will consider the single dimensional array.
Basically, an array is an ordered set of homogeneous elements. Every array has a lower
bound, which is the smallest index and upper bound, the largest index. Two operations
done on an array are extraction and storing. Let us consider the ADT for arrays.

Chapter 1
14 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

1) Value definition

i) Definition clause:

Arraytype (upper_bound, element_type)

The array will have elements equal to upper_bound and will contain elements of
element_type.

ii) Condition clause:

Upper_bound should be of integer type.

2) Operations

i) Retrieve (a, I)

Function: It retrieves an element from array placed at index, i. Returns an element _type
value.

Precondition: 0 <= I < upper_bound


Postcondition: output = a[I]

ii) Store (a, I, element)

Function: stores element which is element_type into array a at position i.

Precondition: 0 <= I < upper_bound


Postcondition: a [I] == element

Using basic data types, i.e. variables, we can carry out the implementation. A block of
memory capable of storing the required elements will be pointed by the array variable.
We will not move towards implementation details of array. This is just to give you an
idea how data structures can be built using basic data types.

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 15
Introduction to Data Structures And Algorithms

SELF ASSESSMENT

1.What do you understand by the term Abstract Data Type?

2. Differentiate between the Stacks and Queues on the basis of the storage of data.

3. Analyze algorithms on the basis of the space complexity factor.

4. The two parts of an algorithm are ________________ and _____________.

5. Differentiate between the inbuilt functions and the user defined functions.

6. Value Definition of algorithm is divided into two parts ____________ and


_____________.

Chapter 1
16 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

SUMMARY

Key points covered in this chapter are as follows:

♦ A computer program has data and a set of algorithms, which act on the data to
produce, required information.

♦ There are various ways in which data can be stored for a given problem. A data
structure is a collection of data elements whose organization is characterized by
accessing functions that are used to store and retrieve individual data elements.

♦ An algorithm is a logical sequence of discrete steps that describe a complete solution


to a given problem in a finite amount of time.

♦ A particular method of introducing the bit patterns stored in a computer is called a


data type or a formal description of the set of values that a variable of a given type
may take is called a data type.

♦ An ADT is the logical picture of a data type plus the specifications of the operations
required in creating and manipulating objects of this data type.

♦ Basic operations performed on any type of data structures are insert, modify, delete,
search, sort etc.

♦ An array is a list of elements in which each element is accessible via an index.

♦ There are various algorithms available to implement the same task. Hence it is
necessary to analyze algorithms.

♦ Performance of a program depends on two factors - space complexity and time


complexity.

♦ Space complexity of a program is the amount of memory required to execute the


program successfully.

♦ In static allocation memory required by a program is known at compile time whereas


in dynamic allocation it can increase during the execution.

♦ Time complexity of a program is the amount of time required to execute a program


successfully.

♦ The order of magnitude of an algorithm is the sum of the frequencies of all of its
statements.

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 17
Introduction to Data Structures And Algorithms

♦ Asymptotic notations calculate the approximate time and space requirements of an


algorithms

♦ There are various types of asymptotic notations viz. Big Oh, Omega, Theta etc.

♦ The major goals involved in the study of data structure are :

z To identify and develop useful mathematical entities and operations and to


determine what classes of problems can be solved using these entities and
operations.

z Determine representations for abstract entities and implement them

Chapter 1
18 Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority
Introduction to Data Structures And Algorithms

LAB EXERCISE
1. Write a program using two-dimensional arrays to store the names of 20 students.

Implement the following functions:


• Enter a name.
• Delete a name.
• Display a name.
• Display all the names.
• Modify a name.

2. Write a program using structures to store the record of ten employees. The record
contains the following information:
• Employee name
• Employee number
• Salary

Implement the following functions

• Enter a record.
• Delete a record.
• Display a record.
• Display all the record.
• Modify a record.

3. Implement problem number (1) using array of pointers.

Chapter 1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority. 19