class Sort(object):

     @staticmethod
def bubble_sort(ls):
lenth = len(ls)
if lenth == 0:
return []
while lenth:
for i in range(lenth-1):
if ls[i] > ls[i+1]:
ls[i], ls[i+1] = ls[i+1], ls[i]
lenth -= 1
return ls @staticmethod
def select_sort(ls):
if not ls:
return []
lenth = len(ls)
i = 0
while i < lenth-1:
min_v = ls[i]
for n in range(i+1, lenth):
if ls[n] < min_v:
loc, min_v = n, ls[n]
if ls[i] != min_v:
ls[i], ls[loc] = ls[loc], ls[i]
i += 1
return ls
#以下有错误,当时没注意
# @staticmethod
# def insert_sort(ls):
# if not ls:
# return []
# i = 1
# lenth = len(ls)
# while i < lenth:
# for n in range(0, i):
# if ls[n] > ls[n+1]:
# ls[n], ls[n+1] = ls[n+1], ls[n]
# i += 1
# return ls
#更正如下:
     @staticmethod
def insert_sort(ls):
if not ls:
return []
i =
lenth = len(ls)
while i < lenth:
tmp = ls[i]
for n in range(i, , -):
if tmp < ls[n-]:
ls[n] = ls[n-] #the smaller ahead
else:
ls[n] = tmp
break
i +=
return ls

     @staticmethod
def shell_sort(ls):
if not ls:
return []
lenth = len(ls)
increment = lenth //
while increment > : # stop the loop when increment =
loc = increment # start from location increment
while loc < lenth:
tmp = ls[loc]
for i in range(loc, increment-, -increment):
if tmp < ls[i-increment]: #if large than the value in loc
ls[i] = ls[i-increment] # move to the next location
else:
break
ls[i] = tmp # move the value in loc to location i
loc += # loop from increment to the last of ls
increment //= 2
return ls # this method allow sb. to sort the ls in different increment
 if __name__ == '__main__':
ls = [1, 9, 5, 4, 3, 7, 6]
s = Sort()
print(s.bubble_sort(ls[:]))
print(s.select_sort(ls[:]))
print(s.insert_sort(ls[:]))
53 print(s.shell_sort(ls[:]))

可知:

  1. 冒泡排序是将最大值(最小值)通过相邻交换到行尾(行首);
  2. 选择排序是选出最大值(最小值)放到行尾(行首);
  3. 插入排序是通过相邻交换将下一个数插入到已经排好序的序列中。
  4. 希尔排序与3相近,但通过设置间隔分别排序,直到间隔为1完成排序。

使用Python完成排序(冒泡、选择、插入法)的更多相关文章

  1. python 数据结构与算法之排序(冒泡,选择,插入)

    目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习 ...

  2. 基本排序-冒泡/选择/插入(python)

    # -*- coding: utf-8 -*- import random def bubble_sort(seq): n = len(seq) for i in range(n-1): print( ...

  3. Python学习之---冒泡,选择,插入排序

    Python学习之---冒泡,选择,插入排序 最近学习了python基础,写一下3大排序练练手: 1 ''' 2 Created on 2013-8-23 3 4 @author: codegeek ...

  4. python实现排序算法 时间复杂度、稳定性分析 冒泡排序、选择排序、插入排序、希尔排序

    说到排序算法,就不得不提时间复杂度和稳定性! 其实一直对稳定性不是很理解,今天研究python实现排序算法的时候突然有了新的体会,一定要记录下来 稳定性: 稳定性指的是 当排序碰到两个相等数的时候,他 ...

  5. C++学习(三十八)(C语言部分)之 排序(冒泡 选择 插入 快排)

    算法是解决一类问题的方法排序算法 根据元素大小关系排序 从小到大 从大到小冒泡 选择 插入 快排希尔排序 归并排序 堆排序 冒泡排序 从头到尾比较 每一轮将最大的数沉底 或者最小数字上浮 选择排序 1 ...

  6. python算法与数据结构-选择排序算法(33)

    一.选择排序的介绍 选择排序(Selection sort)是一种简单直观的排序算法.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素, ...

  7. Java数据结构和算法(三)--三大排序--冒泡、选择、插入排序

    三大排序在我们刚开始学习编程的时候就接触过,也是刚开始工作笔试会遇到的,后续也会学习希尔.快速排序,这里顺便复习一下 冒泡排序: 步骤: 1.从首位开始,比较首位和右边的索引 2.如果当前位置比右边的 ...

  8. 用python编写排序算法

    交换排序 === 冒泡排序,快速排序 插入排序 ===直接插入排序,希尔排序 选择排序 === 简单选择排序,堆排序 归并排序 基数排序 冒泡排序 要点 冒泡排序是一种交换排序. 什么是交换排序呢? ...

  9. 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)

    本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...

  10. Python的排序

    1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a','have','I' ...

随机推荐

  1. ISE软件报错

    ISE弹出如下报错并关闭程序或在编译时出现PATH类报错 一,解决办法 本人自己试了一下  E:\ISE\14.7\ISE_DS\settings64.bat E:\ISE\14.7\ISE_DS\I ...

  2. day18 logging模块 sys shelve

    昨日回顾 re 正则表达式 匹配字符串 场景 例如:爬虫,密码规则验证,邮箱地址验证,手机号码 学习re主要学习的就是 那一堆特殊符号 hashlib hash是一种算法 lib表示库 该模块包含了一 ...

  3. python基础入门学习简单程序练习

    1.简单的乘法程序 i = 256*256 print('The value of i is', i) 运行结果: The value of i is 65536 2.执行python脚本的两种方式 ...

  4. TOJ 3151: H1N1's Problem(欧拉降幂)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3151 时间限制(普通/Java): ...

  5. 2018年全国多校算法寒假训练营练习比赛(第四场)B:道路建设

    传送门:https://www.nowcoder.net/acm/contest/76/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 65536K,其他语言131072K 64b ...

  6. FileReader读取文件详解

    FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. <in ...

  7. 自定义进度条渐变色View

    package com.jianke.stepCounter.Activity; import android.annotation.SuppressLint; import android.cont ...

  8. linq to sql语句中转换数据类型和日期操作

    System.Data.Entity.SqlServer.SqlFunctions.StringConvert System.Data.Entity.DbFunctions

  9. IIS 7.5 上传文件大小限制

    上传插件:uploadify IIS版本:7.5 描述: 从IIS6升级到IIS7.5以后,网站上传文件大小被限制了,在Chrome下提示:ERR_CONNECTION_RESET,网上的各种方法都试 ...

  10. devexpress WinForms MVVM

    WinForms MVVM This section is dedicated to the Model-View-ViewModel (MVVM) architectural pattern. Yo ...