Complexity of Algorithms
aka: performance analysis or efficiency of a program
To analyse an algorithm
- First determine the operations the are included like additions,subtractions,multiplication and divisoin to find
their relative cost.
- Comparisions generally take longer
- The total time taken to compute the comparision of 2 strings generally depends on the length of the string
- If the comparision is done among just characters , it is bounded by a constant time
An algorithm can be analysed in two ways :
The correctness of an algorithm
By measureing time and space complexity of an algorithm
Two phases are generally required :
Priori analysis
Posteriori Analysis
Priori Analysis
Give an initial state input data
We need to determine the total time some statements in the middle of the program will spend for the execution
Requirements:
- The statemtns frequency count [ The number of times a statement will execute ]
- The time taken for one execution
The product of these two numbers is the total time
It ignores the following factors
- The machine's cpu process capacity and speed
- The programming language and compiler that is been used
The notation used in priori analysis are :
- Big-oh [O]
- Omega [Ω]
- Theta [Θ]
- Small-oh [o]
Posteriori Analysis
We will collect the actual statistics about the algortihm , conjunction of the time and space while executing
Once the algorithm is written it has to be tested, which consists of two phases :
- Debugging: It is the process of correcting the algorithm if a fault result occurs . Independent of the machine, programming langauge, compiler used
- Profiling:The process of executing the program and measuring the actual time taken by the algorithm to process the data is called profiling [ could include measuring the space as well ] . Dependent of the machine, programming language and the compiler used
In order to perform these we need to consider :
- Time complexity
- Space complexity
Space complexity
The amount of memory required by the algorithm to run to completion
Ignoring the space required for input and output , since we use this information to compare different algorithm
There are two compoenents for the space required by an algorithm
The Fixed static part
- Independent of the characterisitcs of the input and output
- Instructions space for variables , constants, fixed size component variables
The Variable dynamic part
- The space required by the a component variable who size depends on the particular problem instance at runtime being solved,
- The space needed by the reference variable
- The recurrence stack
The overall space required for an algorithm is the sum of both the fixed and variable [ i.e static and dynamic ] part of the storage .
Time Complexity
The amount of time needed to run the program is termed as time efficiency or time complexity
The total time taken by the program is the sum of compile time and the runtime
The compile time does not depend on the instance characterisitcs and it can be assumed as a constant factor , so we ignore it
Bubble sort
The most simplest form of sorting algorithm
Works optimal only on small arrays
With each iterations the value moves on top of the array just like a bubble


Code
void bubble sort(int a[],int n){
int pass,i;
for (pass=0;pass < n-1;pass++){
for (i=0;i<n-pass-1;i++){
if (a[i]>a[i+1])
swap(a[i],a[i+1]);
}
}
}
The time complexity for bubble sort is :
its compares every adjacent elements - all n elements , but since it compares 2 elements in the first go itself : n-1
Once the above step reaches the end of the array - one elements [largest,smallest] is sorted , so the next iterations with ignore that [first/ last] element making the second iteration : n-2
So the final time complexity is (n-1)+(n-2)+(n-3)+(n-4)=(n(n-1))/2
that is (n² - n )/ 2 ,
Since n² is larger than n and neglecting the constant "2"
Bubble Sort's Time Complexity : n²
No merge sort is not for begineer - every person who takes birth can sort using merge sort - its not even an algorithm at this points
A step count is the steps per execution i.e the number of times that particular line has been executed in one instance of "program execution" , summation of all the number of executions of these lines in the program would be the step count of that program . The count that we use to determine the time complexity is the operation count