Algorithm Algorithm Notes
Algorithm Algorithm Notes
1. Input: An algorithm usually takes one or more inputs, which are the data needed to perform the task.
2. Output: An algorithm produces one or more outputs, which are the results of the algorithm's processing.
5. Effectiveness: Every step of the algorithm should be basic enough to be carried out, in principle, by a person
using just paper and pencil.
Importance of Algorithms
Algorithms form the backbone of programming and computer science, allowing for the execution of
calculations, data processing, and decision-making in applications ranging from simple tasks to complex
systems
Time complexity is defined in terms of how many times it takes to run a given algorithm, based on the length
of the input. Time complexity is not a measurement of how much time it takes to execute a particular
algorithm because such factors as programming language, operating system, and processing power are also
considered.
Time complexity is a type of computational complexity that describes the time required to execute an
algorithm. The time complexity of an algorithm is the amount of time it takes for each statement to
complete.
Common notations for time complexity include Big O, which categorizes algorithms based on their growth
rates, such as O(1) for constant time, O(n) for linear time, and O(n2) for quadratic time.
Correctness of an algorithm means that the algorithm correctly solves the problem it was designed to solve. Proving
correctness ensures that for every possible input, the algorithm will produce the correct output as specified.
1. Partial Correctness:
o An algorithm is partially correct if, whenever it terminates, the result is correct according to the
problem specification.
o This is often proved using loop invariants, which are conditions that hold true before and after every
iteration of a loop within the algorithm.
2. Termination:
o An algorithm is said to terminate if it eventually stops after a finite number of steps for any input.
o Termination is usually shown by identifying a measure (often called a variant function) that
decreases with every step of the algorithm and is bounded below, ensuring that the algorithm
cannot continue indefinitely.
Partial Correctness:
Loop Invariant: At the start of each outer loop iteration, the last i elements are sorted, and the elements
before them are smaller than or equal to those sorted elements.
Proof:
o After the first pass, the largest element is placed in its correct position.
o This invariant holds true after every subsequent pass, gradually sorting the array.
o By the time the algorithm finishes, all elements are in their correct positions, proving that the output
is a sorted array.
Termination:
The outer loop runs n times, and the inner loop runs fewer times with each iteration.
Since both loops are bounded by the size of the array, and the inner loop strictly reduces the number of
comparisons, the algorithm must terminate after a finite number of steps.
Reliability: Ensures the algorithm behaves as expected under all circumstances, crucial for critical
applications like finance, healthcare, or safety systems.
Predictability: Helps predict the algorithm's behavior, especially when dealing with edge cases.
Optimization: Correctness proofs can often lead to the discovery of optimizations by understanding the
algorithm's behavior more deeply.
Conclusion:
Proving an algorithm's correctness is a fundamental step in algorithm design. It guarantees that the algorithm not
only works as intended but also that it handles all inputs, including edge cases, correctly. This assurance is vital in
both academic and practical applications where reliable and predictable performance is essential.
KOSARUJ’S ALGORTHM: