×

Close

- Data Structure and Algorithms - DSA
- 2016
- PYQ Solution
**Kalinga Institute of Industrial Technology - KIIT**- Computer Science Engineering
- B.Tech
**575 Views**- 14 Offline Downloads
- Uploaded 2 years ago

School of Computer Engineering AUTUMN 2016 KIIT, UNIVERSITY DSA Questions & Answers Data Structure & Algorithm INTRODUCTION 1. Define the term Data Structure and ADT with example. List any four applications of data structure. Answer: Data Structure: A data structure is a specialized/way format for organizing and storing data. A data structure is the organization of data in computer memory in such a way that any programming language can use it in an efficient way. Abstract Data Type (ADT): An abstract data type (ADT) is a mathematical model for data types where a data type is defined by its behavior from the point of view of a user of the data. Or An ADT is a set of elements with some well defined operation. The representation of stack ADT struct stacknode { int info[MAX]; int top; }; typedef struct stacknode STACK; here STACK is the stack ADT. Through this we can define different variables of stack as follows; STACK s1, s2; //Here s1 and s2 are of STACK type. 2. Find the time complexity of the following code snippet: void function(int n) { int i,j,k; for(i=n/2; i<=n; i++) for(j=1; j + n/2<=n; j= j++) for(k=1; k<=n; k= k * 2) count++; } Answer: O(n2log n) Explanation: for-i loop executes approx n times (though it executes actually n/2 times, for larger value of n, n and n/2 are same), for each i, for-j loop executes approx. n times and for each j, for-k loop executes approx. log n times, so total n*n*log int i, j, k, sum = 0; for (i = 1; i<= n; i++) { for (j = 1; j <= 1000; j += 2) { sum++; } } for (k = -50; k <= -1; k++) { sum++; } printf(“%d”, sum); Answer: O(n) Explanation: Simple individual statements/expressons simply ignore. Nested outer for-ij loop executes approx O(n) times and for-k loop executes O(1) time. Total=O(n)+O(1)=O(n) KIIT/SCE/AUTUMN-2016/3rdSem/CS-10/DSA/CS-2001/Study Material by Prof. Anil Kumar Swain Page 1 of 30

School of Computer Engineering AUTUMN 2016 KIIT, UNIVERSITY DSA Questions & Answers Data Structure & Algorithm n=n2log n times. Simply rule to remember: (concentrate on loop variable) a) Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. Example: void swap(int *x, int *y) { int temp; temp=*x; *x=*y; *y=temp; } Time complexity of swap function= Total number of simple statements=4=constant=O1) b) A loop or recursion that runs a constant number of times is also considered as O(1). Example // Here c is a constant int i; for (i = 1; i <= c; i++) { // some O(1) expressions } c) Time Complexity of a loop is considered as O(n) if the loop variables i is incremented / decremented by a constant amount c. Example: for (i = 1; i <= n; i=i+c) for (i = n; i <= 1; i = i-c) { { // some O(1) expressions // some O(1) expressions } } d) Time Complexity of a loop is considered as O(log n) if the loop variables i is multiplied or divided by a constant amount c. Example: for (i = 1; i <= n; i=i*c) for (i = n; i <= 1; i = i/c) { { // some O(1) expressions // some O(1) expressions } } e) Time Complexity of a loop is considered as O(log log n) if the loop variables i if the loop variables is reduced / increased exponentially by a constant amount c. Example: // Here c is a constant greater than 1 //Here fun is sqrt or cuberoot or any for (i = 2; i <= n; i=pow(i,c)) other constant root { for (i = n; i > 0; i = fun(i)) KIIT/SCE/AUTUMN-2016/3rdSem/CS-10/DSA/CS-2001/Study Material by Prof. Anil Kumar Swain Page 2 of 30

School of Computer Engineering AUTUMN 2016 KIIT, UNIVERSITY DSA Questions & Answers // some O(1) expressions Data Structure & Algorithm { } // some O(1) expressions } f) Time complexity of nested loops is equal to the number of times the innermost statement is executed that is nothing but the multiplication of outer loop complexity into inner loop complexity. Sl. Program/Program Segment Time Explanation No. Complexity 1 int i, j, k; O(m*n) Outer for m times and inner for n for (i = 1; i <=m; i ++) times, so total mn times { If m=n, then for (j = 1; j <=n; j ++) { O(n2) k=k+1; } } 2 int i, j, k; O(n2) Outer loop approx n times and for (i = n; i >1; i =i-2) inner approx n times, so total n2 { times. for (j = 1; j <=n; j=j+3) { k=k+1; } } 3 int i, j, k; O(nlog n) Outer loop approx log n times, for (i = n; i >1; i =i*2) inner loop n times, so total nlog n { times. for (j = 1; j <=n; j++) { k=k+1; } } 4 for (i = 1; i<=n; i=i*2) O((log n)2) Outer loop approx log n times and { inner loop approx log n times, so for (j =n; j>=1; j=j/2) total ((log n)2 times. { Simple-Statements; } } 5 for (i = 1; i<=n; i++) O(n) The outer loop executes approx. n { times, and the innermost 100 times. for (j =1; j<=100; j++) Here { n=O(n) and 100=O(1) constant ime Simple-Statements; So total=O(n)*O(1)=O(n) } } KIIT/SCE/AUTUMN-2016/3rdSem/CS-10/DSA/CS-2001/Study Material by Prof. Anil Kumar Swain Page 3 of 30

School of Computer Engineering AUTUMN 2016 KIIT, UNIVERSITY DSA Questions & Answers Data Structure & Algorithm g) When there are consecutive loops, the time complexity is calculated as sum of time complexities of individual loops and final time complexity is the higher order term in terms of n (the one which is larger than others for larger value of n) Sl. Program/Program Segment Time Explanation No. Complexity 1 for (int i = 1; i <=m; i += c) O(m+n) First for-I loop O(m), second for-i { If m=n, then loop O(n) times, so total O(m+n) // some O(1) expressions O(2n)=O(n) times } for (int i = 1; i <=n; i += c) { // some O(1) expressions } 2 for (int i = 1; i <=n; i *= 2) O(n) First for-I log n times, second n { times. Now total=log n+n =O(n) as // some O(1) expressions n is asymptotically larger than log } n. for (int i = 1; i <=n; i ++) { // some O(1) expressions } 3 int i, j, k; O(nlog n) Nested for-ij loop is executed nlog for (i = 1; i <=n; i ++) n times. For k loop log n times, for{ l loop n times. So total=nlog n + for (j = 1; j <=n; j=j*2) log n + n = O(nlog n) as nlog { n>n>log n p=i+j; } } for ( k = n; k >=1; k=k/3) { q=k+p; } for ( l = n; l >=1; l=l-2) { q=k+p; } KIIT/SCE/AUTUMN-2016/3rdSem/CS-10/DSA/CS-2001/Study Material by Prof. Anil Kumar Swain Page 4 of 30

## Leave your Comments

## md shadab

2 years ago00