

























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Algorithms: Definition, Properties, Performance Analysis-Space Complexity, Time Complexity,. Asymptotic Notations.Data structures: Introduction, Data ...
Typology: Exercises
1 / 97
This page cannot be seen from the preview
Don't miss anything!


























































































The course should enable the students to :
Iteration: Iteration type algorithms are used in solving the problems which involves repetition of statement. In this type of algorithms, a particular number of statements are repeated ‘n’ no. of times. Example1: Step 1 : start Step 2 : read n Step 3 : repeat step 4 until n> Step 4 : (a) r=n mod 10 (b) s=s+r (c) n=n/ Step 5 : write s Step 6 : stop Performance Analysis an Algorithm: The Efficiency of an Algorithm can be measured by the following metrics. i. Time Complexity and ii. Space Complexity. i.Time Complexity: The amount of time required for an algorithm to complete its execution is its time complexity. An algorithm is said to be efficient if it takes the minimum (reasonable) amount of time to complete its execution. ii. Space Complexity: The amount of space occupied by an algorithm is known as Space Complexity. An algorithm is said to be efficient if it occupies less space and required the minimum amount of time to complete its execution.
2. Write an algorithm to find the largest among three different numbers entered by user Step 1: Start Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c. Step 4: If a>b If a>c Display a is the largest number. Else Display c is the largest number. Else If b>c Display b is the largest number. Else Display c is the greatest number. Step 5: Stop 3.Write an algorithm to find the factorial of a number entered by user. Step 1: Start Step 2: Declare variables n,factorial and i. Step 3: Initialize variables factorial← i← Step 4: Read value of n Step 5: Repeat the steps until i=n 5.1: factorial←factorial*i 5.2: i←i+ Step 6: Display factorial Step 7: Stop 4.Write an algorithm to find the Simple Interest for given Time and Rate of Interest. Step 1: Start Step 2: Read P,R,S,T. Step 3: Calculate S=(PTR)/ 100 Step 4: Print S Step 5: Stop
Asymptotic analysis of an algorithm refers to defining the mathematical boundation/framing of its run-time performance. Using asymptotic analysis, we can very well conclude the best case, average case, and worst case scenario of an algorithm. Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to work in a constant time. Other than the "input" all other factors are considered constant. Asymptotic analysis refers to computing the running time of any operation in mathematical units of computation. For example, the running time of one operation is computed as f (n) and may be for another operation it is computed as g (n^2 ). This means the first operation running time will increase linearly with the increase in n and the running time of the second operation will increase exponentially when n increases. Similarly, the running time of both operations will be nearly the same if n is significantly small.
θ( f (n)) = { g (n) if and only if g (n) = Ο( f (n)) and g (n) = Ω( f (n)) for all n > n 0. } DATA STRUCTURES Data may be organized in many different ways logical or mathematical model of a program particularly organization of data. This organized data is called “Data Structure”. Or The organized collection of data is called a ‘Data Structure’. Data Structure involves two complementary goals. The first goal is to identify and develop useful, mathematical entities and operations and to determine what class of problems can be solved by using these entities and operations. The second goal is to determine representation for those abstract entities to implement abstract operations on this concrete representation. Primitive Data structures are directly supported by the language ie; any operation is directly performed in these data items. Ex: integer, Character, Real numbers etc. Non-primitive data types are not defined by the programming language, but are instead created by the programmer.
Linear data structures organize their data elements in a linear fashion, where data elements are attached one after the other. Linear data structures are very easy to implement, since the memory of the computer is also organized in a linear fashion. Some commonly used linear data structures are arrays, linked lists, stacks and queues. In nonlinear data structures, data elements are not organized in a sequential fashion. Data structures like multidimensional arrays, trees, graphs, tables and sets are some examples of widely used nonlinear data structures. Operations on the Data Structures: Following operations can be performed on the data structures:
Representation of Stack (or) Implementation of stack: The stack should be represented in two ways:
2.Pop(): When an element is taken off from the stack, the operation is performed by pop(). Below figure shows a stack initially with three elements and shows the deletion of elements using pop(). We can insert an element from the stack, decrement the top value i.e top=top- 1. We can delete an element from the stack first check the condition is stack is empty or not. i.e top==- 1. Otherwise remove the element from the stack. Void pop() { If(top==-1) { Printf(“Stack is Underflow”); } else { printf(“Delete data %d”,stack[top]); top=top-1; } } Algorithm: procedure pop(): Step 1: START Step 2: if top==-1 then Write “Stack is Underflow” Step 3: otherwise 3.1: print “deleted element” 3.2: top=top-1; Step 4: END 3.display(): This operation performed display the elements in the stack. We display the element in the stack check the condition is stack is empty or not i.e top==-1.Otherwise display the list of elements in the stack.
display(); break; } case 4: { printf("\n\t EXIT POINT "); break; } default: { printf ("\n\t Please Enter a Valid Choice(1/2/3/4)"); } } } while(choice!=4); return 0; } void push() { if(top>=n-1) { printf("\n\tSTACK is over flow"); } else { printf(" Enter a value to be pushed:"); scanf("%d",&x); top++; stack[top]=x; } } void pop() { if(top<=-1) { printf("\n\t Stack is under flow"); } else { printf("\n\t The popped elements is %d",stack[top]); top--; } } void display() { if(top>=0) {
printf("\n The elements in STACK \n"); for(i=top; i>=0; i--) printf("\n%d",stack[i]); printf("\n Press Next Choice"); } else { printf("\n The STACK is empty"); } }
2. Stack using Linked List: We can represent a stack as a linked list. In a stack push and pop operations are performed at one end called top. We can perform similar operations at one end of list using top pointer. The linked stack looks as shown in figure. Applications of stack:
Evaluation of postfix expression: The postfix expression is evaluated easily by the use of a stack.
Representation of Queue (or) Implementation of Queue: The queue can be represented in two ways:
Next insert another element, say 66 to the queue. We cannot insert 66 to the queue as the rear crossed the maximum size of the queue (i.e., 5). There will be queue full signal. The queue status is as follows: Now it is not possible to insert an element 66 even though there are two vacant positions in the linear queue. To overcome this problem the elements of the queue are to be shifted towards the beginning of the queue so that it creates vacant position at the rear end. Then the FRONT and REAR are to be adjusted properly. The element 66 can be inserted at the rear end. After this operation, the queue status is as follows: This difficulty can overcome if we treat queue position with index 0 as a position that comes after position with index 4 i.e., we treat the queue as a circular queue. Queue operations using array: a.enqueue() or insertion(): which inserts an element at the end of the queue. void insertion() { if(rear==max) printf("\n Queue is Full"); else { printf("\n Enter no %d:",j++); scanf("%d",&queue[rear++]); } } Algorithm: Procedure for insertion(): Step-1:START Step-2: if rear==max then Write ‘Queue is full’ Step-3: otherwise 3.1: read element ‘queue[rear]’ Step-4:STOP b.dequeue() or deletion(): which deletes an element at the start of the queue. void deletion() { if(front==rear) { printf("\n Queue is empty"); } else { printf("\n Deleted Element is %d",queue[front++]); x++; } } Algorithm: procedure for deletion(): Step-1:START Step-2: if front==rear then Write’ Queue is empty’ Step-3: otherwise 3.1: print deleted element Step-4:STOP