-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path(Algorithm)SmallSum.py
More file actions
67 lines (56 loc) · 1.21 KB
/
Copy path(Algorithm)SmallSum.py
File metadata and controls
67 lines (56 loc) · 1.21 KB
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
import random
import copy
def GenerateRandomList(number, size):
temp = []
random_legth = random.randint(0, size)
current_length = 0
while current_length < random_legth:
temp.append(random.randint(1, number))
current_length += 1
return temp
def SmallSum(arr):
if arr == None or len(arr) < 2:
return 0
return sortProcess(arr, 0, len(arr) - 1)
def sortProcess(arr, l, r):
if l == r:
return 0
mid = l + (r - l) // 2
return sortProcess(arr, l, mid) + sortProcess(arr, mid + 1, r) + merge(arr, l, mid, r)
def merge(arr, l, mid, r):
temp = []
p1 = l
p2 = mid + 1
res = 0
while p1 <= mid and p2 <= r:
if arr[p1] <= arr[p2]:
temp.append(arr[p1])
res += (r - p2 + 1) * arr[p1]
p1 += 1
else:
temp.append(arr[p2])
p2 += 1
while p1 <= mid:
temp.append(arr[p1])
p1 += 1
while p2 <= r:
temp.append(arr[p2])
p2 += 1
for i in range(len(temp)):
arr[l+i] = temp[i]
return res
def standard_fun(arr):
ans = 0
for i in range(1, len(arr)):
for j in range(0, i):
if arr[j] <= arr[i]:
ans += arr[j]
return ans
for i in range(100):
l = GenerateRandomList(100,100)
temp = copy.copy(l)
standard_l = standard_fun(l)
res = SmallSum(l)
if res != standard_l:
print(temp)
print('Done!')