-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.py
45 lines (36 loc) · 1.01 KB
/
tools.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : tools.py
# @Author: 投笔从容
# @Date : 2018/5/24
# @Desc : 排序测试辅助工具
import random
import numpy as np
import datetime
def genNearlyOrderArray(n, swapTimes):
arr = list(range(n))
for i in range(swapTimes):
x = random.randint(0, n-1)
y = random.randint(0, n-1)
swap(arr, x, y)
return arr
def genRandomArray(n, start=0, end=10000):
return np.random.randint(start, end, size=n)
def aTestSort(sortName, arr, n):
t_start = datetime.datetime.now()
sortName(arr, n)
t_end = datetime.datetime.now() # 记录函数结束时间)
long = (t_end - t_start).total_seconds()
if isSorted(arr, n):
print("sortName: %s, time: %f s" % (sortName.__name__, long))
else:
print('Sort ERROR!')
def swap(arr, i, j):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
def isSorted(arr, n):
for i in range(n - 1):
if (arr[i] > arr[i + 1]):
return False
return True