1. 选择排序

--

--

def selectSort(l):
for i in range(len(l)):
j = i + 1
t_min = l[i]
loc_min = i
for j in range(j,len(l)):
if l[j] < t_min:
t_min = l[j]
loc_min = j
if loc_min != i:
t = l[i]
l[i] = t_min
l[loc_min] = t

2. 冒泡排序

--

--

def bubbleSort(l):
for i in range(len(l)-1,1,-1):
for j in range(i):
if l[j] > l[j+1]:
t = l[j+1]
l[j+1] = l[j]
l[j] = t

3. 插入排序

--

--

def insertSort(param):
p_len = len(param)
for i in range(1,p_len):
key = param[i]
#for j in range(1,i+1)[::-1]:
for j in range(i,0,-1):
#if j>0 and key < param[j-1]:
if key < param[j-1]:
param[j] = param[j-1]
param[j-1] = key

4. 桶排序

--

--

def bucketSort(l):
min_,max_ = min_max(l)
bucket_len = max_ - min_ + 1
bucketList = [0] * bucket_len
rList = []
for e in l:
bucketList[e-min_] += 1 for i in range(bucket_len):
if bucketList[i] != 0:
for j in range(bucketList[i]):
rList.append(i + min_)
return rList def min_max(l):
min_ = max_ = l[0] for e in l:
if min_ > e:
min_ = e
if max_ < e:
max_ = e
return min_,max_

【python,排序】几种常用的排序算法,使用python实现的更多相关文章

  1. C语言几种常用的排序算法

    /* ============================================================================= 相关知识介绍(所有定义只为帮助读者理解 ...

  2. Python之几种常用模块

    模块 注意事项: 所有的模块导入都应该尽量往上写 内置模块 扩展模块 自定义模块 模块不会重复被导入 : sys.moudles 从哪儿导入模块 : sys.path import import 模块 ...

  3. 二、python 中五种常用的数据类型

    一.字符串 单引号定义: str1 = 'hello' 双引号定义: str1 = "hello" 三引号定义:""" 人生苦短, 我用python! ...

  4. 用python介绍4种常用的单链表翻转的方法

    这里给出了4种4种常用的单链表翻转的方法,分别是: 开辟辅助数组,新建表头反转,就地反转,递归反转 # -*- coding: utf-8 -*- ''' 链表逆序 ''' class ListNod ...

  5. Python实现几种简单的排序算法

    一.冒泡排序 概念:为一个无序的列表排成有序的 实现过程描述:(升序) 1.比较相邻的元素,如果第一个比第二个大,就交换他们的位置 2.对每一对相邻元素重复1的工作,从开始第一队到最后一对,最后结束的 ...

  6. python中几种常用的数据类型

    1.字典 字典的创建: dict1=dict((('name','PIG'),)),其中第一层()代表里面的内容是dict函数的输入参数.第二层和第三层代表字典中的各元素,也就是key和value组合 ...

  7. 三种方法实现PCA算法(Python)

    主成分分析,即Principal Component Analysis(PCA),是多元统计中的重要内容,也广泛应用于机器学习和其它领域.它的主要作用是对高维数据进行降维.PCA把原先的n个特征用数目 ...

  8. 两种常用的全排列算法(java)

    问题:给出一个字符串,输出所有可能的排列. 全排列有多种算法,此处仅介绍常用的两种:字典序法和递归法. 1.字典序法: 如何计算字符串的下一个排列了?来考虑"926520"这个字符 ...

  9. Python使用三种方法实现PCA算法[转]

    主成分分析(PCA) vs 多元判别式分析(MDA) PCA和MDA都是线性变换的方法,二者关系密切.在PCA中,我们寻找数据集中最大化方差的成分,在MDA中,我们对类间最大散布的方向更感兴趣. 一句 ...

随机推荐

  1. Labeled ContentControl & LabeledControl【项目】

    LabeledTextBoxControl: C#定义 using System; using System.Collections.Generic; using System.Linq; using ...

  2. [转] [Visual Studio 2012] 找回 建立單元測試 選單

    原文链接:http://www.dotblogs.com.tw/yc421206/archive/2013/03/08/95920.aspx Step1.建立選單 在VS2012選單,Tools→Cu ...

  3. Merge into 使用

    在进行SQL语句编写时,我们经常会遇到这样的问题:当存在记录时,就更新(Update),不存在数据时,就插入(Insert),oracle为我们提供了一种解决方法——Merge into ,具体语法如 ...

  4. javascript数据缓存

    if(!self.hotCityPrice[city]) { $.ajax({ type: 'GET', url: self.hotCityUrl, data: {cityCode: city, t: ...

  5. java懒汉式单例遇到多线程

    单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 在计算机系统中,线程池.缓存.日志对象.对话框.打印机.显卡的驱动程序对象常被设计成单例.这些应用都或多或少具有资源管理器的功 ...

  6. 理解MapReduce哲学

    Google工程师将MapReduce定义为一般的数据处理流程.一直以来不能完全理解MapReduce的真义,为什么MapReduce可以“一般”? 最近在研究Spark,抛开Spark核心的内存计算 ...

  7. 编写你自己的单点登录(SSO)服务

    王昱 yuwang881@gmail.com   博客地址http://yuwang881.blog.sohu.com 摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统其中.本文从 ...

  8. Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造

    E. Brackets in Implications Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  9. Topcomponent --sighoff

    Topcomponent --sighoff 实现一个置于右边框的Topcomponent: 1.可远程同步更新(根据远程的xml文件),修改(增删)该xml文件,查看.刷新等 2.打包工程,记录该打 ...

  10. [ES6] 13. Using the ES6 spread operator ...

    The spread operator (...) allows you to "explode" an array into its individual elements. S ...