-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.py
executable file
·96 lines (96 loc) · 2.39 KB
/
sort.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#-*- coding: utf-8 -*-
#!/usr/bin/python
#Filename:sort.py
#Author: Boyce
#Email: boyce.ywr@gmail.com
import time
import randata
import bubble_sort
import quick_sort
import heap_sort
import shell_sort
import merge_sort
import insert_sort
fileName='sort.dat'
size=10000
print '/nStart generate randam data...'
arr=randata.getrandata(size)
print 'Data generation finished.'
print 'Data size is %d, result will be save to file %s'%(size,fileName)
f=file(fileName,'w')
f.write("/nOriginal data:/n")
f.write(str(arr))
#使用python内置的timSort排序算法
a=arr[:]
print '/nStart internal sort...'
t1=time.clock()
a.sort()
t2=time.clock()
print 'Internal sort finisehd. Time used=%fs'%(t2-t1)
f.write('/n/nInternal sort [Time used=%fs]/n'%(t2-t1))
f.write(str(a))
del a
a=arr[:]
print '/nStart quick sort...'
t1=time.clock()
quick_sort.quickSort(a)
t2=time.clock()
print 'Quick sort finished. Time used=%fs'%(t2-t1)
f.write('/n/nQuick sort [Time used=%fs]/n'%(t2-t1))
f.write(str(a))
del a
a=arr[:]
print '/nStart heap sort...'
t1=time.clock()
heap_sort.heapSort(a)
t2=time.clock()
print 'Heap sort finished. Time used=%fs'%(t2-t1)
f.write('/n/nHeap sort [Time used=%fs]/n'%(t2-t1))
f.write(str(a))
del a
a=arr[:]
print '/nStart shell sort...'
t1=time.clock()
shell_sort.shellSort(a)
t2=time.clock()
print 'Shell sort finished. Time used=%fs'%(t2-t1)
f.write('/n/nShell sort [Time used=%fs]/n'%(t2-t1))
f.write(str(a))
del a
a=arr[:]
print '/nStart merge sort with new space...'
t1=time.clock()
merge_sort.mergeSortWithNewSpace(a)
t2=time.clock()
print 'Merge sort with new space finished. Time used=%fs'%(t2-t1)
f.write('/n/nMerge sort with new space [Time used=%fs]/n'%(t2-t1))
f.write(str(a))
del a
a=arr[:]
print '/nStart merge sort without new space...'
t1=time.clock()
merge_sort.mergeSortWithoutNewSpace(a)
t2=time.clock()
print 'Merge sort without new space finished. Time used=%fs'%(t2-t1)
f.write('/n/nMerge sort without new space [Time used=%fs]/n'%(t2-t1))
f.write(str(a))
del a
a=arr[:]
print '/nStart insert sort...'
t1=time.clock()
insert_sort.insertSort(a)
t2=time.clock()
print 'Insert sort finished. Time used=%fs'%(t2-t1)
f.write('/n/nInsert sort [Time used=%fs]/n'%(t2-t1))
f.write(str(a))
del a
a=arr[:]
print '/nStart bubble sort...'
t1=time.clock()
bubble_sort.bubbleSort(a)
t2=time.clock()
print 'Bubble sort finished. Time used=%fs'%(t2-t1)
f.write('/n/nBubble sort [Time used=%fs]/n'%(t2-t1))
f.write(str(a))
del a
f.close()