使用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 多线程操作数据库
如果使用多线程操作数据库,容易引起多用户操作锁表 OperationalError: (2013, 'Lost connection to MySQL server during query') 使用 ...
- pta l3-3(社交集群)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805053141925888 题意:给定n个人,以及每个人的兴趣 ...
- 第三章 列表(b)无序列表
- Linux下查看编辑二进制文件:hexedit神器
1.如果没有hexedit安装hexedit:2.打开终端输入hexedit filename;3.打开即可用键盘输入字符,用方向键移动,ctrl+w保存,ctrl+x退出:4.详细用法 man he ...
- 使用ffmpeg从mp4文件中提取视频流到h264文件中
ffmpeg -i 2018.mp4 -codec copy -bsf: h264_mp4toannexb -f h264 tmp. 注释: -i 2018.mp4: 是输入的MP4文件 -code ...
- 204. Count Primes (Integer)
Count the number of prime numbers less than a non-negative number, n. 思路:寻找质数的方法 class Solution { pu ...
- c# double 类型保留几位小数
C 或 c :货币 D 或 d:十进制数 E 或 e:科学记数法(指数) F 或 f:定点 G 或 g:常规 N 或 n:数字 P 或 p:百分比 double.ToString("Nx & ...
- 使用vue-cli快速搭建大型单页应用
前言: 经过一段时间angular的洗礼之后 ,还是决定回归Vue.现就vue安装.工程搭建.常用依赖安装直至开发挣个流程做一整理,希望对初学者有所帮助. 前提条件: 对 Node.js 和相关构建工 ...
- 并行网络爬虫(C++实现)
step1 使用socket编程技术,利用http协议,抽取网页中的url,实现简单的爬虫. socket int socket (int domain, int type, int protocol ...
- 总结函数open与fopen的区别
转自:https://www.zybuluo.com/yiltoncent/note/87461 对于这两个名字很类似的函数,对于很多初学者来说,不容易搞清楚它们有什么不同,只知道按照函数用法使用.如 ...