0% found this document useful (0 votes)
9 views10 pages

Algorithm Algorithm Notes

An algorithm is a step-by-step set of instructions designed to solve a specific problem, characterized by input, output, definiteness, finiteness, and effectiveness. Time complexity measures the time an algorithm takes based on input length, while space complexity measures the memory required, both often represented using Big O notation. The correctness of an algorithm ensures it reliably produces the correct output for all possible inputs, which is vital for applications in critical fields.

Uploaded by

Hafsa Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Algorithm Algorithm Notes

An algorithm is a step-by-step set of instructions designed to solve a specific problem, characterized by input, output, definiteness, finiteness, and effectiveness. Time complexity measures the time an algorithm takes based on input length, while space complexity measures the memory required, both often represented using Big O notation. The correctness of an algorithm ensures it reliably produces the correct output for all possible inputs, which is vital for applications in critical fields.

Uploaded by

Hafsa Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Definition of an Algorithm

 An algorithm is a step-by-step technique or set of instructions designed to solve a specific problem or


perform a task. It comprises a finite sequence of well-defined instructions that leads to the completion of the
task, often independent of programming language

Key Characteristics of Algorithms:

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.

3. Definiteness: Each step of the algorithm is clearly and unambiguously defined.

4. Finiteness: The algorithm must terminate after a finite number of steps.

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

What Is Time Complexity?

 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.

What Is Space Complexity?

 When an algorithm is run on a computer, it necessitates a certain amount of memory space.


 The amount of memory used by a program to execute it is represented by its space complexity.
 Because a program requires memory to store input data and temporal values while running.
 Like time complexity , space complexity is represented using the notations 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.
Example: Bubble Sort Algorithm
 Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order.
 This process is repeated until the list is sorted.

Time Complexity of Bubble Sort:


1. Worst-Case Time Complexity (O(n^2)):
o When the array is in reverse order, the algorithm needs to perform the maximum number of swaps.
o For every element, it compares with every other element, leading to n * (n-1)/2 comparisons.
o This results in O(n^2) time complexity.
2. Best-Case Time Complexity (O(n)):
o If the array is already sorted, no swaps are needed, and the algorithm only needs to make one pass
through the list.
o This results in O(n) time complexity.
3. Average-Case Time Complexity (O(n^2)):
o On average, the number of comparisons is still quadratic.
Space Complexity of Bubble Sort:
 Auxiliary Space (O(1)):
o Bubble Sort is an in-place sorting algorithm, meaning it requires a constant amount of extra memory
space, regardless of the input size.
Summary
 Time Complexity:
o Worst-case: O(n^2)
o Best-case: O(n)
o Average-case: O(n^2)
 Space Complexity:
o Auxiliary Space: O(1)
Example Walkthrough:
Consider an array [4, 3, 2, 1]. The Bubble Sort algorithm will sort this array in the following way:
1. First Pass:
o Compare 4 and 3, swap.
o Compare 4 and 2, swap.
o Compare 4 and 1, swap.
o Array becomes [3, 2, 1, 4].
2. Second Pass:
o Compare 3 and 2, swap.
o Compare 3 and 1, swap.
o No need to compare the last element since it's already in the correct position.
o Array becomes [2, 1, 3, 4].
3. Third Pass:
o Compare 2 and 1, swap.
o Array becomes [1, 2, 3, 4].
4. Fourth Pass:
o No swaps needed; the array is sorted.
The algorithm took 4 passes, with each pass involving multiple comparisons, which results in O(n^2) time complexity.
The algorithm used a constant amount of space (O(1)) for its execution.
Correctness Proof of an Algorithm

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.

Two Main Aspects of Correctness:

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.

Example: Correctness of Bubble Sort

To illustrate the correctness proof, consider the Bubble Sort algorithm:

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 Before any iteration, the array is unsorted.

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.

Why Emphasize Correctness?

 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:

You might also like