Sei sulla pagina 1di 10

Theory Data It is collection of numbers, alphabets and symbols combined to represent information.

. Data Structure It is the way in which the data is organized. It is an instance of abstract data type (ADT). The combination which describes the set of elements together with the description of all operations is called data structure. Data structure is formally defined as a triplet (D, F, A) where D is set of domains, F is set of operations and A is axioms defining the function in F. Abstract Data Type It is a triplet of D set of domains, F set of functions and A set of axioms in which what is to be done is mentioned but how to do is not mentioned. In ADT, all the implementation details are hidden. Classification of Data Structure

Primitive DS Data Structure (DS) Non - Primitive DS

Linear DS Non - Linear DS Static DS Dynamic DS

1. Primitive Data Structure they are the fundamental data types. Example :- int, float, char, double in C etc. 2. Non Primitive Data Structure they are built using primitive data types. They are user defined data types. Example :- structure, union , enum in C. 3. Linear Data Structure are the data structures in which data is arranged in a list or in a straight sequence. Example :- array, stack, queue, linked list. 4. Non Linear Data Structure are the data structures in which data may be arranged in a hierarchical manner. Example :- tree, graph.

5. Static Data Structure are the data structures having fixed size memory utilization. The size is allocated before using it. Example :- array in C 6. Dynamic Data Structure are the data structures in which memory is allocated as per the requirement. Example :- linked list The advantage of dynamic DS over static DS is there is no wastage of memory. Algorithm it is a finite set of instructions for performing some task. Characteristics of algorithm 1. 2. 3. 4. 5. Input Output Definiteness Finiteness Effectiveness

Time Complexity it is defined as the time required by the program / algorithm statements to execute. It is counted in terms of frequency count. Space Complexity it is defined as amount of space required by the algorithm / data. It is calculated by considering data and their sizes. Best case time complexity it is the time complexity when algorithm runs for short (minimum) time. Average case time complexity it gives information about the behaviour of an algorithm on specific input. Best case time complexity it is the time complexity when algorithm runs for a longest (maximum) time. Asymptotic notations it is a shorthand way to represent the time complexity. Various notations like , and O used are called asymptotic notations. 1. Big Oh (O) notation It represents the upper bound of algorithms running time. i.e. it gives longest amount of time taken by algorithm. Defination Let F(n) and G(n) be two non negative functions. Let n0 and constant c are two integers such that n0 denotes some value of input and n > n0 and c > 0. We can write F (n) <= c * G (n)

Then F (n) is big oh of G (n). It is also denoted as F (n) O (G (n)). 2. Omega () notation It represents the lower bound of algorithms running time. i.e. it gives shortest amount of time taken by algorithm. Defination Let F (n) and G (n) be two non negative functions. Let n0 and constant c are two integers such that n0 denotes some value of input and n > n0 and c > 0. We can write F (n) >= c * G (n) Then F(n) is omega of G (n). It is also denoted as F (n) (G (n)). 3. Theta () notation It represents the running time between upper bound and lower bound. Defination Let F(n) and G(n) be two non negative functions. Let c1 and c2 are two positive constants such that c1 <= G (n) <= c2 * G (n) Then F (n) is theta of G (n). It is also denoted as F (n) (G (n)). Array it is referred as the sequential organization i.e. the data in array is stored in some sequence. Representation of array:1D array data_type name_of_array [size]; 2D array data_type name_of_array [row_size][column_size]; Advantages of sequential organization of DS 1. Elements can be retrieved or stored very efficiently with the help of index or memory location. 2. As all elements are stored in continuous memory locations, searching of element is easy. Disadvantages of sequential organization of DS 1. Insertion and deletion of elements becomes complicated because of sequential nature. 2. For storing the data, large continuous free memory block is required.

3. Memory fragmentation occurs if we remove the elements randomly. Applications of array 1. Arrays are useful for representing simple lists. 2. For handling the polynomial, array can be used. 3. It is useful in sparse matrix representation & for performing various operations on it. Sparse Matrix it is that matrix which has a very few non zero elements as compared to the size m x n of matrix. Representation of sparse matrix:The representation of sparse matrix will be a triplet. Only non zero values are considered with their positions. Sparse matrix is a 2D matrix where 0th row will store following information:[Total rows of matrix Example:Suppose a matrix is 6 x 7 and number of non-zero terms are 8. In sparse matrix representation, the matrix will be stored as Index Row no. Column no. Value Total columns of matrix Total non zero values]

0 6 7 8 1 0 6 -10 2 1 0 55 3 2 5 -23 4 3 1 67 5 3 6 88 6 4 3 14 7 4 4 -28 8 5 0 99 Here, 0th row gives information about total no. of non-zero rows, total no. of non-zero columns and total non-zero values.

Stack it is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called top of the stack (tos). It is a LIFO (Last In First Out) data structure. Basic operations on stack: 1. To create a stack Let st be an array representing a stack and size = 5 and tos stores top of the stack. Initially tos = -1 0 1 2 3 4

2. To insert an element on to the stack After inserting 10, 20, 30 on stack tos 10 20 30 0 1 2 3 4 3. To remove an element from the stack After removing 1 element from stack tos 10 20 0 1 2 3 4 Key point: If stack is full then we cannot insert any new element. This is overflow condition. If stack is empty then we cannot remove any element. This is underflow condition.

Application of Stack 1. 2. 3. 4. 5. 6. Parsing well formed parenthesis (Balanced parenthesis) Expression conversion Expression evaluation Decimal to binary conversion Reversing a string Storing function calls

Expression it is a string of operands and operators. There are 3 types of expression: 1. Infix expression here, the arrangement of operands & operator is as follows: Infix expression = operand1 operator operand2 Example: (a+b) 2. Postfix expression (Reverse Polish Notation) here, the arrangement of operands & operators is as follows: Postfix expression = operand1 operand2 operator Example: ab+ 3. Prefix expression (Polish Notation) here, the arrangement of operands & operators is as follows: Prefix expression = operator operand1 operand2 Example: +ab Recursion it is a process of executing certain set of instructions repeatedly by calling the self function itself. A function calling itself repeatedly is called recursive function. Properties of recursive function 1. There is at least one non-recursive call because of which the function will exit. 2. Any instant of recursive defination must eventually reduce some manipulation. Comparison between iteration and recursion Sr. no. 1 Iteration It is a process of executing set of instructions without calling self function. Iterative functions are implemented using while, for & do while loop. More efficient Recursion It is a process of executing set of instructions by calling self function. Recursive functions are implemented using a condition so that same function can be called again. Less efficient

4 5 6

Memory utilization is less Simple to implement Lines of code is more

Memory utilization is more Complex to implement Lines of code is less

Tower of Hanoi here, there are 3 towers or pegs, source, auxiliary (temporary) and destination. There are some disks available on source tower of different size. We have to move all the disks from source tower to destination tower using auxiliary tower. There are 2 rules:1. Only 1 disk can be moved at a time. 2. Cannot put (move) larger disk on smaller disk. (Algorithm of Tower Of Hanoi must be written after this theory.) Queue it is an ordered collection of items from which items may be deleted at one end (called front of queue) and into which items may be inserted at other end (called rear of queue). It is a FIFO (First In First Out) data structure. 1. 2. 3. 4. Simple Queue Circular Queue Priority Queue Dequeue (Double Ended Queue)

Basic operations on queue: 1. To create a simple queue Let que[size] be simple queue of size=5. Intially front = -1, rear = -1 0 1 2 3 4

2. To insert an element in simple queue front rear 10 20 30 0 1 2 3 4 3. To remove element from simple queue front rear 20 30 0 1 2 3 4

Circular Queue In simple queue (fig below), if we try to insert 60 rear will become 5 (size) and overflow condition will be reached. Still the queue is not empty, we cannot insert any new element. front 30 40 2 3 rear 50 4

Hence there is a concept of circular queue. The main advantage of circular queue is utilization of space of queue is done fully.

rear 60 0

front 30 40 2 3

50 4

In circular queue (fig above), when rear = size and we want to insert new element then it is inserted at 0th position i.e. rear = 0. Priority Queue It is a data structure in which the intrinsic ordering of the elements does determine the results of its basic operations. OR It is a data structure having a collection of elements which are associated with specific ordering. There are 2 types of priority queue:1. Ascending priority queue 2. Descending priority queue Ascending priority queue it is a collection of items into which items can be inserted arbitrarily and from which only the smallest item can be removed. Descending priority queue it is a collection of items into which items can be inserted arbitrarily and from which only the largest item can be removed. Note:- Stack may be viewed as a descending priority queue whose elements are ordered by time of insertion. The element that was inserted 1st has largest insertion time & is the only value that can be retrieved.

Queue may be viewed as a ascending priority queue whose elements are ordered by time of insertion. The element that was inserted last has smallest insertion time & is the only value that can be retrieved. Double Ended Queue (Dequeue) it is an ordered set of items from which items may be deleted at either end (front or rear) and into which items may be inserted at either end (front or rear). 10 front end 0 Front 20 1 30 2 Rear 3 4 rear end

Dequeue

Input restricted queue

Output restricted queue

Input restricted queue In dequeue, when we use only one end for inserting an element and both the end for deleting then such a queue is called input restricted queue. Output restricted queue In dequeue, when we use both end for inserting an element and only one end for deleting then such a queue is called output restricted queue. Application of Queue 1. 2. 3. 4. Job Scheduling Categorizing Data Josephus Problem Simulation

Linked Lists it is a set of nodes where each node has 2 fields info and next. The info field contains the actual element on the list. The next field contains the address of next node in the list. Fig shows structure of a node in linked list. Info Next Structure of a node The null pointer is used to indicate end of a list. The list with no nodes on it is called null list or empty list. Types of Linked Lists 1. Singly linear linked list 10 20 2. Singly circular linked list 10 20 3. Doubly linear linked list null 10 20 30 null 30 null 30 null

Application of liked list 1) Representation of polynomial and performing various operations such as addition, multiplication, evaluation on it. 2) Performing addition of long positive integer. 3) Representing non-integer and non-homogeneous list. Advantages of linked list 1) No prior estimation on number of term is required. 2) Insertion and deletion operation can be carried out very easily without movement of data. 3) Physically data can be deleted. Disadvantages of lined list 1) We cannot access any term randomly or directly, we have to go from start node always.

Potrebbero piacerti anche