Time and space complexity in simple way
When we look at any computer program ,on first hand it should perform its requirement that is functional criteria . To test we generate input data(test data or test case) and invoke function with input data(test data) as argument and validate output with expected value .
Performance of program depends processor and memory .Processor is directly proportional to amount of instruction (program statement ) to execute per unit time .Memory is directory proportional size of state of program under execution .For example when we open 2 huge game , code execution speed of processor shared by 2 games and state of 2 game save in memory (RAM).
On other way if you decrease number of instruction to execute on constant processor capacity, and if you reduce states of size of state of program on constant memory performance can improved .
The of number of instruction execution expressed by Time complexity and size of state of program allocated expressed by Space complexity .
Let's take an example on time complexity .
int x=n;
while (x>0){
x=x/2;
}
The loop will run till x=1 (N.B int 1/2 is 0) .The amount of iteration required to make x to 0.
if n=8 then on iteration x become 4 ,2,1 i,e 8/2,8/2^2,8/2^3
So it is 3 time loop will execute and we can get 3 by 8/2^3 =1 from that 3 is log8 base 2
hence for input N ,total number of iteration = time complexity =logN base 2
NB:Normally time complexity evaluated keeping mind of input(N) is huge ,so declaration ,assignment statements are ignored.It is basically focused on iterations evalution with huge inpute.This type of analysis is called Big-Oh analysis .O(logn)
Let's take an example on space complexity .
public void fun(int n){
if(n==0)
return ;
fun(n-1);
}
This is recursive function the number of stack it will generate on memory in otherwards number of state this program have is n(number of input) hence space complexity is n.O(n)
QA Analyst at Oracle
8 年Good One, thank you