本来在博客上看到用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操作。

python中的归并排序的更多相关文章

  1. python 数据结构之归并排序

    def merger_sort(alist): if len(alist) <= 1 : return alist num=int(len(alist)/2) left=merger_sort( ...

  2. Python中sort与sorted函数

    python中列表的内置函数sort()可以对列表中的元素进行排序,而全局性的sorted()函数则对所有可迭代的序列都是适用的: 并且sort()函数是内置函数,会改变当前对象,而sorted()函 ...

  3. Python中的序列

    Python中有四种内建的数据结构,即列表.元组.字典.集合.其中字典和集合我会以后再写,现在先说列表和元组,它们两个和以前提到很多次的字符串, 其实都属于——序列. 一.列表(list): 1. l ...

  4. [转]Python中的str与unicode处理方法

    早上被python的编码搞得抓耳挠腮,在搜资料的时候感觉这篇博文很不错,所以收藏在此. python2.x中处理中文,是一件头疼的事情.网上写这方面的文章,测次不齐,而且都会有点错误,所以在这里打算自 ...

  5. python中的Ellipsis

    ...在python中居然是个常量 print(...) # Ellipsis 看别人怎么装逼 https://www.keakon.net/2014/12/05/Python%E8%A3%85%E9 ...

  6. python中的默认参数

    https://eastlakeside.gitbooks.io/interpy-zh/content/Mutation/ 看下面的代码 def add_to(num, target=[]): tar ...

  7. Python中的类、对象、继承

    类 Python中,类的命名使用帕斯卡命名方式,即首字母大写. Python中定义类的方式如下: class 类名([父类名[,父类名[,...]]]): pass 省略父类名表示该类直接继承自obj ...

  8. python中的TypeError错误解决办法

    新手在学习python时候,会遇到很多的坑,下面来具体说说其中一个. 在使用python编写面向对象的程序时,新手可能遇到TypeError: this constructor takes no ar ...

  9. python中的迭代、生成器等等

    本人对编程语言实在是一窍不通啊...今天看了廖雪峰老师的关于迭代,迭代器,生成器,递归等等,word天,这都什么跟什么啊... 1.关于迭代 如果给定一个list或tuple,我们可以通过for循环来 ...

随机推荐

  1. MemSQL Start[c]UP 2.0 - Round 1 E - Three strings 广义后缀自动机

    E - Three strings 将三个串加进去,看每个节点在三个串中分别出现了多少次. #include<bits/stdc++.h> #define LL long long #de ...

  2. bzoj 1899 贪心+dp

    思路:这个贪心排顺序我居然没看出来. 吃饭时间长的在前面, 用反证法很容易得出. 剩下的就是瞎dp啦. #include<bits/stdc++.h> #define LL long lo ...

  3. Salt 与Salt API配置

    1.安装 [root@localhost yum.repos.d]# cat /etc/yum.repos.d/salt.repo [saltstack-repo] name=SaltStack re ...

  4. ubuntu 安装 eslint

    1. 安装 npm install -g eslint 安装结束后记住 /usr/local/bin/eslint -> /usr/local/lib/node_modules/eslint/b ...

  5. Android Studio奇技淫巧

    No1: Ctrl+E:显示最近浏览过的文件 Ctrl+Shift+E:显示最近编辑过的文件 Ctrl+Tab:切换最近编辑过的文件 No2: 条件断点:断点右键 No3: 全局断点 这样只要碰到空指 ...

  6. DNS 设置

    用上 ip 靓号1.1.1.1,Cloudflare 花了多少钱? ipv4: 1.1.1.1 和 1.0.0.1 是目前最快的 DNS. 通过ipv6***之二--使用IPv6 DNS服务器

  7. 海康威视 - 萤石云开放平台 js 版

    开放平台 https://open.ys7.com/mobile/download.html API http://open.ys7.com/doc/zh/uikit/uikit_javascript ...

  8. centos 安装php7、pecl 、swoole、redis拓展

    一.更新yum源,安装php7 CentOS/RHEL 7.x: rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest- ...

  9. linux下使用cronjob定时执行php脚本

    在linux中输入命令 crontab -e 然后使用vim的命令编辑打开的文件,输入 * * * * /usr/bin/php -f /home/userxxx/update.php 保存,退出,好 ...

  10. Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力

    A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...