最近跟左神学算法然后才听说有对数器这么个东西,特地实现了个Python版跟大家一起分享
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
|
import random
# 交换两个值
def swap(l, arg1, arg2):
l[arg1], l[arg2] = l[arg2], l[arg1]
return arg1, arg2
def comparator(l):
l = sorted(l)
return l
def generate_random_list(max_size, max_value):
my_list = []
random_size = random.randint(1, max_size)
for i in range(random_size):
my_list.append(random.randint(-max_value, max_value))
return my_list
def copy_list(l):
return l[:]
def print_list(l):
for i in range(len(l)):
print(l[i], end=' ')
def main():
max_size = 20
max_value = 100
my_list = generate_random_list(max_size, max_value)
print_list(my_list)
ordered_list = comparator(my_list)
print('\n')
print_list(ordered_list)
if __name__ == '__main__':
main()
|