zoukankan      html  css  js  c++  java
  • python中的归并排序

           本来在博客上看到用python写的归并排序的程序,然后自己跟着他写了一下,结果发现是错的,不得不自己操作。而自己对python不是非常了解所以就变百度边写,最终在花了半个小时之后就写好了。

    def merge(a, first, end, temp):
    	if first < end:
    		mid = (first+end)//2
    		merge(a, first, mid, temp) #前半部分拍好序
    		merge(a, mid+1, end, temp) #后半部分拍好序
    		merger(a, first, mid, end, temp) #每次对前面拍好序的两个数组进行合并
    	else:
    		return
    
    def merger(a, first, mid, end, temp):
    	i = first
    	j = mid + 1
    	k = 0
    	while (i <= mid and j < end):
    		if (a[i] < a[j]):
    			temp.append(a[i])
    			i = i + 1
    		else:
    			temp.append(a[j])
    			j = j + 1
    	while (i <= mid):
    		temp.append(a[i])
    		i = i + 1
    	while (j < end):
    		temp.append(a[j])
    		j = j + 1
    
    	for i in range(len(temp)):
    		a[first+i] = temp[i]
    	temp.clear() #这里记得要清零
    
    a = [1,5,38,78, 4, 56, 21]
    print(len(a))
    print(a[3])
    temp = []
    merge(a, 0, len(a), temp)
    print(a)

         得到的结果例如以下:

          

    这次学习中知道了例如以下:

    1、python中没有++操作。

    2、python中的数组事实上就是list。

    3、python中的除法/是浮点数,//是整数。

    4、python中的递归调用限制了多少次。

    5、python对数组的动态加入使用的是list中的append操作。

  • 相关阅读:
    js 中的 EventLoop
    线程的并发工具类
    xpath获取某个节点下的全部字节点的文本
    2020中国 .NET开发者大会精彩回顾:葡萄城高性能表格技术解读
    .NET 控件集 ComponentOne V2020.0 Update3 发布,正式支持 .NET 5
    log4net配置
    TP5.1 爬虫
    pip下载慢
    TP5.1 二维码生成
    composer插件集合
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6733637.html
Copyright © 2011-2022 走看看