Skip to content

Commit 1a8ef88

Browse files
committed
Space Time Complexity Analysis: Program 13 & 14
1 parent 7ae7ab2 commit 1a8ef88

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
3+
TOPIC - Space & Time Complexity - Merge Sort
4+
5+
-----------------------------------------------------------------------------------------------------
6+
// Time Complexity
7+
8+
___________
9+
merge_sort(arr,s,e) |_|_|_|_|_|_| size = n
10+
{ ___/_ _\___
11+
while(s<e){ |_|_|_| |_|_|_| // divide into n/2
12+
m = (s+e)/2; . . ...
13+
merge_sort(arr,s,m); . . // merge
14+
merge_sort(arr,m+1,e); __\______/_
15+
merge(arr,m,s,e) |_|_|_|_|_|_| // merge
16+
}
17+
}
18+
19+
If T(n) is the total time to sort the complete array.
20+
21+
Then this time include 3 parts:
22+
divide divide merge
23+
T(n) = T(n/2) + T(n/2) + T(merge array of size n/2 = n iterations)
24+
25+
So,
26+
T(n) = 2T(n/2) + T(merge array of size n/2 = n iterations)
27+
T(n/2) = 2T(n/4) + T(merge array of size n/4 = n/2 iterations)
28+
T(n/4) = 2T(n/8) + T(merge array of size n/8 = n/4 iterations)
29+
...
30+
...
31+
...
32+
Now, the recurrence is, T(n) = 2T(n/2) + k*n
33+
divide merge
34+
35+
Lets solve the recurrence:-
36+
T(n) = 2T(n/2) + k*n
37+
2T(n/2) = 4T(n/4) + 2k*n/2 (after multiplying and dividing both side by 2)
38+
4T(n/4) = 8T(n/8) + 4k*n/4 (after multiplying and dividing both side by 2)
39+
...
40+
...
41+
T(1) = 0 + k*n (Note: when n=1, time taken to divide is 0)
42+
----------------------------- // By adding all the above equations on both sides & cancellation, we can get the total time
43+
logn
44+
T(n) = ∑ k*n
45+
i=1
46+
(NOTE: From coming from array size n to 1, we have taken logn steps)
47+
= kn*logn
48+
= nlogn
49+
= O(nlogn)
50+
51+
Time Complexity = O(nlogn)
52+
53+
54+
-----------------------------------------------------------------------------------------------------
55+
// Space Complexity
56+
57+
Now, The space in merge sort algorithm is used by two types of functions:
58+
59+
Space Complexity
60+
/ \
61+
Recursive Call stack Auxiliary Array in merge function
62+
| O(n)
63+
|
64+
(At any give time, we can have atmost logN function calls in call stack)
65+
O(logn)
66+
67+
So, Space Complexity = O(logn + n)
68+
= O(n)
69+
70+
-----------------------------------------------------------------------------------------------------
71+
Thus,
72+
Time Complexity = O(nlogn)
73+
Space Complexity = O(n)
74+
75+
76+
-----------------------------------------------------------------------------------------------------
77+
78+
Reference - https://www.youtube.com/watch?v=279cymdrmdg [GATE Applied Course]
79+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
3+
TOPIC - Space & Time Complexity - Quick Sort
4+
5+
-----------------------------------------------------------------------------------------------------
6+
// Time Complexity
7+
___________
8+
|_|_|_|_|_|X| size = n
9+
| p (pivot)
10+
_____|_____
11+
|_|_|X|_|_|_|
12+
p
13+
14+
15+
If T(n) is the total time to sort the complete array.
16+
17+
Then this time include 3 parts:
18+
divide divide partition function
19+
T(n) = T(n-p) + T(p-1) + k*n
20+
21+
Now, assume p (i.e pivot) is always at MID,
22+
So, p = n/2
23+
T(n) = T(n -_n_) + T(_n_- 1) + k*n
24+
2 2
25+
T(n) = T(n/2) + T(n/2) + k*n
26+
T(n) = 2T(n/2) + k*n
27+
28+
Now, the recurrence is, T(n) = 2*T(n/2) + k*n
29+
divide partition function
30+
31+
Lets solve the recurrence:-
32+
T(n) = 2T(n/2) + k*n
33+
2T(n/2) = 4T(n/4) + 2k*n/2 (after multiplying and dividing both side by 2)
34+
4T(n/4) = 8T(n/8) + 4k*n/4 (after multiplying and dividing both side by 2)
35+
...
36+
...
37+
T(1) = 0 + k*n (Note: when n=1, time taken to divide is 0)
38+
----------------------------- // By adding all the above equations on both sides & cancellation, we can get the total time
39+
logn
40+
T(n) = ∑ k*n
41+
i=1
42+
(NOTE: From coming from array size n to 1, we have taken logn steps)
43+
= kn*logn
44+
= nlogn
45+
= O(nlogn)
46+
47+
Time Complexity = O(nlogn) (For Average Case)
48+
49+
-----------------------------------------------------------------------------------------------------
50+
_________
51+
Worst Case => |1|2|3|4|5|
52+
p
53+
54+
divide divide partition function
55+
T(n) = T(n-p) + T(p-1) + k*n
56+
57+
putting p = n-1
58+
T(n) = T(n-n-1) + T(n-1-1) + k*n
59+
= T(1) + T(n-2) + k*n
60+
= T(n-1) + k*n
61+
62+
Now, the recurrence is, T(n) = T(n-1) + k*n
63+
64+
Lets solve the recurrence:-
65+
T(n) = T(n-1) + k*n
66+
T(n-1) = T(n-2) + k*(n-1)
67+
...
68+
...
69+
T(1) = 0 + k
70+
------------------------- (after adding & cancellation)
71+
T(n) = k + 2k + 3k +...+ (n-1)k + nk)
72+
= k*(1+2+3+...n)
73+
= O(n^2)
74+
75+
Time Complexity = O(nlogn) (For Worst Case)
76+
(NOTE: To overcome this worst case using "Randomized Quick Sort"
77+
In which we shuffle the entire array before sorting)
78+
79+
-----------------------------------------------------------------------------------------------------
80+
// Space Complexity
81+
82+
We know quick sort is a Inplace Sorting Algorithm (i.e it doesn't use extra array like merge sort)
83+
84+
Space Complexity = Recursive Call stack
85+
= (At any give time, we can have atmost logN function calls in call stack)
86+
= O(logn)
87+
88+
So, Space Complexity = O(logn)
89+
90+
-----------------------------------------------------------------------------------------------------
91+
Thus,
92+
Time Complexity = O(nlogn) (For average case)
93+
= O(n^2) (For Worst Case) (Solution: Use Randomized Quick Sort)
94+
Space Complexity = O(logn)
95+
96+
97+
-----------------------------------------------------------------------------------------------------
98+
*/

0 commit comments

Comments
 (0)