Sorting Algorithms
Merge sort by using recursive strategy, i.e. divide and conquer.
def merge(left,right):
result = []
i,j =,
while i < len(left) and j < len(right):
if left[i]<right[j]:
result.append(left[i])
i +=
else:
result.append(right[j])
j +=
result += left[i:]
result += right[j:]
return result def merge_Sort(seq):
if len(seq)<=:
return seq
mid = len(seq)/
left = merge_Sort(seq[:mid])
right = merge_Sort(seq[mid:])
return merge(left,right) seq = [, , , , , , , , , , , , , , , , , , , , ]
merge_Sort(seq)
def ins_sort_rec(seq,i):
if i==: return
ins_sort_rec(seq,i-)
j = i
while j > and seq[j - ] > seq[j]:
seq[j -],seq[j] = seq[j], seq[j-]
j -=
return seq seqq = [,,,,,,,,,,,,,] a = ins_sort_rec(seqq,len(seqq)-)
a def insertsort(seq):
for i in range(,len(seq)):
j = i
while seq[j] <seq[j-] and j>:
seq[j],seq[j-] = seq[j-],seq[j]
j -=
return seq b = insertsort(seqq)
b def changesort(seq):
count = True
while count:
count = False
for i in range(,len(seq)):
if seq[i]<seq[i-]:
seq[i],seq[i-] = seq[i-],seq[i]
count =True
return seq ls = [,,,,,,,,,,,,,]
c = changesort(ls)
c seqq = [,,,,,,,,,,,,,] def selectsort(ls):
for i in range(len(ls)-):
k = i
for j in range(i,len(ls)):
if ls[k] >ls[j]:
k = j
if k !=i:
ls[i],ls[k]=ls[k],ls[i]
return ls
selectsort(seqq)
Sorting Algorithms的更多相关文章
- JavaScript 排序算法(JavaScript sorting algorithms)
JavaScrip 排序算法(JavaScript Sorting Algorithms) 基础构造函数 以下几种排序算法做为方法放在构造函数里. function ArrayList () { va ...
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- Summary: sorting Algorithms
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item a ...
- Basic Sorting Algorithms
*稳定指原本数列中相同的元素的相对前后位置在排序后不会被打乱 快速排序(n*lgn 不稳定):数组中随机选取一个数x(这里选择最后一个),将数组按比x大的和x小的分成两部分,再对剩余两部分重复这个算法 ...
- 算法的稳定性(Stability of Sorting Algorithms)
如果具有同样关键字的纪录的在排序前和排序后相对位置保持不变.一些算法本身就是稳定的,如插入排序,归并排序,冒泡排序等,不稳定的算法有堆排序,快速排序等. 然而,一个本身不稳定的算法通过一点修正也能变成 ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- [zt]Which are the 10 algorithms every computer science student must implement at least once in life?
More important than algorithms(just problems #$!%), the techniques/concepts residing at the base of ...
- Top 10 Algorithms for Coding Interview--reference
By X Wang Update History:Web Version latest update: 4/6/2014PDF Version latest update: 1/16/2014 The ...
- 转:Top 10 Algorithms for Coding Interview
The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...
随机推荐
- js仿ppt,在线演示文档:Impress.js
(附件) Impress.js是一款基于css3转 换和过渡.工作于现代浏览器(Google Chrome或Safari (或 Firefox 10 或 IE10)).并受prezi.com的理念启发 ...
- linux golang
wget -c http://www.golangtc.com/static/go/go1.3.linux-386.tar.gz #下载32位Linux的够源码包 tar -zxvf go1.1.li ...
- xdg-open命令智能打开各种格式的文件
在linux中,通常用命令行打开文本文件,比如用命令gedit.more.cat.vim.less.但当需要打开其他格式文件时,比如pdf.jpg.mp3格式文件,咱们通常做法是进入到文件所在的目录, ...
- python2.7使用requests时报错SSLError: HTTPSConnectionPool(host='b-ssl.duitang.com', port=443)
import requests url='https://www.duitang.com/napi/blog/list/by_search/?kw=%E6%A0%A1%E8%8A%B1&sta ...
- CentOS 7 安装pip2
使用yum安装python-pip,但是报错,说没有可用的包 安装epel源 [root@sishen yum.repos.d]# yum install -y epel-release 然后再安装 ...
- Qt532界面.ZC测试
ZC:Delphi中只要随便拖几个控件,设置一下属性就OK了.但是,Qt中 貌似没有 方便的方式来做这个... ZC:目前的解决方案是:Qt中 拖几个控件,然后点 工具条里面的 "栅格布局( ...
- 将矩阵数据转换为栅格图 filled.contour()
require(grDevices) # for colours filled.contour(volcano, color = terrain.colors, asp = 1) # simple x ...
- 基于iOS用CoreImage实现人脸识别
2018-09-04更新: 很久没有更新文章了,工作之余花时间看了之前写的这篇文章并运行了之前写的配套Demo,通过打印人脸特征CIFaceFeature的属性,发现识别的效果并不是很好,具体说明见文 ...
- nRF52832无法加载协议栈文件
使用keil向nRF52832下载程序时报错 Error:Flash Download failed-Could not load file"..\..\..\..\compoents\so ...
- 第 3 章 镜像 - 015 - 调试 Dockerfile
如何 debug Dockerfile 通过 Dockerfile 构建镜像的过程 从 base 镜像运行一个容器 执行命令对容器做修改 执行类似 docker commit 的操作,生成一个新的镜像 ...