使用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' ...
随机推荐
- 【C++】c++11多线程初探
相关头文件c++11 新标准中引入了四个头文件来支持多线程编程,他们分别是<atomic> ,<thread>,<mutex>,<condition_vari ...
- 【OpenGL】第一个窗口
包含头文件: #include <GL/glew.h> // GLFW #include <GLFW/glfw3.h> 初始化与配置GLFW: glfwInit(); //初始 ...
- php打印错误报告
//error handler functionfunction customError($errno, $errstr){ echo "<b>Error:</b> ...
- KNN算法应用
import numpy as np# 运算符模块,这里主要用来排序 import operator import matplotlib.pylab as plt def create_dataset ...
- centos 7 下 TFTP服务器安装
一.介绍 TFTP(Trivial File Transfer Protocol,简单文件传输协议)),是一个基于UDP 协议 69端口 实现的用于在客户机和服务器之间进行简单文件传输的协议提供不复杂 ...
- WAS 添加数据源
一.创建安全性别名认证 1.资源-全局安全性-JAVA认证和授权服务-J2C认证数据 2.新建 3.输入别名,这里后面加IP末尾.输入用户名.密码. 4.点击确定.保存. 二.创建数据源连接配置 1. ...
- WAS 忘记密码
一.重置密码 1.首先关闭was,ps –ef|grep java 查看java进程号,然后kill -9 XXXX杀掉进程即可.或者使用命令./stopServer.sh server1 2.取消控 ...
- 2018年全国多校算法寒假训练营练习比赛(第四场)F:Call to your teacher
传送门:https://www.nowcoder.net/acm/contest/76/F 题目描述 从实验室出来后,你忽然发现你居然把自己的电脑落在了实验室里,但是实验室的老师已经把大门锁上了.更糟 ...
- Genymotion 模拟器上网出现 net::ERR_NAME_NOT_RESOLVED
Genymotion 模拟器在公司网络安装的,然后启动能正常上网,把笔记本带回家,网络变化了,再使用模拟器 上网显示: (net::ERR_NAME_NOT_RESOLVED) 各种百度,最后用如下方 ...
- TZOJ 4712 Double Shortest Paths(最小费用最大流)
描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ...