Subject Name: Data Structure II Subject Code: CS-3004 Semester: 3
Subject Name: Data Structure II Subject Code: CS-3004 Semester: 3
Unit -1
INTRODUCTION:
Computer Science is the study of data, its representation and transformation by
Computer. For every data object, we consider the class of operations to be performed and then the
way to represent the object so that these operations may be efficiently carried out. We require two
techniques for this:
- Devise alternative forms of data representation
- Analyse the algorithm which operates on the structure.
These are several terms involved above which we need to know carefully before we
proceed. These include data structure, data type and data representation.
A data type is a term which refers to the kinds of data that variables may hold. With every
programming language there is a set of built-in data types. This means that the language allows
variables to name data of that type and provides a set of operations which meaningfully manipulates
these variables. Some data types are easy to provide because they are built-i i to the o puter s
machine language instruction set, such as integer, character etc. Other data types require
considerably more efficient to implement. In some languages, these are features which allow one to
construct combinations of the built-in types like stru tures i C . Ho e er, it is e essar to ha e
such mechanism to create the new complex data types which are not provided by the programming
language. The new type also must be meaningful for manipulations. Such meaningful data types are
referred as abstract data type.
DATA STRUCTURE:
Data Structures are the programmatic way of storing data so that data can be used efficiently.
Almost every enterprise application uses various types of data structures in one or the other way.
Thus, a data structure is the portion of memory allotted for a model, in which the required data can
be arranged in a proper fashion.
Apart from the four operations mentioned above, there are two more operations occasionally
performed on data structures. They are:
(a) Sorting :- Sorting means arranging the data in a particular order.
(b) Merging : - Merging means joining two lists.
A data structure can be broadly classified into (i) Built-in/Primitive data structure
(ii) User Defined/Non-primitive data structure
A list is an ordered list, which consists of different data items connected by means of a link or
pointer. This type of list is also called a linked list. A linked list may be a single list or double linked
list.
Single linked list: - A single linked list is used to traverse among the nodes in one
direction.
Double linked list: - A double linked list is used to traverse among the nodes in both the
directions.
A linked list is normally used to represent any data used in word-processing applications, also
applied in different DBMS packages.
Stack: - It is also called as last-in-first-out (LIFO) system. It is a linear list in which insertion
A list has two subsets. They are: -
insertion takes place at once end and deletion takes place at other end. It is generally
used to schedule a job in operating systems and networks.
Characterstic Description
Linear In Linear data structures,the data items are arranged in a linear sequence.
Example: Array
Non-Linear In Non-Linear data structures,the data items are not in sequence. Example: Tree, Graph
Homogeneous In homogeneous data structures,all the elements are of same type. Example: Array
Non- In Non-Homogeneous data structure, the elements may or may not be of the same type.
Homogeneous Example: Structures
Static Static data structures are those whose sizes and structures associated memory locations
are fixed, at compile time. Example: Array
Dynamic Dynamic structures are those which expands or shrinks depending upon the program
need and its execution. Also, their associated memory locations changes.
Example: Linked List created using pointers
PROGRAM DESIGN:
Every computer program is built from components, data, and control. For a single-user application
(used by one person at a time), which normally reads data, saves it in a data structure, computes on
the data, and writes the results, there is a standard way of organizing the component structure, data
structure, and control structure:
1. First, design the program's component structure with three components, organized in
a model-view-controller pattern.
2. Next, decide what form of data structure (array, table, set, list, tree, etc.) will hold the
program's data. The data structure will be inserted in the program's model component.
3. Then, write the algorithm that defines the execution steps- the control structure. The
algorithm will be placed inside the program's controller.
4. Determine the form of input and output (disk file, typed text in a command window, dialogs,
a graphical-use interface, etc.) that the program uses. This will be embedded in the
program's view.
Good, logical programming is developed through good pre-code planning and organization. This is
assisted by the use of pseudocode and program flowcharts.1)Pseudo Code 2)Flow Chart
1)Pseudo Code: Consists of natural language-like statements that precisely describe the steps
of an algorithm or program.
Statements describe actions
Focuses on the logic of the algorithm or program.
Avoids language-specific elements.
Written at a level so that the desired programming code can be generated almost
automatically from each statement.
Steps are numbered. Subordinate numbers and/or indentation are used for dependent
statements in selection and repetition structures
Example:
1. Enter Limit
2. Set Number = 0.
3. Set Sum = 0.
4. Repeat the following:
a. If Sum > Limit, terminate the repitition, otherwise.
b. Increment Number by one.
c. Add Number to Sum and set equal to Sum.
5. Print Number and Sum.
2)Flow Chart:. Flowcharts are written with program flow from the top of a page to the bottom. Each
command is placed in a box of the appropriate shape, and arrows are used to direct program flow.
The following shapes are often used in flowcharts:
COMPLEXITIES:
In computer science, the time complexity of an algorithm quantifies the amount of time taken by
an algorithm to run as a function of the length of the string representing the input. The
time complexity of an algorithm is commonly expressed using big O notation, which excludes
coefficients and lower order terms.Complexities can be mesaure in 2 ways, space and time.
There may be many algorithms devised for an application and we must analyse and validate
the algorithms to judge the suitable one.
To judge an algorithm the most important factors is to have a direct relationship to the
performance of the algorithm. These have to do with their computing time & storage requirements (
referred as Time complexity & Space complexity).
Space Complexity:
The space complexity of an algorithm is the amount of memory it needs to run.
Time Complexity:
The time taken by a program is the sum of the compiled time & the run time. The time complexity of
an algorithm is given by the number of steps taken by the algorithm to compute the function it was
written for.
Algorithm : a procedure for solving a mathematical problem in a finite number of steps that
frequently involves repetition of an operation; broadly : a step-by-step procedure for solving a
problem or accomplishing some end especially by a computer.
TIME COMPLEXITY:
In computer science, the time complexity of an algorithm quantifies the amount of time taken by an
algorithm to run as a function of the length of the string representing the input. The time
complexity of an algorithm is commonly expressed using big O notation, which excludes coefficients
and lower order terms.
Time complexity is expressed via Asymptotic Notations.
ORDER OF GROWTH:
Order of growth in algorithm means how the time for computation increases when you increase the
input size. It really matters when your input size is very large.
Order of growth provides a useful indication of how we may expect the behavior of the process to
change as we change the size of the problem. Depending on the algorithm, the behaviour changes.
So, this is one of the most important things that we have to care when we design an algorithm for
some given problem.
There are different notations to measure it and the most important one is Big O which gives you the
worst case time complexity.
ASYMPTOTIC NOTATIONS:
Following are the commonly used asymptotic notations to calculate the running time complexity of
an algorithm.
a Ο Notatio
Ω Notatio
θ Notatio
Big Oh Notatio , Ο
The otatio Ο is the for al a to e press the upper ou d of a algorith 's ru i g ti e. It
measures the worst case time complexity or the longest amount of time an algorithm can possibly
take to complete.
O ega Notatio , Ω
The otatio Ω is the for al a to e press the lo er ou d of a algorith 's ru i g ti e. It
measures the best case time complexity or the best amount of time an algorithm can possibly take to
complete.
Theta Notatio , θ
The otatio θ is the for al a to e press oth the lo er ou d a d the upper ou d of a
algorithm's running time. It is represented as follo s −