我的算法基准是正确的吗



我为它们编写了Quicksort和Mergesort以及Benchmark,以了解它们的速度。

这是我的代码:

#------------------------------Creating a Random List-----------------------------#

def create_random_list (length):
import random
random_list = list(range(0,length))
random.shuffle(random_list)
return random_list
# Initialize default list length to 0
random_list = create_random_list(0)
# Testing random list function
print ("n" + "That is a randomized list: " + "n")
print (random_list)
print ("n")

#-------------------------------------Quicksort-----------------------------------#

"""
Recursive Divide and Conquer Algorithm
+ Very efficient for large data set
- Performance Depends largely on Pivot Selection
Time Complexity
--> Worst-Case -----> O (n^2)
--> Best-Case  -----> Ω (n log (n)) 
--> Average Case ---> O (n log (n))
Space Complexity
--> O(log(n))
"""
# Writing the Quick Sort Algorithm for sorting the list - Recursive Method
def qsort (random_list):

less = []
equal = []
greater = []
if len(random_list)>1:
# Initialize starting Point
pivot = random_list[0]
for x in random_list:
if x < pivot:
less.append(x)
elif x == pivot:
equal.append(x)
elif x > pivot:
greater.append(x)
return qsort(less) + equal + qsort(greater)
else:
return random_list
"""
Build in Python Quick Sort:
def qsort(L):
if len(L) <= 1: return L
return qsort([lt for lt in L[1:] if lt < L[0]]) + L[0:1] + 
qsort([ge for ge in L[1:] if ge >= L[0]])
"""
# Calling Quicksort
sorted_list_qsort = qsort(random_list)
# Testint Quicksort
print ("That is a sorted list with Quicksort: " + "n")
print (sorted_list_qsort)
print ("n")

#-------------------------------------FINISHED-------------------------------------#



#-------------------------------------Mergesort------------------------------------#

"""
Recursive Divide and Conquer Algorithm
+ 
- 
Time Complexity
--> Worst-Case -----> O (n l(n))
--> Best-Case  -----> Ω (n l(n))  
--> Average Case ---> O (n l(n))
Space Complexity
--> O (n)
"""
# Create a merge algorithm
def merge(a,b): # Let a and b be two arrays
c = [] # Final sorted output array
a_idx, b_idx = 0,0 # Index or start from a and b array
while a_idx < len(a) and b_idx < len(b):
if a[a_idx] < b[b_idx]:
c.append(a[a_idx])
a_idx+=1
else:
c.append(b[b_idx])
b_idx+=1
if a_idx == len(a): c.extend(b[b_idx:])
else:               c.extend(a[a_idx:])
return c
# Create final Mergesort algorithm
def merge_sort(a):
# A list of zero or one elements is sorted by definition
if len(a)<=1:
return a
# Split the list in half and call Mergesort recursively on each half
left, right = merge_sort(a[:int(len(a)/2)]), merge_sort(a[int(len(a)/2):])
# Merge the now-sorted sublists with the merge function which sorts two lists
return merge(left,right)
# Calling Mergesort
sorted_list_mgsort = merge_sort(random_list)
# Testing Mergesort
print ("That is a sorted list with Mergesort: " + "n")
print (sorted_list_mgsort)
print ("n")

#-------------------------------------FINISHED-------------------------------------#


#------------------------------Algorithm Benchmarking------------------------------#

# Creating an array for iterations
n = [100,1000,10000,100000]
# Creating a dictionary for times of algorithms
times = {"Quicksort":[], "Mergesort": []}
# Import time for analyzing the running time of the algorithms 
from time import time
# Create a for loop which loop through the arrays of length n and analyse their times
for size in range(len(n)):
random_list = create_random_list(n[size])
t0 = time()
qsort(random_list)
t1 = time()
times["Quicksort"].append(t1-t0)
random_list = create_random_list(n[size-1])
t0 = time()
merge_sort(random_list)
t1 = time()
times["Mergesort"].append(t1-t0)
# Create a table while shows the Benchmarking of the algorithms
print ("ntMergetQuick")
print ("_"*25)
for i,j in enumerate(n):
print ("{}t{:.5f}t{:.5f}t".format(j, times["Mergesort"][i], times["Quicksort"][i]))

#----------------------------------End of Benchmarking---------------------------------#

该代码有很好的文档记录,并且可以完美地使用Python 3.8运行。为了更好的可读性,您可以将其复制到代码编辑器中。

-->我的问题如标题所示:

我的基准测试正确吗?我有点怀疑,因为我的算法的运行时间似乎有点奇怪。有人能确认我的运行时间吗?

-->这是此代码的输出:

That is a randomized list: 
[]

That is a sorted list with Quicksort: 
[]

That is a sorted list with Mergesort: 
[]

n       Merge   Quick
_________________________
100     0.98026 0.00021 
1000    0.00042 0.00262 
10000   0.00555 0.03164 
100000  0.07919 0.44718 

-->如果有人有其他/更好的关于如何打印表格的代码片段,请随时与我分享。

错误在n[size-1]中:当size为0(第一次迭代(时,转换为n[-1],这对应于您的最大大小。因此,在第一次迭代中,您将比较qsort(100)merge_sort(100000),这显然会大大有利于第一个。调用这个变量size没有帮助,因为它实际上不是大小,而是包含大小的n列表中的索引。

因此,删除-1,或者更好:直接在n上迭代。我还要确保两种排序算法都能对同一列表进行排序:

for size in n:
random_list1 = create_random_list(size)
random_list2 = random_list1[:]
t0 = time()
qsort(random_list1)
t1 = time()
times["Quicksort"].append(t1-t0)
t0 = time()
merge_sort(random_list2)
t1 = time()
times["Mergesort"].append(t1-t0)

最后,考虑使用timeit,它是为测量性能而设计的。

相关内容

  • 没有找到相关文章

最新更新