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 ...
随机推荐
- Docker6之Network containers
how to network your containers. Launch a container on the default network Docker includes support fo ...
- 用flvplayer.swf在网页中播放视频(网页中flash视频播放的实现)
原:http://blog.csdn.net/ricciozhang/article/details/46868201 由于公司项目的需求,需要在展示一些信息的时候能够播放视频,拿到这个要求,我就从最 ...
- 使用Coding体验小记
https://coding.net/ 写了些小程序后放在github上托管,并用gitpages展示. 今天发现用git pages展示的网页网络特别不稳定,时常会出现网页打不开的现象. 之前一直通 ...
- 小程序模板template
WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用. 定义模板 使用 name 属性,作为模板的名字.然后在<template/>内定义代码片段,如: & ...
- python3使用pymysql模块,连接mysql数据库,实现新增、查询和更新操作
1.环境数据准备: python3环境.pymysql模块 mysql数据库:本次代码中用到的数据库为本地的testdb数据库,user表(表字段比较简单,只有主键id,手机号mobile,密码pas ...
- Java——HashMap
获取数组长度 数组.length 获取下标 HashMap HashMap 构造函数 // 默认构造函数. HashMap() // 指定“容量大小”的构造函数 HashMap(int capacit ...
- leecode第七题(整数反转)
题解给的思路: ; class Solution { public: int reverse(int x) { ;//如果这里还是int,会在判断前就被裁剪了,无法判断溢出 ; ) flag=; wh ...
- x1c2017 8G版 win linux的取舍纠结记录
x1c 2017 的 i5 7200U 8G ram 的丐版.换了1T SSD.其实一般使用没啥问题. 1 外出携带的轻便性太满意(mac13寸相比之下都太重了): 2 coding时候的安静性,比原 ...
- ubuntu配置chrome git
安装chrome (也可以直接从官网下载deb包安装) sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/ ...
- springboot 解决配置js/css/img缓存问题
# 解决配置js/css/img缓存问题 spring.resources.chain.strategy.content.enabled=true spring.resources.chain.str ...