-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseOrder
67 lines (56 loc) · 1.45 KB
/
ReverseOrder
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
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 ReverseOrder(arr):
if arr is 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) // 2
return SortProcess(arr, L, mid) + SortProcess(arr, mid + 1, R) + Merge(arr, L, mid, R)
def Merge(arr, L, mid, R):
help_list = []
p1 = L
p2 = mid + 1
res = 0
while p1 <= mid and p2 <= R:
if arr[p1] > arr[p2]:
res += 1 * (mid - p1 + 1)
help_list.append(arr[p2])
p2 += 1
else:
help_list.append(arr[p1])
p1 += 1
while p1 <= mid:
help_list.append(arr[p1])
p1 += 1
while p2 <= R:
help_list.append(arr[p2])
p2 += 1
for i in range(len(help_list)):
arr[L + i] = help_list[i]
return res
def standard_ReverseOrder(arr):
ans = 0
for i in range(len(arr) - 1):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
ans += 1
return ans
for i in range(100):
l = GenerateRandomList(100,100)
temp = copy.copy(l)
standard_l = standard_ReverseOrder(l) % 1000000007
res = ReverseOrder(l) % 1000000007
if res != standard_l:
print(temp)
print('Done!')