Professional Documents
Culture Documents
Data Struct1
Data Struct1
CHAPTER-1
DEPARTMENT OF MCA
Fig-1
Fig-2
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
PRIMITIVE DATA
STRUCTURE
NON-PRIMITIVE
DS
INTEGER
NONLINEAR
LINEAR
FLOAT
ARRAYS
TREES
STACKS
DOUBLE
CHARACTER
GRAPHS
QUEUES
POINTERS
STRINGS
LINKED LIST
Overview of Chapter-1:
1. System Life Cycle
2. Pointers and Dynamic Memory Allocation
3. Algorithm Specification
4. Data Abstraction
5. Performance Analysis
6. Performance Measurement
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Requirements
Analysis.
Design
Refinement and Coding
Verification
1) Requirements
All large programming projects begin with a set of
specifications that define the purpose of the projects.
These requirements describe the information required to develop
the project.
2) Analysis: bottom-up vs. top-down
In this phase we begin to break the problem into two manageable
phases.
There are two approaches to analysis:
Bottom-Up
Top-Down
Bottom-Up Approach:
o The Bottom-up approach is an older approach which gives
early emphasis on coding.
o Since the programmer does not have a master plan for the
project, the resulting program may have many error
ridden segments.
Top-Down Approach:
o This approach divides the problem in to manageable
segments.
o This technique generates diagrams that are used to
design the system.
o Frequently several alternate solutions to the
programming problem are developed and compared during
this phase.
3) Design:
Lecturer: Syed Khutubuddin Ahmed
Contact-Email: khutub27@gmail.com
SEM-II
o
o
o
o
CHAPTER-1
DEPARTMENT OF MCA
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
2. Pointers in C:
Pointer is a special variable which is used to store the
address of other variables.
int i,
int *pi;
pi = &i;
// declare pointer
// assign address to pointer
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
1. malloc():
This function is used to allocate a block of memory. If the
memory is not available then it returns null.
Syntax:
pointer ptr;
ptr = (data type *) malloc(size);
Example:
int *p;
p=(int *) malloc(10*size of(int));
Ex-2: cptr = (char *) malloc (10);
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
void main()
{
int *p, *table;
int size;
printf("what is the size of table\n");
scanf("%d",&size);
if((table = (int *) malloc (size * sizeof(int))) == NULL)
{
printf("No space available \n");
exit(1);
}
printf("\n input table values \n");
for(p=table; p<table+size; p++)
{
scanf("%d",p);
printf("%d is stored in address %u \n", *p, p);
}
}
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
2. calloc():
This function is used to allocate multiple block of memory.
Usually calloc() is used to allocate memory to array during run
time. If the memory is not available it returns null.
Syntax:
pointer *ptr;
ptr = (data type *) calloc(n,size);
n=size required
Example:
int p*;
p=(int *) calloc(10,size of(int));
Following segment
variables.
program
allocates
space
for
structure
strcut student
{
char name[25];
float age;
long int id_num;
};
typedef struct student record;
record *st_ptr;
int class_size = 30;
st_ptr = (record *) calloc (class_size, sizeof(record));
. . .
. . .
Contact-Email: khutub27@gmail.com
SEM-II
3.
CHAPTER-1
DEPARTMENT OF MCA
realloc():
This function is used to reallocate the memory either by
increasing or by decreasing. If the memory is not available
then it returns null.
Syntax:
pointer ptr;
ptr = realloc(new_size);
Example:
int p*;
p=realloc(p,10*size of(int));
4. free():
This function is used to free the memory allocated during run
time. while realising the memory the pointer must be
dynamically allocated.
Syntax:
pointer ptr;
free(ptr);
Example:
int P*;
free(p);
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
3. Algorithm Specification
Defn: an Algorithm is a sequence of unambiguous instructions for
solving a problem, i.e., for obtaining a required output for any
legitimate input in a finite amount time.
Criteria or Characteristics of algorithms:
Input: zero or more quantities that are externally supplied
Output: at least one quantity is produced
Definiteness: clear and unambiguous
Finiteness: terminate after a finite number of steps
Effectiveness: the algorithm must terminate after a finite
sequence of instructions.
Representation of Algorithm:
A natural language, like English or Chinese.
A graphic, like flowcharts.
A computer language, like C.
Lecturer: Syed Khutubuddin Ahmed
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
[0]
[1]
[2]
[3]
[4]
30
10
50
40
20
10
30
50
40
20
10
20
50
40
30
10
20
30
40
50
10
20
30
40
50
for(i=0;i<n;i++)
{
min = i;
for(j=i+1; j<n; j++)
{
if(a[j]<a[min])
min = j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("Sorted Array is\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
[1]
[2]
[3]
[4]
[5]
[6]
14
26
30
43
50
52
Contact-Email: khutub27@gmail.com
10
SEM-II
Search No = 43
Low
CHAPTER-1
DEPARTMENT OF MCA
Search No = 18
High
Mid
Compare
Low
30 < 43
30 > 18
50 > 43
14 < 18
43 == 43
26 > 18
High
Mid
Compare
Contact-Email: khutub27@gmail.com
11
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Recursive algorithms
Beginning programmer view a function as something that is
invoked (called) by another function
It executes its code and then returns control to the calling
function.
This perspective ignores the fact that functions can call
themselves (direct recursion).
They may call other functions that invoke the calling function
again (indirect recursion).
Recursive function is
Extremely powerful
Frequently allow us to express an otherwise complex
process in very clear term
Note:
We should express a recursive algorithm when the problem
itself is defined recursively.
: return middle;
: return binarysearch(list, searchnum, left, middle-1);
-1;
Contact-Email: khutub27@gmail.com
12
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
4. Data Abstraction:
Data Type
A data type is a collection of objects and a set of operations
that act on those objects.
For example, the data type int consists of the objects
{0, +1, -1, +2, -2, , INT_MAX,
INT_MIN} and
the operations +, -, *, /, and %
Contact-Email: khutub27@gmail.com
13
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
5. Performance analysis
Performance Analysis (machine independent)
space complexity: storage requirement
time complexity: computing time
Defn... of Analysis of algorithm or performance analysis or
efficiency algo.
Algorithm analysis refers to the task of finding the computing
time and storage space of an algorithms, and it is also known
as performance analysis or efficiency of an algorithm, which
enables us to select an efficient algorithm
We can analyze the algorithm in two ways
Contact-Email: khutub27@gmail.com
14
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Space complexity:
Specifies the amount of temporary storage required for running
the algorithm.
We do not count storage required for input and output when we
are working with the same type of problem and with different
algorithms.
Space needed by algorithm consists of the following component.
a) The fixed static part: it is independent of characteristics
ex: number of input/output. This part includes the
instruction space (i.e. space for code). Space for simple
variables, space for constants, and fixed size component
variables. Let Cp be the space required for the code segment
(i.e. static part)
b) The variable dynamic part: that consists of the space needed
by component variables whose size is dependent on the
particular problem instance at runtime. i.e. space needed by
reference variables, and the recursion stack space. Let Sp be
the space for the dynamic part.
c) Overall space required by program: S(p)= Cp + Sp
Contact-Email: khutub27@gmail.com
15
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Space Analysis:
The Depth of recursion = 5
There fore The depth of recursion = n
Fact(5)
5*Fact(4) 120
4*Fact(3) 24
3*Fact(2)6
2*Fact(1)2
Space occupied by n and constant 1 = 4 bytes
Total space = 4 * depth of recursion
Total space = 4 * n
Contact-Email: khutub27@gmail.com
16
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Time Complexity:
1) Operation Count
2) Step Count
3) Asymptotic Notation
Algorithm Efficiency:
Linear Loops:
For(i=0;i<1000;i++)
Application code
f(n)= n
For(i=0;i<1000;i+=2)
Application code
f(n)= n/2
Logarithmic Loops:
For(i=0;i<1000;i*=2)
Application code
Or
For(i=0;i<1000;i/=2)
Application code
f(n)= logn
f(n)= n/2
Nested loops:
For(i=0;i<1000;i++)
For(j=0;j<1000;j++)
Application code
f(n)= n2
For(i=0;i<10;i++)
For(j=0;j<10;j*=2)
Application code
f(n)=
nlogn
For(i=0;i<10;i++)
For(j=0;j<i;j++)
Application code
f(n)=
n((n+1)/2)
Contact-Email: khutub27@gmail.com
17
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
1) Operation Count:
Find the basic operation a= a * b; this code takes 1 unit of
time
For(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(j==5)
{
printf(Internal Instruction);
}
}
Basic operation is comparison
So the Time Complexity N*n = n square
2) Step Count:
Ex:
i) x = a + b;
step count =1
Contact-Email: khutub27@gmail.com
18
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
1
=
n + 1
n
=
1
2n + 3
Asymptotic Notations:
The asymptotic behavior of a function is the study of how the
value of a function varies for larger values of n where n
is the size of the input.
Using asymptotic behavior we can easily find the time
efficiency of an algorithm.
Contact-Email: khutub27@gmail.com
19
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Solution:
It is given that f(n) =100n + 5. Replacing 5 with n (so that
next higher order term is obtained), we get 100n + n and call
it c* g(n).
i.e. c* g(n) = 100n + n for n = 5
= 101n for n = 5
Now, the following constraint is satisfied
f(n) c * g(n) for all n n0
i.e. 100n + 5 <= 101* n
for n>= 5
it is clear from the above relation that c=101, g(n)=n and n0 =
5, so by definition
f(n) O(g(n)) i.e. f(n) O(n)
2. notation (Big-Omega)
Let f(n) be the time complexity of an algorithm. The
function f(n) is said to be
Big-Omega of g(n) which is denoted by
f(n) (g(n)) OR f(n) (g(n))
such that there exist a +ve constant c and non negative
integer n0 satisfying the constraint
f(n) c * g(n) for all n n0
ex: Let f(n) = 100n + 5 express f(n) using big-omega
Lecturer: Syed Khutubuddin Ahmed
Contact-Email: khutub27@gmail.com
20
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
for n>= 0
3. notation (Theta)
Let f(n) be the time complexity of an algorithm. The
function f(n) is said to be
Big-Theta of g(n) which is denoted by
f(n) (g(n)) OR f(n) (g(n))
such that there exist a +ve constant c1, c2 and non negative
integer n0 satisfying the constraint
c2g(n) f(n) c1g(n) for all n n0
Contact-Email: khutub27@gmail.com
21
SEM-II
+ 5
CHAPTER-1
DEPARTMENT OF MCA
Contact-Email: khutub27@gmail.com
22
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Contact-Email: khutub27@gmail.com
23
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
6. Performance Measurement:
Clocking:
Although performance analysis gives us a powerful tool for
assessing an algorithms space and time complexity, at some
point we also must consider how the algorithm executes on our
machine.
This consideration moves us from the realm of analysis to that
of measurement.
Start timing
Stop timing
Type
returned
Result in
seconds
Method-1
Start=clock();
Stop=clock();
Clock_t
Method-2
Start=time(NULL)
Stop =time(NULL)
Time_t
Duratrion =
((double)(stop-start)
/ CLOCKS_PER_SEC;
Duration=
(double)
difftime(stop,start);
Contact-Email: khutub27@gmail.com
24