Data Structures are ways to organize and store data so that they can be accessed and modified
efficiently. They play a critical role in the performance of algorithms.
Types of Data Structures:
1.Linear Data Structures:
•Array: A collection of elements, stored in contiguous memory locations. Access is fast (O(1)),
but insertion/deletion can be slow.
•Linked List: A sequence of elements (nodes), where each node contains data and a reference
to the next node. It allows dynamic memory allocation.
2.Non-Linear Data Structures:
•Tree: A hierarchical structure with a root node and child nodes. Common types: Binary Tree,
Binary Search Tree (BST), AVL Tree.
•Graph: A set of nodes (vertices) connected by edges. Can be directed or undirected.
3.Hashing:
•A technique used to convert data into a fixed-size value using a hash function for fast access.
2. Algorithms and Their Types
An Algorithm is a step-by-step procedure for solving a problem or performing a task.
Types of Algorithms:
1.Sorting Algorithms: Used to reorder elements in a list.
•Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong order.
•Selection Sort: Selects the smallest element and swaps it with the current position.
•Insertion Sort: Inserts elements in their correct position.
•Merge Sort: Divides the list into sublists, sorts them, and merges them.
•Quick Sort: Divides the list into smaller partitions based on a pivot element and sorts each
partition.
, 2.Searching Algorithms: Used to find an element in a collection.
•Linear Search: Checks each element one by one.
•Binary Search: Divides the collection in half and eliminates half of the elements with each
comparison (only works on sorted data).
3.Graph Algorithms:
•BFS (Breadth-First Search): Explores all the neighbors of a node before moving to the next
level.
•DFS (Depth-First Search): Explores as far as possible down one branch before backtracking.
4.Dynamic Programming (DP): Solves problems by breaking them down into simpler
subproblems and storing the results to avoid recomputation. Examples: Fibonacci sequence,
Knapsack problem.
5.Greedy Algorithms: Always makes the local optimal choice at each step with the hope of
finding the global optimum. Example: Coin change problem.
3. Time and Space Complexity
•Time Complexity: Describes how the execution time of an algorithm increases with the size of
the input.
•Common complexities:
•O(1): Constant time
•O(log n): Logarithmic time (e.g., Binary Search)
•O(n): Linear time (e.g., Linear Search)
•O(n^2): Quadratic time (e.g., Bubble Sort)
•Space Complexity: Describes the amount of memory an algorithm uses in relation to the input
size.
4. Stacks and Queues
1.Stack: A linear data structure that follows the Last In First Out (LIFO) principle. Operations:
•Push: Insert an element.