DSA I Detailed Notes English Corrected
DSA I Detailed Notes English Corrected
Definition:
A Data Structure is a way of organizing and storing data so that it can be accessed and modified efficiently.
2. Arrays
Definition:
An array is a collection of elements of the same data type stored at contiguous memory locations.
Operations:
Insertion, Deletion, Traversal, Searching, Sorting
Advantages:
Fast access using index
Easy to implement
Disadvantages:
Fixed size
Insertion/Deletion is costly (requires shifting)
3. Stacks
Definition:
A Stack is a Linear Data Structure that follows the LIFO (Last In, First Out) principle.
Real-Life Example:
A stack of plates - the last plate placed is the first one removed.
Operations:
Push, Pop, Peek/Top, isEmpty
Applications:
Expression evaluation, Syntax parsing, Undo mechanisms
4. Queues
Definition:
A Queue is a Linear Data Structure that follows the FIFO (First In, First Out) principle.
Real-Life Example:
Data Structures and Algorithms - I (Detailed Notes in English)
A queue of people at a bus stop - the first person to arrive is the first to be served.
Types of Queues:
Simple Queue, Circular Queue, Priority Queue, Double-Ended Queue (Deque)
Operations:
Enqueue, Dequeue
Applications:
CPU Scheduling, Print Spooling, Handling requests to a shared resource
5. Linked Lists
Definition:
A Linked List is a Linear Data Structure consisting of nodes, where each node contains data and a pointer to the next
node.
Structure of a Node:
struct Node {
int data;
Node* next;
};
Types:
Singly Linked List, Doubly Linked List, Circular Linked List
Advantages:
Dynamic size, Easy insertion and deletion
Disadvantages:
Sequential access only (no direct access)
6. Searching Algorithms
Linear Search:
Checks each element sequentially.
Time Complexity: O(n)
Binary Search:
Works only on sorted arrays.
Divides the array based on the mid-point.
Time Complexity: O(log n)
7. Sorting Algorithms
Bubble Sort:
Compares adjacent elements and swaps them if needed until the list is sorted.
Algorithm:
1. Start from the 0th index.
2. Compare current element with the next.
3. Swap if required.
4. Repeat for all elements.
Time Complexity:
Best case: O(n) (already sorted)
Average/Worst case: O(n²)
8. Recursion
Definition:
Recursion is when a function calls itself.
Example:
Factorial function:
factorial(n) = n * factorial(n-1)
Base Case: factorial(0) = 1
Important Points:
- Every recursion must have a base case to terminate.
- Recursion internally uses the stack data structure.
9. Complexity Analysis
Time Complexity:
Measures the execution time based on input size.