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. Python 豆瓣日记爬取

    无聊写了个豆瓣日记的小爬虫,requests+bs4. cookies_src可填可不填,主要是为了爬取仅自己可见的日记. url填写的是日记页面,即https://www.douban.com/pe ...

  2. SML + NL + HJ

    Join是一种试图将两个表结合在一起的谓词,一次只能连接2个表,表连接也可以被称为表关联.在后面的叙述中,我们将会使用”row source”来代替”表”,因为使用row source更严谨一些,并且 ...

  3. python os.path模块常用方法详解(转)

    转自:https://www.cnblogs.com/wuxie1989/p/5623435.html os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方 ...

  4. 侯捷STL课程及源码剖析学习1

    1.C++标准库和STL C++标准库以header files形式呈现: C++标准库的header files不带后缀名(.h),例如#include <vector> 新式C hea ...

  5. js导出excel:前端当前数据的导出

    网上找的库文件,同样做了修改.在导出的时候,有时候数据第一列和最后一列可能是复选框和操作按钮,这个是我们不需要的,加了这个的过滤 //table2excel.js /* * jQuery table2 ...

  6. JavaWeb网站后台开发记录手册

    1.javaweb网站后台开发 1.封装DBTools类 1.注册数据库驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); 2 ...

  7. 【收藏】UI自动化测试基本规则与设计模式

    总体规则 所有模块设计均遵循page object结构 用例层:测试人员编写测试用例代码的地方,可以调用page层和封装层. page层:一个页面一个类,包含该页面的业务逻辑封装以及部分控件定义. 封 ...

  8. Codeforces Beta Round #67 (Div. 2)

    Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...

  9. Codeforces Beta Round #57 (Div. 2)

    Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...

  10. 46-wxpython 4 使用 grid 展示表格

    转载:https://blog.csdn.net/soslinken/article/details/79024938#%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B wxp ...