You are on page 1of 24

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Data Structures using C


Chapter1

BASIC CONCEPTS in Data Structures: What do you mean by Data structure and why do we need to use Data structure?
To understand what Data structure is the following analogy will explain the concept in a better way. Say for example I have a cupboard with me in which I keep my clothes. And say I have 10 shirts with me, without folding it I just throw one by one all the shirts in the cupboard as shown in Fig-1. The next day if I want the pink shirt, then I have to check with each and every shirt whether it is pink or not, Say if I have 100 shirts with me then it is really hard for me to find the pink shirt from that huge collection of shirts. Now instead of throwing it directly without fold, lets keep the shirts in a folded manner neatly, as shown in the fig-2. now can you see the color of every shirt, is it not easy for you to select the particulate shirt what you want? Now just pay attention for this statement, the shirts what I am having with me is the data and arranging this data in a logical manner is called as Data structure.

Fig-1

Fig-2

Defn of Data Structures:


Arranging the data in Logical manner is called as data structures.

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

CLASSIFICATION OF DATA STRUCTURES


DATA STRUCTURES

PRIMITIVE DATA STRUCTURE

NON-PRIMITIVE DS

INTEGER

LINEAR

NONLINEAR

FLOAT

ARRAYS

TREES

DOUBLE
CHARACTER

STACKS

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

1. System Life Cycle


Lecturer: Syed Khutubuddin Ahmed Contact-Email: khutub27@gmail.com 2

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Good programmers regard large-scale computer programs as systems that contain many complex interacting parts. As systems, these programs undergo a development process called the System life cycle. We consider this cycle as consisting of five phases. 1. 2. 3. 4. 5. 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 3

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

o o o o

Data objects and operations This phase continues the work done in analysis phase. The First perspective leads to the creation of abstract data types, Second requires the specification of algorithms and a consideration of algorithm design strategies. Ex: suppose that we are designing a scheduling algorithm system for university. The typical operations might include students, courses, and professors. Typical operations might include inserting, removing, and searching within each object or between them.

4) Refinement and Coding o Choose representation for our data objects and write algorithms for each operation on them. o The order in which we do this is crucial because a data objects representation can determine the efficiency of the algorithm related to it. o Typically this means that we should write those algorithms that are independent of the data objects first. o If our original design is good, it can absorb changes easily. 5) Verification This phase consists of developing correctness proofs for the program, testing the program with a variety of input data, and removing errors. Program Proving: Proving the correctness of program on sample outputs. Testing: Giving the Good test data and to verify every piece of code runs correctly. Debugging: identifying the error if the behavior of the code is not according to the requirements. Debugging will be easier for a well documented code.

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

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

* the dereferencing (indirection) operator referred as value at the address & Address operator gives the address of a memory location. Points to be remembered: Address id a non-negative number Pointer allows arithmetic operations Size of pointer can be different on different computers the null pointer which contains a null address, points to no object or function. NULL is represented by the integer 0 Always set pointers to NULL when not used.

2.1 Dynamic Memory Allocation:


It is defined as the allocation of memory during the execution of program. This means that memory is allocated during run time. C has dynamic memory allocation in the library function alloc.h. 1. malloc() 2. calloc() 3. realloc() 4. free()

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

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); } }

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

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)); . . . . . .

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

3.

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);

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Write a C program to implement Dynamic Memory allocation. #include<stdio.h> #include<conio.h> void main() { int i,p*,size; printf(\n Enter the size of the array); scanf(%d,&size); p=(int *) calloc(5,size of(int)); if(p==NULL) { printf(\n The Memory Is Not available); exit(0); } printf(\n Input The Array Elements); for(i=p;i<p+size;i++) scanf(%d,i); printf(\n The Contents of array ); for(i=p;i<p+size;i++) printf(%d\n,i); }

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 9

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Algorithms + Data structures = Programs

Example 1.1 [Selection sort]:


From those integers that are currently unsorted, find the smallest and place it next in the sorted list. i 0 1 2 3 [0] 30 10 10 10 10 [1] 10 30 20 20 20 [2] 50 50 50 30 30 [3] 40 40 40 40 40 [4] 20 20 30 50 50

Selection Sort Program:


#include<stdio.h> #define MAX_SIZE 10 #include<stdlib.h> void main() { int i,n,a[MAX_SIZE]; int j,min,temp; clrscr(); printf("Enter the size of Array\n"); scanf("%d",&n); if(n<1 || n>MAX_SIZE) { printf("Improper Value"); exit(1); } printf("Enter the Elements\n"); for(i=0; i<n; i++) { scanf("%d",&a[i]); } 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(); }

Example 1.2 [Binary search]: [0] 8 [1] 14 [2] 26 [3] 30 [4] 43 [5] 50 [6] 52

left right middle list[middle] : searchnum Lecturer: Syed Khutubuddin Ahmed Contact-Email: khutub27@gmail.com 10

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Search No = 43 Low 0 4 4 High 6 6 4 Mid 3 5 4 Compare 30 < 43 50 > 43 43 == 43

Search No = 18
Low High Mid Compare

0 0 2 2

6 2 2 1

3 1 2

30 > 18 14 < 18 26 > 18

Searching a sorted list while (there are more integers to check) { middle = (left + right) / 2; if (searchnum < list[middle]) right = middle - 1; else if (searchnum == list[middle]) return middle; else left = middle + 1; }

Algorithm Binary Search:


int binsearch(int list[], int searchnum, int left, int right) { /* search list[0] <= list[1] <= <= list[n-1] for searchnum. Return its position if found. Otherwise return -1 */ int middle; while (left <= right) { middle = (left + right)/2; switch (COMPARE(list[middle], searchnum)){ case -1: left = middle + 1; break; case 0 : return middle; case 1 : right = middle 1; } } return -1; }

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

11

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

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.

Recursive Algorithm Binary Search:


int binarysearch (int list[ ], int searchnum, int left, int right) { /* search list[0] <= list [1] <= <= list[n-1] for searchnum. Return its position if found. Otherwise return -1. */ int middle; if (left <= right) { middle = ( left + right ) / 2; switch ( COMPARE (list[middle], searchnum) ) { case -1 : return binarysearch(list, searchnum, middle+1, right); case 0 case 1 } } return } -1; : return middle; : return binarysearch(list, searchnum, left, middle-1);

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

12

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

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 %

The data types of C


The The The The basic data types: char, int, float and double group data types: array and struct pointer data type user-defined types

Abstract Data Type


An abstract data type(ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations. We know what it does, but not necessarily how it will do it.

Specification vs. Implementation


An ADT is implementation independent Operation specification function name the types of arguments the type of the results The functions of a data type can be classified into several categories: creator / constructor transformers observers / reporters

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

13

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

[Abstract data type Natural_Number]

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 By checking the correctness of an algorithm By measuring time and space and complexity of an algorithms

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

14

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

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

Finding the sum of three numbers:


#include<stdio.h> Void main() { int x, y, z, sum; Printf(Enter the three numbers); Scanf(%d, %d, %d, &x, &y, &z); Sum= x + y + z; Printf(the sum = %d, sum); } In the above program there are no instance characteristics and the space needed by x, y, z and sum is independent of instance characteristics. The space for each integer variable is 2. we have 4 integer variables and space needed by x, y, z and sum are 4 * 2 = 8 bytes. S(p) = Cp + Sp S(p) = 8 + 0 S(p) = 8

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

15

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Finding the sum of array elements:


int ADD(int x[], int n) { int total = 0, I; For(I = 0; I < n ; I ++) total = total + x[I]; Return total; } The space needed by X is the space needed by variable of type array of integer number. This is at least containing N elements to be summed. Here the problem instance is characterized by n. S(p) = Cp + Sp S(p) = 3 * 2 + n S(p) = 6 + n

Finding the Factorial


Int Fact(int n) { if ( n <= 1) return 1; Else return (n * Fact(n-1)); }

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

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

16

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

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 For(i=0;i<1000;i+=2) Application code Logarithmic Loops: For(i=0;i<1000;i*=2) Application code Or For(i=0;i<1000;i/=2) Application code Nested loops: For(i=0;i<1000;i++) For(j=0;j<1000;j++) Application code For(i=0;i<10;i++) For(j=0;j<10;j*=2) Application code For(i=0;i<10;i++) For(j=0;j<i;j++) Application code f(n)= n f(n)= n/2

f(n)= logn f(n)= n/2

f(n)= n2

f(n)=

nlogn

f(n)=

n((n+1)/2)

Internal loop : 1+2+3+.+9+10=55 Avg = 55/10 = 5.5 Can be written as (10+1)/2 so we can write as (n+1)/2

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

17

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

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

ii) for(i =1; i <= n; i++) step count = n x= a + b; iii) for(i=1; i <= n; i++) step count = n2 for(j=1; j <= n; j++) x = a + b; Ex: int sum (int a[], int n) { int i , sum=0; sum-count ++; for (i=0; i<n; i++) { for-count ++; sum = sum + a[i]; } for-count++; return-count ++; return sum; }

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

18

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

The Step count for sum = The step count for for statement The step count for assignment = The step count for return total steps =

1 = n + 1 n = 1 2n + 3

Time Complexity is divided in to THREE Types.


Best Case Time Complexity: Efficiency of an algorithm for an input of size N for which the algorithm takes the smallest amount of time. Average Case Time Complexity: It gives the expected value of T(n). average case is used when worst case and best case doesnt gives any necessary information about algorithms behavior, then the algorithms efficiency is calculated on Random input. Worst Case Time Complexity: efficiency of an algorithm for an input of size N for which the algorithm takes the longest amount of time.

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.

Different asymptotic notations are: Big (big oh) it is the formal method of expressing the upper bound of an algorithms running time. Its a measure of longest amount of time it could possibly take for the algorithm to complete. Let f(n) is the time efficiency of an algorithm. The function f(n) is said to be Big-oh of g(n), denoted by
f(n) O(g(n)) OR f(n) O(g(n))

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

19

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Such that there exist a +ve constant c and +ve integer n0 satisfying the constraint.

f(n) c * g(n) for all n n0

E.g.: Consider the following assertion: Let f(n)=100n + 5 Express f(n) using big-oh

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

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

f(n) c * g(n) for all n n0 i.e. 100n + 5 100 * n for n>= 0

it is clear from the above relation that c = 100, g(n) = n and n0 ,So by definition f(n) (g(n)) i.e. f(n) (n)

Ex: let f(n) = 10n3 + 5 10n3 + 5 10 * n3 for n 0 f(n) (n3)

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

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

21

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Ex: Let f(n) = 100n

+ 5

express f(n) using big-theta

c2g(n) f(n) c1g(n) for all n n0 100 * n 100n + 5 100n+n for n 5 It is clear from the above relation that c1=100, c2=105, n0= 5 , g(n)= n So by definition f(n) (g(n)) f(n) (n)

There are two types of algorithms or functions


1) Non-Recursive 2) Recursive Each one has its own advantages and disadvantages Mathematical analysis of Non recursive algorithms: Non-Recursive or Iterative Algorithms: In analysis of Non-Recursive algorithms there are some basic steps we follow: Deciding the input parameters size (or) count Identifying the basic operation required Finding the reason if the basic operation is to be executed more than once Setting up a sum or a recurrence relation for expressing the number of times the basic operation is executed Finding the complexity function or order of growth using standard formulae. Ex: consider a problem of finding the largest element in an array: Algorithm: MAX (A [0 n-1]) // to find the largest element in a given array // Input: an array A [0 n-1] // Output: The largest element in A [0 n-1] Max A [0] for I = 1 to n 1 do if A [ i ] > Max Max A [ i ] Return max

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

22

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

Analysis of the above Program Steps: Input parameter size : n Basic Operation required : if A [ i ] > Max Reason for basic operation to be made throughout the array, for loop is to be used starting from second element until last element is reached. Relation for finding the number of times the basic operation is executed Basic formula involved in solving the sum/ relation Therefore the time complexity in finding the largest element is the array = O (n)

Mathematical analysis of Recursive algorithms: Recursive Algorithms:


In analysis of Recursive algorithms there are some basic steps we follow: Deciding the input parameters size Identifying the basic operation required Finding the reason if the basic operation is to be executed more than once Setting up a recurrence relation for expressing the number of times the basic operation is executed Solving the recurrence relation for finding the complexity function and order of growth. Ex: Fact (n) // // // If computing n! For an arbitrary non-negative integer input: A non-negative integer n output: factorial of n (n!) n = 0 Return 1 Else Return n * Fact (n - 1)

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

23

SEM-II

KNS INSTITUTE OF TECHNLOGY

DATA STRUCTURES USING C-

CHAPTER-1

DEPARTMENT OF MCA

The Basic Steps involved are: Input parameter size: n Basic operation required : n * Fact (n-1) Reason if the operation be called more number of times: As n is non-negative integer and it starts reducing by 1 for each recursion, the algorithm should stop when n becomes 0, so the operation is performed until n = 0. Recurrence relation and initial condition: T (n) = T (n-1) + 1 for n > 0 // Relation T (0) = 1 // initial condition Solution for the recurrence relation: T (n) = T (n-1) + 1 = T (n -2) + 1 + 1 = T (n -3) + 1 + 1 + 1 = . = T (0) + 1 + 1 + ..n Times. There fore the number of multiplication required = n.

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 Duratrion = ((double)(stop-start) / CLOCKS_PER_SEC; Method-2 Start=time(NULL) Stop =time(NULL) Time_t Duration= (double) difftime(stop,start);

Lecturer: Syed Khutubuddin Ahmed

Contact-Email: khutub27@gmail.com

24

You might also like