Skip to content

Commit 370488d

Browse files
committed
Space Time Complexity Analysis: Program 5 & 11
1 parent 620d46a commit 370488d

8 files changed

+444
-59
lines changed

22_space_time_complexity_analysis/05_time_complexity_bubble_sort.cpp

-59
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Topic - Time Complexity: IF & WHILE LOOP Examples
3+
4+
Find the time complexity of below functions:-
5+
6+
-------------------------------------------------------------------------------------
7+
//find GCD/HCF using while loop
8+
9+
void test1() | void test1()
10+
{ | {
11+
while(m != n) | for( ; m != n; )
12+
{ | {
13+
if(m>n) | if(m>n)
14+
m = m-n; | m = m-n;
15+
else | else
16+
n = n-m; | n = n-m;
17+
} | }
18+
} | }
19+
20+
____Assume______|___Instructions___
21+
m=6 & n=3 | 2 (time) Running when, n=3 m=6,3
22+
m=5 & n=5 | 0 (time) Not running
23+
m=16 & n=2 | 8 (time) Running when, n=2 m=16,14,12,10,8,6,4,2
24+
m=10 & n=50 | 5 (time) Running when, m=10 n=50,40,30,20,10
25+
26+
27+
Time Complexity = O(n/2)
28+
= O(n)
29+
30+
min. time = O(1) Best Case
31+
max. time = O(n) Worst Case
32+
33+
-------------------------------------------------------------------------------------
34+
35+
void test2(n)
36+
{
37+
if(n<5)
38+
{
39+
cout << 'A';
40+
}
41+
else
42+
{
43+
for(int i=0; i<n; i++)
44+
{
45+
cout << 'B';
46+
}
47+
}
48+
}
49+
50+
For, if case, statement is running 1 (time) i.e O(1) for if case
51+
else case, statement is running n (times) i.e O(n) for else case
52+
53+
Time Complexity = O(n)
54+
min. time = O(1) Best Case
55+
max. time = O(n) Worst Case
56+
57+
-------------------------------------------------------------------------------------
58+
Reference: https://www.youtube.com/watch?v=p1EnSvS3urU [Time Complexity - "While" & "if"]
59+
60+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Time Complexity Bubble Sort
3+
4+
How to do analysis?
5+
- Practical Approach : Run each and every algoirithm & then decide which one is better.
6+
NOTE: it may be infeasible to code all the algorithm & then check the complexity
7+
as it will waste a lot of time.
8+
- Theoretical Approach : Judge the complexity of algorithm before we write the code. [Mostly Preferred]
9+
10+
Eg: We will try to analyse bubble sort for a given input N.
11+
i.e calculate how much time does it take to execute the code for value N.
12+
13+
- So, we will count the number of insturctions because time is directly proportional to number of
14+
instruction processor execute & no of instruction is directly proportional to input N.
15+
i.e time -> instructions -> f(N)
16+
Thus, for each value of N, we will try to find out how much instuctions will it run as per the
17+
rule & condition mentioned in the code.
18+
19+
-------------------------------------------------------------------
20+
// bubble sort
21+
void bubble_sort(int arr[], int n)
22+
{
23+
// Outer Loop
24+
for (int i = 1; i < n; i++)
25+
{
26+
// Inner Loop
27+
for (int j=0; j < (n-i); j++)
28+
{
29+
// pairwise swapping
30+
if (arr[j] > arr[j + 1])
31+
{
32+
swap(arr[j], arr[j + 1]);
33+
}
34+
}
35+
}
36+
}
37+
38+
Assume, array size = n
39+
_when__i___ | ____j_(running)__
40+
1 | n-1 (times)
41+
2 | n-2 (times)
42+
3 | n-3 (times)
43+
... | ...
44+
... | ...
45+
n-2 | 3 (times)
46+
n-1 | 2 (times)
47+
n | 1 (times)
48+
49+
So, adding j values,
50+
= 1+2+3+4+....+n
51+
= (arithmetic progression:sum of first n natural numbers)
52+
= _n_*_(n+1)_
53+
2
54+
= _n^2_+_n_
55+
2
56+
= O(n^2)
57+
58+
Time Complexity = O(n^2)
59+
60+
61+
Worst Case
62+
- In the worst-case scenario, the outer loop runs O(n) times.
63+
- As a result, the worst-case time complexity of bubble sort is O(n*n) = O(n^2).
64+
65+
Best Case
66+
- In the best-case scenario, the array is already sorted, but just in case, bubble sort
67+
performs O(n) comparisons.
68+
- As a result, the time complexity of bubble sort in the best-case scenario is O(n).
69+
70+
Average Case
71+
- Bubble sort may require (n/2) passes and O(n) comparisons for each pass in the average case.
72+
- As a result, the average case time complexity of bubble sort is
73+
O(n/2 x n) = O(n/2 x n) = O(n/2 x n) = O(n/2 x n) = O(n^2).
74+
75+
Reference :-
76+
Lalitha Natraj - https://www.youtube.com/watch?v=Yffvd3pkTW4
77+
78+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Topic - Time Complexity Binary Search
3+
4+
Find the time complexity of below function:
5+
6+
----------------------------------------------------------------------------
7+
int binary_search(int arr[], int size, int key)
8+
{
9+
int start = 0;
10+
int end = size;
11+
12+
while (start <= end)
13+
{
14+
int mid = (start + end) / 2; // calculating the middle value
15+
16+
if (arr[mid] == key)
17+
{
18+
return mid;
19+
}
20+
else if (arr[mid] > key)
21+
{
22+
end = mid - 1;
23+
}
24+
else
25+
{
26+
start = mid + 1;
27+
}
28+
}
29+
return -1;
30+
}
31+
32+
Assume, array size = n
33+
__ Steps___ | ____Array Size______
34+
0 | n i.e = n/(2^0)
35+
1 | n/2 i.e = n/(2^1)
36+
2 | n/4 i.e = n/(2^2)
37+
3 | n/8 i.e = n/(2^3)
38+
3 | n/16 i.e = n/(2^4)
39+
... | ...
40+
... | ...
41+
(Suppose at k steps array size reached at 1)
42+
k steps | 1 i.e = n/(2^k)
43+
44+
Now, at k-steps,
45+
1 = n/(2^k)
46+
2^k = n
47+
Taking (log base2) both sides
48+
log(2^k) = logn
49+
k = logn
50+
51+
Time Complexity = O(logn)
52+
53+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
TOPIC - Time Complexity Using Recurrence Method
3+
4+
- Bubble Sort
5+
- Binary Search
6+
7+
-------------------------------------------------------------------
8+
// bubble sort
9+
_________________
10+
Array: |_|_|_|_|_|_|_|_|_|
11+
-------T(n)--------
12+
_________________
13+
|_|_|_|_|_|_|_|_|X|
14+
------T(n-1)----
15+
16+
If T(n) is the total time to sort the complete array.
17+
18+
Then this time include 2 parts:
19+
T(n) = k*n + T(n-1)
20+
- k*n = Time to move one element to the last (i.e k*n where k=some constant work, n=total array elements)
21+
- T(n-1) = Time taken to sort the smaller array
22+
23+
So, the recurrence is, T(n) = k*n + T(n-1)
24+
25+
Lets solve the recurrence:-
26+
T(n) = k*n + T(n-1)
27+
T(n-1) = k*(n-1) + T(n-2)
28+
T(n-2) = k*(n-2) + T(n-3)
29+
... = ... + ...
30+
... = ... + ...
31+
T(3) = k*(3) + T(2)
32+
T(2) = k*(2) + T(1)
33+
T(1) = k*(1) + 0
34+
---------------------------- // By adding all the above equations, we can get the total time
35+
T(n) = k*n + k*(n-1) + k*(n-2) + ...... + k*(1)
36+
= k*(n + n-1 + n-2 + ..... 3 + 2 + 1)
37+
= _k*_n*(n-1)_
38+
2
39+
= O(n^2)
40+
41+
Time Complexity = O(n^2)
42+
43+
44+
-------------------------------------------------------------------
45+
// binary search
46+
_________________
47+
Array: |_|_|_|_|_|_|_|_|_| size = n
48+
-------T(n)--------
49+
_________________
50+
|_|_|_|_|_|X|X|X|X| size = n/2
51+
--T(n/2)---
52+
53+
If T(n) is the total time to search the complete array.
54+
55+
Then this time include 2 parts:
56+
T(n) = k + T(n/2)
57+
- k = To convert a array from size n to size n/2 takes only constant time k because we just have find the mid.
58+
- T(n/2) = Time taken to do binary search on the smaller array
59+
60+
So, the recurrence is, T(n) = k*n + T(n-1)
61+
62+
Lets solve the recurrence:-
63+
T(n) = k + T(n/2)
64+
T(n/2) = k + T(n/4)
65+
T(n/4) = k + T(n/8)
66+
... = ... + ...
67+
... = ... + ...
68+
T(1) = k + 0
69+
---------------------------- // By adding all the above equations, we can get the total time
70+
logn
71+
T(n) = ∑ k
72+
i=1
73+
(NOTE: From coming from array size n to 1, we have taken logn steps)
74+
= k*logn
75+
= logn
76+
= O(logn)
77+
78+
Time Complexity = O(logn)
79+
80+
*/

0 commit comments

Comments
 (0)