-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSA DAY 04
48 lines (41 loc) · 1.2 KB
/
DSA DAY 04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Time Complexity
======================================================
1) for(int i =0; i<n; i++){ ---> n+1 times
stmt; ---> n times
}
time complexity = O(n)
==========================================================
2) for(int i =n; i>0; i--){ ---> n+1 times
stmt; ---> n times
}
time complexity = O(n)
===========================================================
3) for(int i =1; i<n; i=i+2){ ---> n+1 times
stmt; ---> n/2 times
}
time complexity = O(n)
=============================================================
4) for(int i=0; i<n;i++){ --->n+1 times
for(int j =0;j<n;j++){ --->n*(n+1) times
stmt; ---> n*n times
}
}
time complexity = O(n^2)
=============================================================
5) for(int i=0; i<n;i++){
for(int j =0;j<i;j++){
stmt;
}
}
Explantion 1+2+3+...+n = n*(n+1)/2 = n^2+1/2
time complexity = O(n^2)
==============================================================
6) p=0;
for(int i=1; p<=n; i++){
p=p+i;
}
assume p>n stopping criteria
p=k(k+1)/2 >n
k^2>n
k> root n
time complexity = O(root n)