python Quicksort demo
__author__ = 'student' '''
quicksort
step 1, choose one pivot, such as pivot=la[0]
step 2, scan the data from right side, find data less than pivot, then swap this with pivot
pivot=1 [4] 5 7 3 20 9 [j]
then scan from left side, find data greater than pivot, then swap the position j and i
4 [] 7 3 20 9 5
when i>=j then finish one loop. then put the pivot in the i;
all data are dived by pivot now. left is less than pivot and right are greater than pivot.
think step by step then do it and try something
step 3. then you have two parts to sort, left part and right part.
recursive call this method to sort
'''
import random def quicksort(la,l,r):
if l>=r :
return
left=l;right=r
pivot=la[left]
while left < right:
while left<right and la[right]>pivot:
right-=1
if left<right :
la[left]=la[right]
left+=1
while left<right and la[left]<pivot:
left+=1
if left<right:
la[right]=la[left]
la[left]=pivot
quicksort(la,l,left-1)
quicksort(la,left+1,r) def quicksort2(la):
if len(la)<=1:
return la
return quicksort2([lt for lt in la[1:] if lt<la[0]])+ la[0:1]+quicksort2([ge for ge in la[1:] if ge>=la[0]]) import sys
sys.setrecursionlimit(999) la=[]
def generatenumbers(la,len):
for x in range(len):
la.extend([random.randint(1,50)])
generatenumbers(la,1000)
print la
quicksort(la,0,len(la)-1)
print la
python Quicksort demo的更多相关文章
- python supervisor demo deployment
I did a demo about how to deploy other python apps served by a 'supervisord' daemon processor on git ...
- RSA签名 python PHP demo 例子
python RSA+MD5签名demo: #!/usr/bin/env python2.7 #coding:utf-8 import base64 from Crypto.PublicKey imp ...
- 学习python登录demo
要求编写登录接口 : 1. 输入用户名和密码 2.认证成功后显示欢迎信息 3.用户名输错,提示用户不存在,重新输入(5次错误,提示尝试次数过多,退出程序) 4.用户名正确,密码错误,提示密码错误,重新 ...
- python购物车demo
product_list = [ ('Iphone',11800), ('Mac Pro',13800), ('BMW CAR',480000), ...
- python flask demo
from flask import Flask, jsonify from flask import abort from flask import make_response from flask ...
- python redis demo
上代码,redis-demo #!/usr/bin/env python #_*_ coding:UTF-8 _*_ import redis ####配置参数 host = '192.168.0.1 ...
- python thrift demo
简介Thrift最初由Facebook研发,主要用于各个服务之间的RPC通信,支持跨语言,常用的语言比如C++, Java, Python, PHP, Ruby, Erlang, Perl, Hask ...
- python 正则表达式 demo
1.匹配大小写和数字,并且大小写数字均要有,且字符串长度为6~20位 ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,20}$ import repattern ...
- [ Python ] unittest demo
# -*- coding: utf-8 -*- import unittest class MyUT(unittest.TestCase): def test_1(self): print(" ...
随机推荐
- asp.net的code-Behind技术
新建一个VS.NET下的项目..看到ASPX,RESX和CS三个后缀的文件了吗??这个就是代码分离.实现了HTML代码和服务器代码分离.方便代码编写和整理. code-Behind:asp.net中的 ...
- DataSet与DataTable对象
DataSet与DataTable对象 摘自:http://www.cnblogs.com/fttbfttb/articles/1509662.html DataSet对象 DataSet是ADO.N ...
- 重新想象 Windows 8 Store Apps (58) - 微软账号
[源码下载] 重新想象 Windows 8 Store Apps (58) - 微软账号 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 微软账号 获取微软账号的用户 ...
- sql:MySQL 6.7 表,视图,存储过程结构查询
#数据库MySQL 6.7 use sakila; #查询表名 show tables; # SELECT TABLE_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA. ...
- csharp:引入app.manifest,程序在win7下以管理员权限运行配置方法
https://msdn.microsoft.com/en-us/library/windows/desktop/hh848036(v=vs.85).aspx https://msdn.microso ...
- SQL Server性能影响的重要结论
第一次访问数据会比接下来的访问慢的多,因为它要从磁盘读取数据然后写入到缓冲区: 聚合查询(sum,count等)以及其他要扫描大部分表或索引的查询需要大量的缓冲,而且如果它导致SQL Server从缓 ...
- 干净的停止tomcat/java应用程序
通常在使用了jdbc或者netty的应用程序中,当shutdown tomcat或java应用程序时,会出现无法停止的情况,报类似如下错误: 严重: The web application [] re ...
- FAQ_1_陌生的VERSION.SDK_INT
看到VERSION.SDK_INT不禁诧异,这是何物?! 看API的定义,如下: 原来是一个常量值.但是这个常量值可以根据系统的不同而不同哟!为了揭开其神秘的面纱,将源码ctrl如下: 可以看出,获取 ...
- 【GOF23设计模式】代理模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_代理模式.静态代理 package com.test.proxy.staticProxy; public interfac ...
- .net mysql 支持表情
1.表 utf8mb4 2.字段 utf8mb4 3.连接字符串 utf8mb4 4.存储过程参数 utf8mb4