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 ...
随机推荐
- 转载:mysql存储过程讲解
记录MYSQL存储过程中的关键语法: DELIMITER // 声明语句结束符,用于区分; CEATE PROCEDURE demo_in_parameter(IN p_in int) 声明存储过程 ...
- 在mybatis中resultMap与resultType的区别
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMapresultType是直接表示返回类型的,而resultMap则是对外部ResultMa ...
- URAL 1004 Sightseeing Trip(floyd求最小环+路径输出)
https://vjudge.net/problem/URAL-1004 题意:求路径最小的环(至少三个点),并且输出路径. 思路: 一开始INF开大了...无限wa,原来相加时会爆int... 路径 ...
- Windows下使用命令安装Python的scipy库出错的解决
平时使用Python都是在Sublime下使用,不想使用IDE.使用各种库时安装也就是使用pip安装即可.来说说今天自己遇到的一个问题:使用scipy数学库时,使用命令: pip install sc ...
- Python open 对应的参数
- table固定列的宽度,超出部分用…代替(针对普通table和antd)
一. 实现思路 我们都知道让溢出内容变成...,只需要以下: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 表格里的内容 ...
- JaveWeb 公司项目(1)----- 使Div覆盖另一个Div完成切换效果
最近在做网页,用的是CSS+DIV的布局方法,搭建了一个简易的界面,大体上分为三个部分,如图所示: 左侧的为主功能导航栏,右侧是具体的功能实现,下方是固定的版权声明,单击左边不同的导航按钮,在div中 ...
- Java离线人脸识别SDK 支持arcface 2.0 最新版
虹软人脸识别SDK之Java版,支持SDK 1.1+,以及当前最新版本2.0,滴滴,抓紧上车! JDK SDK Win release license status 前言 由于业务需求,最近跟人脸识别 ...
- 滑动拼图 Sliding Puzzle
2018-09-09 22:01:02 问题描述: 问题求解: 问题很Interesting,其实本质就是解空间遍历,使用BFS就可以很快的予以解决~ public int slidingPuzzle ...
- 如何调节tomcat初始内存
1.linux下调节tomcat初始内存大小 linux下tomcat的运行文件为catalina.sh,打开文件,在文件靠近顶部找到“ JAVA_OPTS ”字样,在它后面添加如下内容即可 # JA ...