(python)插入排序递归算法与非递归算法比较实验
实验题目
插入排序递归算法与非递归算法比较实验
实验要求
画出运行时间与n变化曲线对比图,并分析原因
实验目的
1、 掌握递归算法的思想
2、 编写实现插入排序的递归与非递归算法
3、 比较插入排序的递归与非递归实现不同n值所耗费的时间
实验步骤
1、递归实现插入排序算法
#递归实现插入排序
def rec_Insert_sort(seq, i):
if i == 0:
return
rec_Insert_sort(seq, i-1)
j = i
while j>0 and seq[j-1]>seq[j]:
seq[j-1], seq[j] = seq[j], seq[j-1]
j -= 1
2、非递归实现插入排序算法
#非递归实现插入排序
def Insert_sort(seq,i):
for i in range(1, i):
j = i
while j>0 and seq[j-1]>seq[j]:
seq[j-1], seq[j] = seq[j], seq[j-1]
j -= 1
3、统计函数运行时间
#函数运算时间
def time_Counter(func, seq, i):
time_start = time.time()
func(seq,i)
time_end = time.time()
return time_end-time_start
4、可视化显示实验结果
def ShowTimes(N,time1, time2):
#设置汉字格式
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
plt.figure("Lab2")
plt.title(u'不同n值递归与非递归插入排序算法所耗费的时间',FontProperties=font)
plt.xlabel(u'n值',FontProperties=font)
plt.ylabel(u'所需时间(s)',FontProperties=font)
plt.scatter(x=N, y=time1, color='black',s=10)
plt.scatter(x=N, y=time2, color='red',s=10)
plt.plot(N,time1,color='black')
plt.plot(N,time2,color='red')
rec_insert_sort = mlines.Line2D([], [], color='black', marker='.',
markersize=10, label='rec_insert_sort')
insert_sort = mlines.Line2D([], [], color='red', marker='.',
markersize=10, label='insert_sort')
plt.legend(handles=[rec_insert_sort,insert_sort])
plt.show()
5、主函数设计
if __name__ == '__main__':
N = []
time1 = []
time2 = []
for i in range(10,4000,100):
list = []
for j in range(0,i+1):
list.append(j)
#随机打乱列表
random.shuffle(list)
#复制列表list,保证两个排序函数进行排序的是同样的列表
list_copy = list[:]
#递归实现插入排序所耗费的时间
time1.append(time_Counter(rec_Insert_sort,list,i))
#非递归插入排序所耗费的时间
time2.append(time_Counter(Insert_sort, list_copy, i))
N.append(i)
ShowTimes(N,time1,time2)
#测试两个排序算法是否排序正确
test = []
for j in range(0,20):
test.append(j)
random.shuffle(test)
test_copy = test[:]
print('原数据为:',test)
rec_Insert_sort(test,19)
print('递归插入排序后:',test)
Insert_sort(test_copy,20)
print('非递归插入排序后:',test_copy)
print('递归插入排序所耗时间:',time1)
print('非递归插入排序所耗时间:',time2)
6、输出显示结果
全部实验代码
import time
import random
import sys
import numpy as np
sys.setrecursionlimit(1000000)
from matplotlib.font_manager import FontProperties
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
#递归实现插入排序
def rec_Insert_sort(seq, i):
if i == 0:
return
rec_Insert_sort(seq, i-1)
j = i
while j>0 and seq[j-1]>seq[j]:
seq[j-1], seq[j] = seq[j], seq[j-1]
j -= 1
#非递归实现插入排序
def Insert_sort(seq,i):
for i in range(1, i):
j = i
while j>0 and seq[j-1]>seq[j]:
seq[j-1], seq[j] = seq[j], seq[j-1]
j -= 1
#函数运算时间
def time_Counter(func, seq, i):
time_start = time.time()
func(seq,i)
time_end = time.time()
return time_end-time_start
def ShowTimes(N,time1, time2):
#设置汉字格式
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
plt.figure("Lab2")
plt.title(u'不同n值递归与非递归插入排序算法所耗费的时间',FontProperties=font)
plt.xlabel(u'n值',FontProperties=font)
plt.ylabel(u'所需时间(s)',FontProperties=font)
plt.scatter(x=N, y=time1, color='black',s=10)
plt.scatter(x=N, y=time2, color='red',s=10)
plt.plot(N,time1,color='black')
plt.plot(N,time2,color='red')
rec_insert_sort = mlines.Line2D([], [], color='black', marker='.',
markersize=10, label='rec_insert_sort')
insert_sort = mlines.Line2D([], [], color='red', marker='.',
markersize=10, label='insert_sort')
plt.legend(handles=[rec_insert_sort,insert_sort])
plt.show()
if __name__ == '__main__':
N = []
time1 = []
time2 = []
for i in range(10,4000,100):
list = []
for j in range(0,i+1):
list.append(j)
#随机打乱列表
random.shuffle(list)
#复制列表list,保证两个排序函数进行排序的是同样的列表
list_copy = list[:]
#递归实现插入排序所耗费的时间
time1.append(time_Counter(rec_Insert_sort,list,i))
#非递归插入排序所耗费的时间
time2.append(time_Counter(Insert_sort, list_copy, i))
N.append(i)
ShowTimes(N,time1,time2)
#测试两个排序算法是否排序正确
test = []
for j in range(0,20):
test.append(j)
random.shuffle(test)
test_copy = test[:]
print('原数据为:',test)
rec_Insert_sort(test,19)
print('递归插入排序后:',test)
Insert_sort(test_copy,20)
print('非递归插入排序后:',test_copy)
print('递归插入排序所耗时间:',time1)
print('非递归插入排序所耗时间:',time2)