使用Python完成排序(冒泡、选择、插入法)
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[:]))
可知:
- 冒泡排序是将最大值(最小值)通过相邻交换到行尾(行首);
- 选择排序是选出最大值(最小值)放到行尾(行首);
- 插入排序是通过相邻交换将下一个数插入到已经排好序的序列中。
- 希尔排序与3相近,但通过设置间隔分别排序,直到间隔为1完成排序。
使用Python完成排序(冒泡、选择、插入法)的更多相关文章
- python 数据结构与算法之排序(冒泡,选择,插入)
目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习 ...
- 基本排序-冒泡/选择/插入(python)
# -*- coding: utf-8 -*- import random def bubble_sort(seq): n = len(seq) for i in range(n-1): print( ...
- Python学习之---冒泡,选择,插入排序
Python学习之---冒泡,选择,插入排序 最近学习了python基础,写一下3大排序练练手: 1 ''' 2 Created on 2013-8-23 3 4 @author: codegeek ...
- python实现排序算法 时间复杂度、稳定性分析 冒泡排序、选择排序、插入排序、希尔排序
说到排序算法,就不得不提时间复杂度和稳定性! 其实一直对稳定性不是很理解,今天研究python实现排序算法的时候突然有了新的体会,一定要记录下来 稳定性: 稳定性指的是 当排序碰到两个相等数的时候,他 ...
- C++学习(三十八)(C语言部分)之 排序(冒泡 选择 插入 快排)
算法是解决一类问题的方法排序算法 根据元素大小关系排序 从小到大 从大到小冒泡 选择 插入 快排希尔排序 归并排序 堆排序 冒泡排序 从头到尾比较 每一轮将最大的数沉底 或者最小数字上浮 选择排序 1 ...
- python算法与数据结构-选择排序算法(33)
一.选择排序的介绍 选择排序(Selection sort)是一种简单直观的排序算法.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素, ...
- Java数据结构和算法(三)--三大排序--冒泡、选择、插入排序
三大排序在我们刚开始学习编程的时候就接触过,也是刚开始工作笔试会遇到的,后续也会学习希尔.快速排序,这里顺便复习一下 冒泡排序: 步骤: 1.从首位开始,比较首位和右边的索引 2.如果当前位置比右边的 ...
- 用python编写排序算法
交换排序 === 冒泡排序,快速排序 插入排序 ===直接插入排序,希尔排序 选择排序 === 简单选择排序,堆排序 归并排序 基数排序 冒泡排序 要点 冒泡排序是一种交换排序. 什么是交换排序呢? ...
- 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)
本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...
- Python的排序
1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a','have','I' ...
随机推荐
- python 内置函数(一),低阶内置函数功能汇总
python 内置函数 68个 今日主要内容: 1.内置函数 一.内置函数 1.内置函数 详细细节内容地址(id):https://mubu.com/edit/odv-2Dkb6j
- leetcode 树类型题
树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- c++三种进制格式
来源:c++ primer plus 常用的进制有二进制,八进制,十进制,十六进制,在c++的头文件iostream里除了提供了endl控制符之外,还提供了控制进制的控制符,(不含二进制),分别是八进 ...
- 分布式处理框架MapReduce的深入简出
1).MapReduce的概述 2).MapReduce 编程模型 3).MapReduce架构 4).MapReduce编程 Google MapReduce论文,论文发表于2004年 Hadoop ...
- monobehaviour生命周期完整版
- vue 点击图片显示大图
使用指南:https://www.npmjs.com/package/vue-directive-image-previewer 简单使用: 1.安装vue-directive-image-previ ...
- css浮动知识点(转)
转自:http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html 很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能 ...
- SAP中的slashX
SlashX /n This terminates the transaction. 关闭当前事务. /nxxxx This terminates the current transaction, a ...
- linux命令学习之:systemctl
systemctl命令是系统服务管理器指令,主要负责控制systemd系统和服务管理器,它实际上将 service 和 chkconfig 这两个命令组合到一起. CentOS 7.x开始,CentO ...
- 关于MYSQL字符集问题(一)
MySQL的字符集支持(Character Set Support)有两个方面: 字符集(Character set)和排序方式(Collation). 对于字符集的支持细化到四个层次: 服务器(se ...