UNIT I
UNIT: Algorithms & Complexity Analysis
1. Algorithm
Definition
An algorithm is a finite sequence of well-defined, unambiguous steps designed to solve a specific problem or perform a computation.
👉 In simple words:
An algorithm is a step-by-step procedure to convert input into output.
Example (Finding Maximum of Two Numbers)
-
Start
-
Read two numbers
aandb -
If
a > b, printa -
Else print
b -
Stop
2. Characteristics of an Algorithm
A good algorithm must satisfy the following properties:
-
Input
-
An algorithm can have zero or more inputs.
-
Inputs are externally supplied.
-
-
Output
-
Produces at least one output.
-
Output is the solution to the problem.
-
-
Definiteness
-
Each step must be clear and unambiguous.
-
No confusion in instructions.
-
-
Finiteness
-
The algorithm must terminate after a finite number of steps.
-
-
Effectiveness
-
Each operation must be simple and executable in finite time.
-
-
Correctness
-
Produces the correct output for all valid inputs.
-
-
Generality
-
Should solve all instances of the problem, not just a single case.
-
3. Complexity Analysis
Definition
Complexity analysis is the process of determining how much time and memory an algorithm requires as a function of the input size.
There are two main types:
-
Time Complexity
-
Space Complexity
4. Time Complexity
Definition
Time complexity measures the amount of time an algorithm takes to execute as the input size increases.
It is expressed as a function of input size n.
Cases of Time Complexity
-
Best Case – Minimum time required
-
Average Case – Average time taken
-
Worst Case – Maximum time required (most important for analysis)
Example
Linear search in an array of size n:
-
Best case:
O(1)(element found at first position) -
Worst case:
O(n)(element found at last position or not found)
5. Space Complexity
Definition
Space complexity measures the total memory used by an algorithm during execution.
Components of Space Complexity
-
Fixed Part
-
Variables, constants, program code
-
-
Variable Part
-
Dynamically allocated memory
-
Recursive stack space
-
Example
-
Iterative algorithm → less space
-
Recursive algorithm → extra stack space
6. Asymptotic Notations
Definition
Asymptotic notations describe the growth rate of time or space complexity as input size n becomes very large.
They help compare algorithms independent of machine or language.
6.1 Big-O Notation (O)
Definition
Big-O notation represents the upper bound (worst-case) time complexity of an algorithm.
Example
-
Linear search →
O(n) -
Bubble sort →
O(n²)
👉 Meaning: The algorithm will not take more than this time.
6.2 Big-Omega Notation (Ω)
Definition
Big-Omega represents the lower bound (best-case) time complexity.
Example
-
Linear search (best case) →
Ω(1) -
Binary search →
Ω(1)
👉 Meaning: The algorithm will take at least this much time.
6.3 Big-Theta Notation (Θ)
Definition
Big-Theta represents the tight bound, where best and worst cases grow at the same rate.
Example
-
Binary search →
Θ(log n) -
Merge sort →
Θ(n log n)
👉 Meaning: Exact order of growth.
7. Common Time Complexities
| Complexity | Name | Example |
|---|---|---|
| O(1) | Constant | Accessing array element |
| O(log n) | Logarithmic | Binary search |
| O(n) | Linear | Linear search |
| O(n log n) | Linearithmic | Merge sort |
| O(n²) | Quadratic | Bubble sort |
| O(2ⁿ) | Exponential | Recursive Fibonacci |
8. Importance of Complexity Analysis
-
Helps select the best algorithm
-
Improves program efficiency
-
Saves time and memory
-
Essential for large-scale applications
1. Data Structure
Definition
A data structure is a way of organizing, storing, and managing data in a computer so that it can be accessed and modified efficiently.
👉 In simple words:
A data structure decides how data is stored and how operations are performed on it.
Example
-
Array → stores elements in contiguous memory
-
Linked List → stores elements using pointers
2. Types of Data Structures
2.1 Primitive Data Structures
Basic data types supported by programming languages.
Examples:
-
Integer
-
Float
-
Character
-
Boolean
2.2 Non-Primitive Data Structures
Used to store collections of data.
a) Linear Data Structures
Data elements are arranged sequentially.
Examples:
-
Array
-
Linked List
-
Stack
-
Queue
b) Non-Linear Data Structures
Data elements are arranged hierarchically or graph-like.
Examples:
-
Tree
-
Graph
-
Heap
-
Hash Table
2.3 Static vs Dynamic Data Structures
| Static | Dynamic |
|---|---|
| Fixed size | Size can grow or shrink |
| Memory allocated at compile time | Memory allocated at run time |
| Example: Array | Example: Linked List |
3. Abstract Data Type (ADT)
Definition
An Abstract Data Type (ADT) is a logical description of a data structure, defining:
-
What operations are performed
-
Not how they are implemented
👉 ADT focuses on behavior, not implementation.
Examples of ADT
Stack ADT
-
Operations:
-
push()– insert element -
pop()– remove element -
peek()– view top element -
isEmpty()
-
Queue ADT
-
Operations:
-
enqueue() -
dequeue() -
front()
-
ADT vs Data Structure
| ADT | Data Structure |
|---|---|
| Logical model | Physical implementation |
| Specifies operations | Specifies memory structure |
| Language independent | Language dependent |
4. Introduction to Linked Lists
Definition
A linked list is a dynamic linear data structure where elements are stored in non-contiguous memory locations and are connected using pointers (links).
Each element is called a node.
Structure of a Node
A node consists of:
-
Data field – stores the value
-
Link field – stores address of next node
Types of Linked Lists
-
Singly Linked List
-
Doubly Linked List
-
Circular Linked List
(Usually syllabus starts with Singly Linked List)
5. Representation of Linked List in Memory
Memory Representation
-
Nodes are stored anywhere in memory
-
Each node points to the next node
-
First node is pointed by HEAD
-
Last node points to NULL
Key Points
-
Memory is allocated dynamically
-
No need for contiguous memory
-
Insertion and deletion are efficient
6. Linked List vs Array
Comparison Table
| Feature | Array | Linked List |
|---|---|---|
| Memory allocation | Contiguous | Non-contiguous |
| Size | Fixed | Dynamic |
| Access | Random access | Sequential access |
| Insertion | Costly | Efficient |
| Deletion | Costly | Efficient |
| Memory usage | Less overhead | Extra memory for pointers |
| Cache friendly | Yes | No |
7. Advantages of Linked List
-
Dynamic size
-
Efficient insertion and deletion
-
No memory wastage due to fixed size
8. Disadvantages of Linked List
-
Extra memory for pointers
-
No direct access to elements
-
More complex implementation
9. Applications of Linked List
-
Implementation of stacks and queues
-
Dynamic memory allocation
-
Polynomial manipulation
-
Graph representation (Adjacency list)
10. Summary
-
Data structures organize data efficiently
-
ADT defines what operations are needed
-
Linked list is a dynamic data structure
-
Arrays are faster for access; linked lists are better for insertion/deletion
Ruby assignments can be challenging when students are learning programming logic, syntax, and application development at the same time. Many learners find ruby assignment help experts useful for understanding difficult coding concepts and debugging problems. Good guidance focuses on explaining why a particular solution works rather than simply providing code. This approach helps students strengthen their programming skills, understand Ruby more effectively, and become more confident when handling future development tasks independently.
ReplyDelete