Kumaraguru: Class Notes
Kumaraguru: Class Notes
COLLEGE OF TECHNOLOGY
CLASS NOTES
Material No: I-1
Name of Teacher / Designation : N.Jayakanthan
Subject Code / Subject : P15CAT103-Data structures
Course / Branch : MCA
Topics : Introduction
Duration : ______1_____Hours.
_______________________________________________________________
STEP 1: INTRODUCTION
(1) LEARNING OBJECTIVES
The students are able to know the basics of data structures
After the session, the student should be able to:
Learn fundamentals of the algorithm
STEP 2: ACQUISITION
An algorithm is a sequence of unambiguous instructions for solving
a problem in a finite amount of time.
The algorithm must solve the problem irrespective whatever be the instance (but from the
particular domain) is, and too within the time bound.
Another important property of an algorithm is it must terminate within finite number of
steps.
Introduction
Data Structure is a way to organized data in such a way that it can be used efficiently.
This tutorial explains basic terms related to data structure.
Data Type
Data type is way to classify various types of data such as integer, string etc. which
determines the values that can be used with the corresponding type of data, the type of
operations that can be performed on the corresponding type of data. Data type of two
types −
Integers
Boolean (true, false)
Floating (Decimal numbers)
Character and Strings
List
Array
Stack
Queue
Basic Operations
The data in the data structures are processed by certain operations. The particular data
structure chosen largely depends on the frequency of the operation that needs to be
performed on the data structure.
Traversing
Searching
Insertion
Deletion
Sorting
Merging
STEP 3: PRACTICE/TESTING
1. Define Data structure
A data structure is a specialized format for organizing and storing data. General data
structure types include the array, the file, the record, the table, the tree, and so on. Any
data structure is designed to organize data to suit a specific purpose so that it can be
accessed and worked with in appropriate ways.
CLASS NOTES
Material No: 1-2
Name of Teacher / Designation : N.Jayakanthan
Subject Code / Subject : P15CAT103-Data structures
Course / Branch :MCA
Topics : Primitive Data and non primitive data
Structures, Analysis of algorithm and
notations.
Duration : ______1_____Hours.
Resources required to handle the class : Black board & OHP projector
_______________________________________________________________
STEP 1: INTRODUCTION
(2) LEARNING OBJECTIVES
The students will understand the primitive data structures, Analysis of
algorithm and notations.
STEP 2: ACQUISITION
Primitive Data Type,
A primitive data type is one that fits the base architecture of the underlying computer
such as int, float, and pointer, and all of the variations, thereof such as char short long
unsigned float double and etc, are primitive data type.
Primitive data are only single values, they have not special capabilities.
The examples of Primitive data types are given byte, short, int, long, float, double, char
etc.
The integer reals, logic data character data pointer and reference are primitive data
structures data structure that normally are directly operated upon by machine level
instructions are known as primitive structure and data type.
The data type that are derived from primary data types are known as non-primitive data
type.
The non-primitive data types are used to store the group of values.
Analyzing an Algorithm
We usually want our algorithms to possess several qualities. After correctness, by far the
most important is efficiency. In fact, there are two kinds of algorithm efficiency: time
efficiency, indicating how fast the algorithm runs, and space efficiency, indicating how
much extra memory it uses
There are situations, of course, where the choice of a parameter indicating an input size
does matter. One such example is computing the product of two n × n matrices.
Orders of Growth
A difference in running times on small inputs is not what really distinguishes efficient
algorithms from inefficient ones.
For example, the greatest common divisor of two small numbers, it is not immediately
clear how much more efficient Euclid’s algorithm is compared to the other two
algorithms.
Also note that although specific values of such a count depend, of course, on the
logarithm’s base, the formula
loga n = loga b logb n
The best-case efficiency of an algorithm is its efficiency for the best-case input of size n,
which is an input (or inputs) of size n for which the algorithm runs the fastest among all
possible inputs of that size. Accordingly, we can analyze the bestcase efficiency as
follows. First, we determine the kind of inputs for which the count C(n) will be the
smallest among all possible inputs of size n. (Note that the best case does not mean the
smallest input; it means the input of size n for which the algorithm runs the fastest.)
This is the information that the average-case efficiency seeks to provide. To analyze the
algorithm’s averagecase efficiency, we must make some assumptions about possible
inputs of size n.
Asymptotic Notation
Suppose we are considering two algorithms, A and B, for solving a given problem.
Furthermore, let us say that we have done a careful analysis of the running times of each
of the algorithms and determined them to be and , respectively, where n is a measure of
the problem size. Then it should be a fairly simple matter to compare the two functions
and to determine which algorithm is the best!
But is it really that simple? What exactly does it mean for one function, say , to be better
than another function, ? One possibility arises if we know the problem size a priori. For
example, suppose the problem size is and . Then clearly algorithm A is better than
algorithm B for problem size .
In the general case, we have no a priori knowledge of the problem size. However, if it can
be shown, say, that for all , then algorithm A is better than algorithm B regardless of the
problem size.
Unfortunately, we usually don't know the problem size beforehand, nor is it true that one
of the functions is less than or equal the other over the entire range of problem sizes. In
this case, we consider the asymptotic behavior of the two functions for very large
problem sizes.
Definition (Omega) Consider a function f(n) which is non-negative for all integers .
We say that ``f(n) is omega g(n),'' which we write , if there exists an integer and a
constant c>0 such that for all integers , .
The definition of omega is almost identical to that of big oh. The only difference is in the
comparison--for big oh it is ; for omega, it is . All of the same conventions and caveats
apply to omega as they do to big oh
Definition (Theta) Consider a function f(n) which is non-negative for all integers . We
say that ``f(n) is theta g(n),'' which we write , if and only if f(n) is O(g(n)) and f(n) is .
Little oh notation represents a kind of loose asymptotic bound in the sense that if we are
given that f(n)=o(g(n)), then we know that g(n) is an asymptotic upper bound since
f(n)=O(g(n)), but g(n) is not an asymptotic lower bound since f(n)=O(g(n)) and implies
that .
For example, consider the function f(n)=n+1. Clearly, . Clearly too, , since not matter
what c we choose, for large enough n, . Thus, we may write .
The previous chapter presents a detailed model of the computer which involves a number
of different timing parameters-- , , , , , , , , , , and . We show that keeping track of the
details is messy and tiresome. So we simplify the model by measuring time in clock
cycles, and by assuming that each of the parameters is equal to one cycle. Nevertheless,
keeping track of and carefully counting all the cycles is still a tedious task.
In this chapter we introduce the notion of asymptotic bounds, principally big oh, and
examine the properties of such bounds. As it turns out, the rules for computing and
manipulating big oh expressions greatly simplify the analysis of the running time of a
program when all we are interested in is its asymptotic behavior.
For example, consider the analysis of the running time of Program , which is just
Program again, an algorithm to evaluate a polynomial using Horner's rule.
Nonrecursive Algorithms
Example: 1.1 Finding the largest element in an array.
Algorithm
Find a closed-form formula for the count or, at the very least, establish its order
of growth.
Analysis
Note here that the frequently executed instruction (comparison) is not inside the loop,
rather it determines whether the loop’s body to be executed. Since the number of times
the comparison will be executed is larger than the number of repetitions of the loop’s
body by exactly 1, the choice is not that important. The important thing here is the loop’s
variable generally takes only a few values between its lower and upper bounds, therefore,
we have to use alternate way of computing the number of times the body of the loop is
executed. Since the value of n is about halved on each iteration, the answer should be log
n. That is, the number of times the value of n is compared is log n + 1, and therefore the
complexity of the algorithm is _(log n). Another important factor to consider here is the
size of the input, which is n. Note that we are giving only one positive integer n as an
input, however the size of the input is n, As value of n varies, the complexity also varies
as related to this n, and hence the size of the input is n.
STEP 3: PRACTICE/TESTING
1. What is primitive data type?
The primitive data types are the basic data typesthat are available in most of the
programming languages. The primitive data types are used to represent single
values. Integer: