In mathematics and computer science, an algorithm is a finite sequence of well-defined instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing calculations and data processing.

You can implement this sequence of steps in a Programming Language.

Properties of Algorithms

AlgorithmProgram
Design timeImplementation
Need domain knowledgeNeed to understand code
Any language (formal better)Programming language
Hardware & software independentHardware & OS specific
Analyze complexityTesting required

Analysis of Algorithms

  • Priori Analysis: “From the earlier” in latin is about knowledge by deduction and reasoning.
  • Posteriori Analysis: “From the later” in latin is about knowledge by induction - observing how something works.
Priori AnalysisPosteriori Testing
Analysis of algorithmAnalysis of program
Independent of languageLanguage dependent
Hardware independentHardware dependent
Time & Space functionWatch time & bytes

How to analyse an algorithm

  1. Time - how long it takes to run
  2. Space - Amount of memory used
  3. Network consumpiton - amount of data transferred
  4. Power Consumpiton - how much power is needed
  5. CPU Registers - how much CPU time

Sample Algorithm to analyse

Simple algorithm to swap values of A and B:

swap(a, b){
    temp ← a;
    a ← b;
    b ← temp;
}

Note: there are no data types, and assignment can be specified using ←, = or := symbols. If we look above:

  • there are 3 lines in the function - so time is f(n) = 3 (we assume constant time).
  • there are 3 variables a,b,temp so space is equal to 3 - s(n) = 3.

When we translate that to code in C:

#include <stdio.h>

void swap(int *ap, int *bp){
    int temp = *ap;
    *ap = *bp;
    *bp = temp;
}

int main(){
    int a = 8;
    int b = 9;

    swap(&a, &b);

    printf("A=%d, B=%d",a,b);

    return 0;
}
A=9B=8

Sum of Array Example

Using this array:

img

When we translate that to code in C:

#include <stdio.h>

int main(){
  int sum = 0;
  int arr[] = {8, 3, 9, 7, 2};
  int n = sizeof(arr) / sizeof(arr[0]);

  for (int i = 0; i < n; i++)
    sum += arr[i];

  printf("Sum of array = %d", sum);

  return 0;
}

Sum of array = 29

Characteristics of Algorithm

  1. Input - 0 or more
  2. Ouput - at least 1
  3. Definiteness - can’t have unknown or imaginary values
  4. Finiteness - must terminate at some point.
  5. Effectiveness - don’t do anything unnecessary

Recommended Resources