Python实现的常用排序方法
for i in range(0,len(a)):
for j in range(0,len(a)-i-1):
if a[j]>a[j+1]:
temp = a[j+1]
a[j+1] = a[j]
a[j] = temp
#print(a)
#print(a)
print(a)
for i in range(0,len(a)):
k=i
temp = a[i]
for j in range(i,len(a)):
if a[j]<temp:
temp = a[j]
k = j
a[k] = a[i]
a[i] = temp
print(a)
def sort(a,low,high):
while low < high:
temp = a[low]
while low < high and a[high]>=temp:
high = high-1
a[low]=a[high]
while low<high and a[low]<temp:
low = low+1
a[high]=a[low]
a[low]=temp
return low
def quicksort(a,low,high):
if low<high:
middle = sort(a,low,high)
quicksort(a,low,middle)
quicksort(a,middle+1,high)
print(a)
sort(a,0,len(a)-1)
quicksort(a,0,len(a)-1)
print(a)
#实际上应该是不固定的low值,他每个子循环不定。
def sort(a,low,high):
while low < high:
temp = a[low]
while low < high and a[high]>=temp:
high = high-1
while low<high and a[high]<temp:
a[low]=a[high]
low =low+1
a[high]=a[low]
a[low]=temp
return low
def quicksort(a,low,high):
if low<high:
middle = sort(a,low,high)
quicksort(a,low,middle)
quicksort(a,middle+1,high)
print(a)
sort(a,0,len(a)-1)
quicksort(a,0,len(a)-1)
print(a)
a =[7,1,3,2,6,54,4,4,5,8,12,34]
for i in range(0,len(a)-1):
temp=a[i+1]
j=i+1
while j>=0 and temp<a[j-1]:
j=j-1
print(j)
if j>=-1:
k= i+1
while k>=j:
a[k]=a[k-1]
k=k-1
print(a)
a[j]=temp
print(a)
for i in range(1,len(a)-1):
temp=a[i]
j=i-1
while j>=0 and temp<=a[j]:
print(temp)
j=j-1
if j >=-1:
a[i:i+1]=[]
a.insert(j+1,temp)
print(a)
print(a)
Python实现的常用排序方法的更多相关文章
- python进阶之类常用魔法方法和魔法属性
前言 前面我们总结过了python的关键字.运算符.内置函数.语法糖等与python魔法方法之间的关系,现在我们更细一点,看看python的面向对象编程有哪些常用的魔法属性和魔法方法. 魔法属性 对于 ...
- python中list常用的方法
Python 列表 list (以下内容为比较初级适合小白查看的笔记) 一.介绍: 列表是Python中内置有序.可变序列,列表的所有元素放在一对中括号“[]”中,并使用逗号分隔开: ...
- Python学习之==>常用字符串方法
1.常用字符串方法 a = '\n 字 符 串 \n\n' b = a.strip() # 默认去掉字符串两边的空格和换行符 c = a.lstrip() # 默认去掉字符串左边的空格和换行符 d = ...
- Python实现一些常用排序算法
一些常用的排序 #系统内置排序算法#list.sort()#heapq模块 def sys_heap_sort(list): import heapq heap = [] for i in range ...
- Python学习_数据排序方法
Python对数据排序又两种方法: 1. 原地排序:采用sort()方法,按照指定的顺序排列数据后用排序后的数据替换原来的数据(原来的顺序丢失),如: >>> data1=[4,2, ...
- Windows下python库的常用安装方法
目录: 1.pip安装(需要pip) 2.通过下载whl文件安装(需要pip) 3.在pythn官网下载安装包安装(不需要pip) 方法一:pip安装. 这是最 ...
- Python 数据类型,常用函数方法分类
Python基本数据类型:(int) 字符串(str)列表(list)元组(tuple)字典(dict)布尔(bool) python中可以简单使用 类型(数据)创建或转换数据 例: #字符串转数字 ...
- python之字符串常用的方法
1. 去掉空格或换行符 s='. hello .world .\n' new_s = s.strip()#默认去掉字符串前后的空格和换行符 new_s = s.strip('.')#可传参去掉字符串前 ...
- HashMap和TreeMap的常用排序方法
一.简单描述 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,HashTable以及LinkedHashMap等. TreeMap:能够把它保存的记录根据键(key)排序 ...
随机推荐
- vmware linux NAT CON
NAT上网 vmware设置nat上网: 1.设置本地可提供上网网卡为共享方式到vmware 8(NAT)如下图: 2.设置本地虚拟网卡vmware8 IP地址如下图: 3.设置vmware网络设置启 ...
- bzoj2242 计算器
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- jackson快速实现对象与json之间的转换
Maven依赖: <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <arti ...
- 学习笔记之Kubernetes
Kubernetes | Production-Grade Container Orchestration https://kubernetes.io/ Kubernetes is an open-s ...
- 原生态的javascript的n种技巧(我从别人的博客中拷贝过来的,方便以后查阅)
1.原生JavaScript实现字符串长度截取 function cutstr(str, len) { var temp; var icount = 0; var patrn = /[^\x00-\x ...
- Spark Streaming 例子
NetworkWordCount.scala /* * Licensed to the Apache Software Foundation (ASF) under one or more * con ...
- Noip知识点备考
作为一个oier,适当的整理是有必要的.蒟蒻根据自己的理解,筛选出考noip应当掌握的知识点.可能后期还有解题思路和模板,先挖个坑慢慢补呗. 60级张炳琪Noip知识点总结 可能是本人比较弱,写的内容 ...
- mysql 更新(二)安装和基本管理
03-MySql安装和基本管理 本节掌握内容: MySQL的介绍安装.启动 MySQL破解密码 MySQL中统一字符编码 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目 ...
- 内置锁(二)synchronized下的等待通知机制
一.等待/通知机制的简介 线程之间的协作: 为了完成某个任务,线程之间需要进行协作,采取的方式:中断.互斥,以及互斥上面的线程的挂起.唤醒:如:生成者--消费者模式.或者某个动作完成,可以唤醒下一 ...
- django的小操作,查询效率up, 引用art-template模板+djangorestframework
Part1: 提高查询效率newses = News.objects.select_related('category', 'author').get(id=1) # category和author字 ...