Definition and Purpose 🎯
An algorithm is a well-defined, step-by-step procedure or formula for solving a specific problem or achieving a particular outcome. In computer science, an algorithm is the foundational logic that is translated into a program. It acts as a blueprint for the code, ensuring that the problem is solved correctly and efficiently.
Before writing a single line of code, a programmer must first design a clear and correct algorithm.
Characteristics of a Valid Algorithm ✅
To be considered a valid algorithm, a set of instructions must have the following five properties:
·        Input: An algorithm must have zero or more well-defined inputs—the data it works with.
·        Output: It must produce at least one well-defined output—the result of the computation.
·        Finiteness: It must terminate after a finite number of steps. An algorithm that never ends is not useful.
·        Definiteness (Unambiguous): Every step of the algorithm must be precisely defined and have only one interpretation. There should be no ambiguity.
·        Effectiveness: Each step must be basic enough that it can be carried out by a person in a finite amount of time with just a pen and paper.
Examples of Algorithms 📚
Algorithms can vary greatly in complexity. They are often expressed in pseudocode, which is a simplified, language-agnostic way of writing out the steps.
1. Basic Example: Area of a Circle
·        Problem: Calculate the area of a circle given its radius.
·       Â
Logic: The solution uses the mathematical formula Area=π×r2,
where r is the radius and π (pi) is a constant value (approx. 3.14159).
·        Pseudocode:
START // Define constants and variables SET PI = 3.14159 DECLARE Radius, Area  // Input READ Radius  // Process Area = PI * Radius * Radius  // Output PRINT "The area is", AreaEND2. Moderate Example: Leap Year Check
·        Problem: Determine if a given year is a leap year.
·        Logic: A year is a leap year if it meets one of two conditions:
1.  It is divisible by 4 but not by 100.
2.  It is divisible by 400. The order of these checks is important for the logic to be correct.
·        Pseudocode:
START // Input READ Year  // Process and Decision IF (Year % 400 == 0) OR ((Year % 4 == 0) AND (Year % 100 != 0)) THEN   PRINT Year, "is a Leap Year." ELSE   PRINT Year, "is not a Leap Year." ENDIFEND3. Advanced Example: Prime Factorization of a Number
·        Problem: Find and print all the prime factors of a given integer. For example, the prime factors of 12 are 2, 2, 3.
·        Logic: The algorithm iteratively finds the smallest prime that divides the number.
1.  Start by repeatedly dividing the number by 2 until it's no longer even. Print '2' for each successful division.
2.  The number must now be odd. Loop from 3 up to the square root of the remaining number, incrementing by 2 (to check only odd divisors).
3.  For each odd divisor i, repeatedly divide the number by i until it's no longer divisible, printing i each time.
4.  If the number that remains after the loop is greater than 2, it must be a prime factor itself.
·        Pseudocode:
START // Input READ Number  PRINT "Prime factors are:"  // Process 1: Check for divisibility by 2 WHILE Number % 2 == 0 DO   PRINT 2   Number = Number / 2 ENDWHILE  // Process 2: Check for odd factors from 3 upwards FOR i from 3 to SQRT(Number) step 2 DO   WHILE Number % i == 0 DO     PRINT i     Number = Number / i   ENDWHILE ENDFOR  // Process 3: Handle the case where Number is a prime > 2 IF Number > 2 THEN   PRINT Number ENDIFEND