- Data: Raw, unorganized values or a set of values assigned to entities.
- Information: Data that has been processed to become meaningful.
- Entity: A thing that has certain properties or attributes, which can be assigned values.
- Data Types:
- Primitive: Basic data types directly supported by the computer's hardware, such as integers, floats, and characters.
- Non-primitive: More complex types built from primitive types, including arrays and structures.
- Definition: A way of organizing and storing data in a computer so that it can be accessed and modified efficiently.
- Classification:
- Linear: Data is arranged in a sequential manner. Examples include arrays, linked lists, stacks, and queues.
- Non-linear: Data is not arranged sequentially and has a hierarchical or network-based relationship. Examples include trees and graphs.
- Traversing: Visiting or accessing each element in the structure once to perform an operation, such as displaying or modifying it.
- Searching: Finding a specific element within the data structure.
- Inserting: Adding a new element into the structure.
- Deleting: Removing an element from the structure.
- Sorting: Arranging the elements in a specific order, such as ascending or descending.
- Merging: Combining two or more data structures into one.
Question 2:>> Complexity of Algorithms - Big o-Notation
A data structure is just a specific way of organizing and storing data in a computer so that it can be used efficiently. Think of it like organizing your books:
| Structure | Analogy | Description |
| Array | A numbered shelf where every book is in a fixed spot. | Stores a fixed-size, sequential collection of elements of the same type. Accessing any item is instant. |
| Linked List | A chain where each book knows which book comes next. | A sequence of nodes, where each node has the data and a reference (link) to the next node. Items can be easily added or removed. |
| Stack | A stack of plates: Last one in, First one out (LIFO). | Elements are added and removed only from the top. |
| Queue | A line at a ticket counter: First one in, First one out (FIFO). | Elements are added to the back (enqueue) and removed from the front (dequeue). |
2. Complexity of Algorithms 🤔
When we talk about the "complexity" of an algorithm, we're asking: "How much more time or space (memory) will this algorithm need as the size of the input data grows?"
Time Complexity: How long an algorithm takes to run.
Space Complexity: How much memory an algorithm needs.
Since computers vary in speed, we don't measure the time in seconds. Instead, we measure the number of operations an algorithm performs. This is where Big O-Notation comes in.
3. Big O-Notation (The Simple Way) 🚀
Big O-Notation is a mathematical tool that describes the worst-case scenario for an algorithm's performance. It tells you the rate of growth of the time or space required by an algorithm as the input size (n) increases.
It focuses on the dominant term (the part that grows the fastest) and ignores constants and smaller terms. It’s all about the trend.
Common Big O Examples:
| Notation | Name | Analogy | Meaning (Growth Rate) |
| O(1) | Constant Time | Looking up a book on an indexed shelf. | The time taken is the same regardless of the input size (n). This is the best case. |
| O(\log n) | Logarithmic Time | Finding a word in a dictionary (Binary Search). | The time taken grows very slowly. The time required roughly halves with each step. Very efficient. |
| O(n) | Linear Time | Looking for a specific friend in a crowd of n people, one by one. | The time taken grows directly proportional to the input size (n). If the input doubles, the time roughly doubles. |
| O(n \log n) | Log-Linear Time | Efficient sorting algorithms like Merge Sort. | A very reasonable and efficient growth rate for complex tasks like sorting. |
| O(n^2) | Quadratic Time | Comparing every pair of people in a room of n people. | The time taken grows as the square of the input size. Bad for large inputs. If n doubles, the time quadruples (2^2=4). |
| O(2^n) | Exponential Time | Calculating every possible subset of a set of n items. | The time taken explodes as the input grows. Only feasible for very small inputs. This is the worst. |
The Goal: Lower is Better!
When designing an algorithm, your goal is generally to achieve the lowest (most efficient) Big O complexity possible:
(Best) \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad (Worst)
🎯 Example of Big O in Code
Consider an array of n elements:
