DSA UNIT 1
DSA UNIT 1
Data structures in C is a way of storing and organizing data in the computer memory so
that it can be processed efficiently. Data structures can be broadly classified into two categories -
Primtive and Non-Primitive. Non-primitive data structures can be further classified into two
categories - Linear and Non-linear.
remove() – Remove the first occurrence of any element from a non-empty list.
The program allocates memory for the data and address is passed to the stack ADT.
The head node and the data nodes are encapsulated in the ADT. The calling function can only see the
pointer to the stack.
The stack head structure also contains a pointer to top and count of number of entries currently in
stack.
pop() – Remove and return the element at the top of the stack, if it is not empty.
peek() – Return the element at the top of the stack without removing it, if the stack is not empty.
The queue abstract data type (ADT) follows the basic design of the stack abstract data type.
Each node contains a void pointer to the data and the link pointer to the next element in the queue. The
program’s responsibility is to allocate memory for storing the data.
dequeue() – Remove and return the first element of the queue, if the queue is not empty.
peek() – Return the element of the queue without removing it, if the queue is not empty.
Disadvantages:
Overhead
Complexity
Learning
Limited Flexibility
Cost
Mathematical Notation
Algorithm Analysis An algorithm is said to be efficient and fast, if it takes less time to
execute consumes less memory space.
Space Complexity
The amount of memory space required by the algorithm in its life cycle. A fixed part : For
example simple variables & constant used and program size etc. A variable part : For
example dynamic memory allocation, recursion stacks space etc.
Space complexity S(P) of any algorithm P is S(P) = C + SP(I) Where C is the fixed part S(I) is
the variable part of the algorithm
Time Complexity - T(n)
The amount of time required by the algorithm to run to completion. T(n) can be measured as the number of steps, provided each step consumes
constant time.
ALGORITHM ANALYSIS
The worst-case complexity of the algorithm is the function defined by the maximum number of steps taken on any instance of size n.
The best-case complexity of the algorithm is the function defined by the minimum number of steps taken on any instance of size n.
Finally, the average-case complexity of the algorithm is the function defined by the average number of steps taken on any instance of size n.
Growth of Functions Growth of the function is one where it analyses if the input has increased , or how quickly the running time goes up. One easiest
way of comparing different running times is to plot them and see the natures of the graph Another way of checking if a function f(n) goes faster or
slower is by:
Order of Growth Function
Value of growth-rate function
ASYMPTOTIC NOTATION
Asymptotic analysis refers to computing the running time of any operation in mathematical units of
computation.
Ο Notation
Ω Notation
θ Notation
There are 3 types of asymptotic notations:
• Big O notation
• Ω notation
• θ notation
Big Oh Notation, Ο It measures the worst case time complexity or longest amount of time an algorithm can possibly
take to complete.
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <= f(n) <= cg(n) for all n >= n0}
BIG Ω NOTATION(Ω ): Ω notation provides an asymptotic lower bound. Ω(g(n)) = { f(n): there exist positive
constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n≥ n0}
Θ NOTATION: Θ((g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1*g(n) ≤
f(n) ≤ c2*g(n) for all n≥n0}