# Introduction to Algorithms

# Introduction

An algorithm is a step by step procedure to solve a problem. In real life we illustrate algorithm definition with a recipe. To make any recipe, we need to take a set of ingredients which are referred to as input, and we need to follow instructions in some order to produce the desired recipe or result, which is called as output. Similarly in mathematics and computer science an algorithm is defined as a set of steps which takes some value or set of values (known as input) and produces some value or set of values(known as output).

# Characteristics

**Input:**

An algorithm has zero or more input. The input of an algorithm should be well-defined and it should be any form of data structure.

**Output:**

An algorithm must produce at least one or more outputs. The output produced by an algorithm is again in the form of any data structure.

**Finiteness :**

An algorithm must contain a finite number of steps to complete the problem. That means an algorithm should get terminated after executing a finite number of steps and it should produce the desired output.

**Definiteness :**

Each step of an algorithm must be clear and unambiguous. For example, if we want to sort numbers - sort numbers is not the clear goal, we need to specify whether we need to sort in ascending order or descending order. Thus each step of an algorithm must be precisely defined.

**Effectiveness :**

Each step of an algorithm must be doable (possible to do). For computers there are some mathematical operations which are not possible to do, like divide a number by zero value. These operations should not be part of any algorithm.

Based on the above characteristics, An algorithm is a procedure with a finite set of well-defined steps to solve a problem in which the initial state of the problem is known and the procedure will terminate in a defined end state.

# Efficiency

The computational complexity and efficiency of the algorithm are important in computing, and this depends on suitable Data Structures. The efficiency of an algorithm is always represented in terms of space complexity and time complexity of the algorithm.

## Time Complexity

Time complexity is a function which describes the amount of time consumed to run an algorithm in terms of the amount of the input to the algorithm.

## Space Complexity

Space complexity is a function which describes the amount of memory or space used to run an algorithm in terms of the amount of input to the algorithm

# Example : Even Odd Checker

## Problem Statement

1Problem Description:2Find whether a given number is even or not. Let’s identify input, output and the steps to be performed.34Input : A number.56Output : Even/Odd .78Explanation :9 If the given number is even, the output should be Even,10 If not then the output should be Odd.

## Algorithm

1Step 1 : Start2Step 2 : Read Input number3Step 3 : rem = number%2 (Initialize remainder as rem)4Step 4 : If rem = 0, print Even5Step 5 : Else print Odd.6Step 6 : End

## Flow Chart

## Code Implementation

- Java
- Python
- C

1import java.util.Scanner;23public class EvenOdd {45 public static void main(String[] args) {6 Scanner sc = new Scanner(System.in);7 System.out.println("Enter Number :");8 int number = sc.nextInt();910 int rem = number % 2 ;11 if (rem == 0) {12 System.out.printf("%d is Even\n", number);13 }else {14 System.out.printf("%d is Odd\n", number);15 }16 }17}