import random
import time
def insertion_Sort(A):
if len(A) == 1 :
return A
else :
S = [A[0]]
for i in range(1,len(A)):
j = i-1
while j >= 0:
if A[i] > S[j]:
S.insert((j+1),A[i])
break
else :
j = j-1
if j==-1:
S.insert(0,A[i])
return S
def quick_Sort(A):
if not A:
return []
else:
pivot = random.randint(0, len(A) - 1)
pivot_index = A[pivot]
L = quick_Sort([l for i,l in enumerate(A)
if l <= pivot_index and i != pivot])
R = quick_Sort([r for r in A if r > pivot_index])
return L + [pivot_index] + R
RN = [random.randrange(0,10000) for k in range(100)]
This is the code about quick_sort and insertion_sort.
I want to compare two things, insertion_sort(RN)'s count of performing and quick_sort(RN)'s count of performing.
How can I compare these things?
There is a python module called timeit
which is exactly what you are looking for. You can use it as follows:
from timeit import timeit
print(timeit('insertion_Sort(params)','from __main__ import insertion_Sort',number=100))
print(timeit('quick_Sort(params)','from __main__ import quick_Sort',number=100))
And you replace params
with the value of your parameter A
and number=100
with the number of times you want it to be tested.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.