You are on page 1of 12

SRM Institute of Science And Technology

Kattankulathur, Chennai

V20UDS202 - DATA STRUCTURES AND ALGORITHMS

Unit 1 – Week 1

Introduction

In the modern world, Data and its information is an essential part, and various
implementations are being made to store in different ways. Data are just a collection of facts
and figures, or you can say data are values or a set of values that are in a particular format. A
data item refers to a single set of values. Data items are then further categorized into sub-
items, which are the group of items that are not being called a plain elementary form of items.
Let us take an example where the name of the student may be divided into three sub-items,
namely: first name, middle name, and last name. But the ID that is assigned to a student
would typically be considered as a single item.

In the example mentioned above, such as ID, Age, Gender, First, Middle, Last, Street, Area,
etc. are elementary data items, whereas (Name, Address) is group data items.

Data Structure can be defined as the group of data elements which provides an efficient way
of storing and organising data in the computer so that it can be used efficiently.

A well-designed data structure allows a variety of critical operations to be performed, using


as few resources, both execution time and memory space, as possible. Data structure
introduction refers to a scheme for organizing data, or in other words it is an arrangement of
data in computer's memory in such a way that it could make the data quickly available to the
processor for required calculations.

A data structure should be seen as a logical concept that must address two fundamental
concerns.

1. First, how the data will be stored, and


2. Second, what operations will be performed on it. As data structure is a scheme for data
organization so the functional definition of a data structure should be independent of its
implementation.

The functional definition of a data structure is known as ADT (Abstract Data Type)
which is independent of implementation. The way in which the data is organized affects the
performance of a program for different tasks. Computer programmers decide which data
structures to use based on the nature of the data and the processes that need to be performed
on that data.

Some examples of Data Structures are arrays, Linked List, Stack, Queue, etc. Data
Structures are widely used in almost every aspect of Computer Science i.e. Operating System,
Compiler Design, Artificial intelligence, Graphics and many more.

Data Structures are the main part of many computer science algorithms as they enable
the programmers to handle the data in an efficient way. It plays a vital role in enhancing the
performance of a software or a program as the main function of the software is to store and
retrieve the user's data as fast as possible.

The data structure is not any programming language like C, C++, java, etc. It is a set
of algorithms that we can use in any programming language to structure the data in the
memory.

Each of the data structures has a different special way of organizing data so we
choose the data structure based on the requirement

Need of Data Structures

As applications are getting complexed and amount of data is increasing day by day, the
following are the problems encountered:

Processor speed: To handle very large amount of data, high speed processing is required, but
as the data is growing day by day to the billions of files per entity, processor may fail to deal
with that much amount of data.

Data Search: Consider an inventory size of 106 items in a store, If our application needs to
search for a particular item, it needs to traverse 106 items every time, results in slowing down
the search process.

Multiple requests: If thousands of users are searching the data simultaneously on a web
server, then there are the chances that a very large server can be failed during that process

In order to solve the above problems, data structures are used. Data is organized to form a
data structure in such a way that all items are not required to be searched and required data
can be searched instantly.

Advantages of Data Structures

Efficiency: Efficiency of a program depends upon the choice of data structures. For example:
suppose, we have some data and we need to perform the search for a particular record. In that
case, if we organize our data in an array, we will have to search sequentially element by
element. hence, using array may not be very efficient here. There are better data structures
which can make the search process efficient like ordered array, binary search tree or hash
tables.

Reusability: Data structures are reusable, i.e. once we have implemented a particular data
structure, we can use it at any other place. Implementation of data structures can be compiled
into libraries which can be used by different clients.

Abstraction: Data structure is specified by the ADT which provides a level of abstraction.
The client program uses the data structure through interface only, without getting into the
implementation details.

Applications of Data Structures:

 Storing list of data elements belonging to same data type


 Auxiliary storage for other data structures
 Implement dynamic memory management functions of operating system.
 Polynomial implementation for mathematical operations
 Representing networks and routes in communication, transportation and travel
applications
 Routes in GPS
 Interconnections in social networks and other network based applications
 Web pages can be accessed using the previous and the next URL links which
are linked using linked list.

Data Type:
A data type is an attribute of data which tells the compiler (or interpreter) how the
programmer intends to use the data.
Classification of Data Type:

 Primitive: basic building block (boolean, integer, float, char etc.)


 Composite: any data type (struct, array, string etc.) composed of primitives or
composite types.
 Abstract: data type that is defined by its behaviour (tuple, set, stack, queue,
graph etc).

Primitive Data Type:


The primitive data types are the predefined data types. They specify the size and type of
any standard values. There are 8 primitive data types namely byte, short, int, long, float,
double, char and boolean.
When a primitive data type is stored, it is the stack that the values will be assigned. When a
variable is copied then another copy of the variable is created and changes made to the
copied variable will not reflect changes in the original variable.
Composite Data Type:
A composite data type or compound data type is any data type which can be constructed in
a program using the programming language's primitive data types and other composite types.
It is sometimes called a structure or aggregate data type.
For example, an array of Integer elements is not of the Integer data type.

Abstract Data Type:


Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a
set of value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not how
these operations will be implemented.
It is called “abstract” because it gives an implementation-independent view. The process of
providing only the essentials and hiding the details is known as abstraction.
The user of data type does not need to know how that data type is implemented, for
example, we have been using Primitive values like int, float, char data types only with the
knowledge that these data type can operate and be performed on without any idea of how
they are implemented.
Problem Solving:
Solving problems is the core of computer science. Programmers must first understand how a
human solves a problem, then understand how to translate this "algorithm" into something a
computer can do, and finally how to "write" the specific syntax (required by a computer) to
get the job done. It is sometimes the case that a machine will solve a problem in a completely
different way than a human.

In order to solve a problem on a computer you must:

1. Know how to represent the information (data) describing the problem.


2. Determine the steps to transform the information from one representation into
another.

Program:
A program is a list of instructions or program statements composed in such a way as to
enable a computer to solve a problem.
The problem to be solved is broken down into successively smaller parts. These parts should
form a well defined structure, the large complex problem at the top the small easy to handle
problems at the bottom.
The following are the sequence of steps to form a layout or algorithm for solving the
problem:
Problem analysis.
The context of the proposed investigation must be established to provide the proper
motivation for the design of a computer program. The designer must fully recognize the need
and must develop an understanding of the nature of the problem to be solved.

Problem statement.
Develop a detailed statement of the mathematical problem to be solved with a computer
program.

Processing scheme
Define the inputs required and the outputs to be produced by the program.

Algorithm
Design the step-by-step procedure using the top-down design process that decomposes the
overall problem into subordinate problems. The subtasks to solve the latter are refined by
designing an itemized list of steps to be programmed. This list of tasks is the structure plan; it
is written in pseudocode, i.e. a combination of English, mathematics and anticipated
commands. The goal is to design a plan that is understandable and easily translated into a
computer language.

Program algorithm
Translate or convert the algorithm into a computer language and debug the syntax errors until
the tool executes successfully.
Evaluation
Test all of the options and conduct a validation study of the computer program, e.g. compare
results with other programs that do similar tasks, compare with experimental data if
appropriate, and compare with theoretical predictions based on theoretical methodology
related to the problems to be solved by the computer program. The objective is to determine
that the subtasks and the overall program are correct and accurate. The additional debugging
in this step is to find and correct logicalerrors (e.g. mistyping of expressions by putting a +
sign where a − sign was supposed to be placed), and run-timeerrors that may occur after the
program successfully executes (e.g. cases where division by 0 unintentionally occurs).

Application
Solve the problems the program was designed to solve. If the program is well designed and
useful it could be saved in your work directory (i.e. in your user-developed toolbox) for
future use.

DEFINING THE PROBLEM

To form the program specification. Which includes:


Input data
Processing that should take place
Format of the output report
User interface
Handling of job by individual or team

DESIGNING THE PROBLEM

Program design begins by focusing on the main goal that the program is going to achieve and
breaking the program into manageable components each of which contribute to this goal.
Approach program design is called top down program design or modular programming.

PROGRAM DESIGN TOOLS

o Structure
o charts
o Algorithms
o Flowcharts
o Pseudocodes

CODING THE PROBLEM

Coding the program involves translating the algorithm into specific program language
instructions. While writing the code, prefer to use well defined control structures. technique
of programming using only well-defined control structures is known as structured
programming.

TESTING AND DEBUGGING THE PROBLEM

Programmer must find and correct logical errors by carefully examining the program output
for a set of data for which results are already known, such type of data is known as test data.
If the software developed is a complex one and consists of large number of modules then
testing is done unit testing, system testing. Syntax errors and logical errors are collectively
known as bugs. The process of identifying and eliminating these errors is known as
debugging

DOCUMENTING THE PROGRAM

After testing, the software is almost complete. The structure chart, pseudo codes, flow chart
developed during the design phase become documentation for others who are associated with
the software project. In addition more documentation done as the program are being coded
such as list of variable names and definition, description of files that the program need to be
work with and the format of the output that the program produces.

IMPLEMENTING AND MAINTAINING THE PROGRAM

In the final phase, the program is installed at the user’s site. Here also, the program is kept
under watch till the user gives green signal to it. User may discover the errors that were not
caught in the testing phase. Even after the software project is complete, it needs to be
maintained and evaluated regularly. In program maintenance, the programming team fixes
program errors that user discover during day-to-day use.

INTRODUCTION TO ALGORITHM

An algorithm is a finite set of steps defining the solution of a particular problem.

Every algorithm must satisfy the following criteria:

Input: There are zero or more values which are externally supplied.
Output: At least one value is produced
Definiteness: Each step must be clear and unambiguous.
Finiteness: Must terminate after a finite number of steps.
Effectiveness: Each step must be definite and must be feasible.

EXAMPLE
Given an one dimensional array a[0,n-1] of real values, and we want to find the location of its
largest element.
Algorithm

Let a[0:n-1] be one dimensional array with n real values. This algorithm finds the location loc
of its largest element. The variable i is used as index variable, max is temporary variable to
store the current largest element.

ALGORITHM DESCRIPTION

Algorithm consists of two parts:


Describe the input data, the purpose of the algo. Identifies the variable used in algorithms.
Composed of sequence of instructions that lead to the solution of the problem.

SUMMARY OF CONVENTION USED FOR PRESENTING ALGORITHM

Comments: which explain the purpose of instruction.


//this is a sample comment
Variable Names: we use italicized lower case letters such as max, loc etc.
Assignment statement: the assignment statement will use the notation asset max=ai
Input/output: data may be input and assigned to variables by means of a read statements with
the following format
read: variable list
Output is done by write statement
write: message and/ or variable list

RECURSION:
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function. Using recursive algorithm, certain
problems can be solved quite easily.

The recursion continues until some condition is met to prevent it.


Rules of recursion:
1. Base cases. You must always have some base cases, which can be solved without
recursion.
2. Making progress. For the cases that are to be solved recursively, the recursive call must
always be to a case that makes progress toward a base case.
3. Design rule. Assume that all the recursive calls work.
4. Compound interest rule. Never duplicate work by solving the same instance of a problem
in separate recursive calls.
Difference between Recursion and Iteration:
1. A function is said to be recursive if it calls itself again and again within its body whereas
iterative functions are loop based imperative functions.
2. Recursion uses stack whereas iteration does not use stack.
3. Recursion uses more memory than iteration as its concept is based on stacks.
4. Recursion is comparatively slower than iteration due to overhead condition of maintaining
stacks. 5. Recursion makes code smaller and iteration makes code longer.
6. Iteration terminates when the loop-continuation condition fails whereas recursion
terminates when a base case is recognized.
7. While using recursion multiple activation records are created on stack for each call where
as in iteration everything is done in one activation record.
8. Infinite recursion can crash the system whereas infinite looping uses CPU cycles
repeatedly.
9. Recursion uses selection structure whereas iteration uses repetetion structure.
Towers of Hanoi
Input: The aim of the tower of Hanoi problem is to move the initial n different sized disks
from needle A to needle C using a temporary needle B. The rule is that no larger disk is to be
placed above the smaller disk in any of the needle while moving or at any time, and only the
top of the disk is to be moved at a time from any needle to any needle.
Output:
1. If n=1, move the single disk from A to C and return,
2. If n>1, move the top n-1 disks from A to B using C as temporary.
3. Move the remaining disk from A to C.
4. Move the n-1 disk disks from B to C, using A as temporary.
Example : Program to find the Sum of Natural Numbers
#include <stdio.h>
int sum(int n);
int main() {
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
return 0;
}
int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
Example: Program to find factorial of the Number
#include<stdio.h>
long Factorial(int);
void main()
{
int num;
long fact;
printf("\n\tEnter any positive number : ");
scanf("%d",&num);
fact = Factorial(num);
printf("\n\tThe Factorial of %d is %ld",num,fact);
}
long Factorial(int num)
{
if (num == 1)
return 1;
else
return num*Factorial(num-1);
}
Output :
Enter any positive number :
The Factorial of 10 is 3628800

Advantages of recursion

1. The code may be easier to write.


2. To solve such problems which are naturally recursive such as tower of Hanoi.
3. Reduce unnecessary calling of function.
4. Extremely useful when applying the same solution.
5. Recursion reduce the length of code.
6. It is very useful in solving the data structure problem.
7. Stacks evolutions and infix, prefix, postfix evaluations etc.

Disadvantages of recursion

1. Recursive functions are generally slower than non-recursive function.


2. It may require a lot of memory space to hold intermediate results on the system stacks.
3. Hard to analyze or understand the code.
4. It is not more efficient in terms of space and time complexity.
5. The computer may run out of memory if the recursive calls are not properly checked.

References:

1. https://www.javatpoint.com/data-structure-introduction

2. https://www.freecodecamp.org/news/how-to-think-like-a-programmer-lessons-in-
problem-solving-d1d8bf1de7d2/

3. https://csveda.com/data-structure/applications-of-data-structures/

4. https://www.geeksforgeeks.org/real-time-application-of-data-structures/

5. https://web.chem.ox.ac.uk/fortran/programs.html#:~:text=A%20program%20is%20a
%20list,down%20into%20successively%20smaller%20parts.

You might also like