โก Algorithms
Understand how problems are solved step-by-step, how algorithms are analyzed, and how searching, sorting and recursion work.
๐ง 1. Introduction to Algorithms
An algorithm is a finite sequence of well-defined steps used to solve a problem or perform a computation.
Algorithm = A clear step-by-step method for solving a particular problem.
โญ 2. Characteristics of an Algorithm
๐ฅ Input
Zero or more input values can be supplied.
๐ค Output
The algorithm should produce a useful result.
๐ Definiteness
Each step should be clear and unambiguous.
๐ Finiteness
The process should eventually terminate.
๐งฉ 3. Basic Algorithm Design
Algorithm design involves creating an effective sequence of operations to solve a problem.
- ๐ Understand the problem.
- ๐ฅ Identify inputs.
- ๐ค Identify outputs.
- ๐งฉ Break the problem into smaller tasks.
- โ๏ธ Write an algorithm or pseudocode.
- ๐ป Implement the solution.
- ๐งช Test the solution.
- โก Improve efficiency if necessary.
๐ 4. Algorithm Analysis
Algorithm analysis helps determine how efficiently an algorithm uses computational resources.
โฑ๏ธ Time Complexity
Describes how the number of operations grows as the input size increases.
๐พ Space Complexity
Describes how additional memory usage grows with input size.
โฑ๏ธ 5. Time Complexity
Time complexity describes the growth of an algorithm's running time with respect to input size.
๐ข Constant
Constant growth
๐ต Logarithmic
Slow growth
๐ Linear
Proportional growth
๐ด Quadratic
Faster growth
๐ 6. Searching Algorithms
Searching algorithms locate a target element within a collection of data.
๐ Linear Search
Checks elements sequentially until the target is found or the collection ends.
โฑ๏ธ Worst case: O(n)โก Binary Search
Repeatedly divides a sorted search range into two parts.
โฑ๏ธ Worst case: O(log n)๐ 7. Sorting Algorithms
Sorting means arranging data elements into a particular order, such as ascending or descending.
๐ซง Bubble Sort
Compares adjacent elements and swaps them when they are in the wrong order.
๐ Insertion Sort
Inserts elements into their appropriate position in the sorted part.
๐ฏ Selection Sort
Selects the appropriate minimum or maximum element and places it into position.
โก Quick Sort
Uses partitioning around a pivot to divide the problem.
โป๏ธ 8. Recursion
Recursion is a technique in which a function calls itself to solve a smaller version of a problem.
A recursive solution needs a suitable base case to stop the recursive calls.
๐ง 9. Basic Algorithmic Problem Solving
- ๐ Understand the problem.
- ๐ฅ Identify the input.
- ๐ค Identify the expected output.
- ๐งฉ Divide the problem into smaller parts.
- โ๏ธ Create pseudocode or an algorithm.
- ๐ป Write the program.
- ๐งช Test with different inputs.
- โก Analyze and improve the solution.
Problem โ Plan โ Algorithm โ Code โ Test โ Improve
๐ 10. Important Algorithm Comparison
| Algorithm | Category | Common Complexity | Key Idea |
|---|---|---|---|
| Linear Search | ๐ Searching | O(n) worst case | Sequential checking |
| Binary Search | ๐ Searching | O(log n) worst case | Divide search range |
| Bubble Sort | ๐ Sorting | O(nยฒ) worst case | Adjacent swapping |
| Insertion Sort | ๐ Sorting | O(nยฒ) worst case | Insert into sorted part |
| Selection Sort | ๐ Sorting | O(nยฒ) worst case | Select minimum/maximum |
| Quick Sort | ๐ Sorting | O(nยฒ) worst case | Partition around pivot |
๐ Quick Revision
- ๐ง Algorithm โ Step-by-step problem-solving method.
- โญ Characteristics โ Input, Output, Definiteness, Finiteness.
- โฑ๏ธ Time Complexity โ Running-time growth.
- ๐พ Space Complexity โ Additional memory growth.
- ๐ Linear Search โ Sequential search.
- โก Binary Search โ Search by repeatedly dividing a sorted range.
- ๐ Sorting โ Arranging data.
- โป๏ธ Recursion โ Function calling itself.
- ๐ Big-O โ Asymptotic growth notation.