ALGORITHM
ALGORITHM
Input: An algorithm should have zero or more inputs, which are the initial
values or data the algorithm uses to work.
Output: An algorithm must produce at least one output, which is the result or
solution after the algorithm completes its steps.
3. Types of Algorithms
Algorithms can be classified into various types depending on their application:
o Bubble Sort
o Quick Sort
o Merge Sort
o Insertion Sort
o Linear Search: Checks each element one by one.
o Binary Search: A more efficient method that works on sorted data by repeatedly
dividing the search space in half.
o Example: The factorial algorithm that calculates the product of all integers up to a
given number.
Greedy Algorithms: These algorithms make the best possible choice at each
step with the hope of finding the global optimum.
o Example: The coin change problem, where the algorithm picks the largest possible
coin at each step.
When designing algorithms, it is helpful to use tools like pseudocode and flowcharts:
css
Copy code
Start
Read input A, B
Sum = A + B
Print Sum
End
css
Copy code
Algorithms are often evaluated based on their time complexity and space
complexity. These metrics describe the efficiency of an algorithm in terms of how
fast it executes (time) and how much memory it uses (space).
Time Complexity: Describes how the runtime of an algorithm grows with the
size of the input. Common time complexities include:
o O(1): Constant time (the algorithm takes the same time regardless of the input size).
o O(n): Linear time (the algorithm’s runtime increases proportionally to the input size).
o O(n²): Quadratic time (commonly seen in algorithms that involve nested loops).
Example:
Bubble Sort has a time complexity of O(n²), while Merge Sort has a time complexity of O(n
log n), making Merge Sort more efficient for large datasets.
Binary search is a very efficient algorithm for finding an element in a sorted array.
Steps:
3. Output: The index of the target element or a message indicating that the element is not
found.
Pseudocode:
vbnet
Copy code
Start
Set low to 0
Set high to length of array - 1
While low <= high:
mid = (low + high) / 2
If array[mid] == target:
Return mid
Else If array[mid] < target:
low = mid + 1
Else:
high = mid - 1
Return "Not found"End
Time Complexity: O(log n), because the algorithm cuts the search space in half each
time.
Efficiency: An efficient algorithm can save both time and resources, making processes faster
and less resource-intensive.
Scalability: As data grows, well-designed algorithms ensure that systems can handle larger
workloads without significant slowdowns.
Automation: Algorithms enable automation in tasks like data processing, image recognition,
and decision-making.
9. Conclusion