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 ...
随机推荐
- CodeChef - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...
- 键盘控制div移动并且解决停顿问题(原生js)
<html> <head> <title>键盘控制div移动,解决停顿问题</title> <meta charset="utf-8&q ...
- 【Java】【控制流程】
#栗子 丢手帕 & 菱形 & 金字塔import java.io.*;import java.util.*; public class Test_one { public static ...
- 蚂蚁金服×西安银行 | 西安银行手机银行App的智能升级之路
小蚂蚁说: 当前,数字化信号已经逐渐深入到社会的每个角落,影响着用户的心智和行为,来到数字化时代门口的银行,需要注意到数字化信号.西安银行通过引入蚂蚁金服移动开发平台mPaaS,对手机银行进行架构升级 ...
- vim的简单使用
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- Spring 的@@Autowired 和 @Qualifier注释
@Autowired spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造方法进行标注,配合AutowiredAnnotationBeanProc ...
- leecode第五十四题(螺旋矩阵)
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) ...
- Cordova 混合开发
详细的教程在以下博客 https://blog.csdn.net/csdn100861/article/details/78585333
- Git安装与使用
转载自:https://www.cnblogs.com/smuxiaolei/p/7484678.html git 提交 全部文件 git add . git add xx命令可以将xx文件添加到暂 ...
- Codeforces 932D - Tree
932D - Tree 思路: 树上倍增 anc[i][u]:u的2^i祖先 mx[i][u]:u到它的2^i祖先之间的最大值,不包括u pre[i][u]:以u开始的递增序列的2^i祖先 sum[i ...