-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexample_18.py
76 lines (63 loc) · 1.54 KB
/
example_18.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def alg(data):
if data == []:
return 0
first_val = data[0]
return first_val + alg(data[1:])
print(alg([5, 4, 3, 2, 1, 0])) # 15
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# Time complexity: O(n), I believe.
# Space complexity: O(n^2).
"""
Explanation for space complexity:
"Every time we make a call into alg(), we get new space for local variables.
So any local variable that was O(1) on its own, now gets n copies and becomes O(1*n) or O(n).
But that's not all. When we slice data on the recursive call, we get a new list.
So that means every call we're getting another copy of the list, and each copy is O(n) space as well.
And we're making n recursive calls, so we need n copies of O(n) space, which comes to O(n*n) or O(n^2) space."
"""
"""
"Sure, it's not a complete copy of the list. It's missing the first element. So each call has a shorter and shorter list, like:
[ 1, 2, 3, 4 ]
[ 2, 3, 4 ]
[ 3, 4 ]
[ 4 ]
[ ]
So that means the first call wasn't really O(n), but was more like O(0.8*n), and the second was O(0.6*n), and so on.
But recall that we drop constants with Big-O, so those still just become O(n)."
"""
"""
first_val on its own is O(1). But we recurse n times, so it becomes O(1*n) or just O(n).
Each slice of data on its own is O(n). But we recurse n times, so it becomes O(n*n) or O(n^2).
So the total space complexity for this algorithm is:
O(n) + O(n^2). The O(n^2) dominates, so the final space complexity is just O(n^2).
source: https://github.com/LambdaSchool/cs-module-project-recursive-sorting
"""