(python)插入排序递归算法与非递归算法比较实验

(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)

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值