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
时间: 2024-11-07 23:51:31

python Quicksort demo的相关文章

[硕.Love Python] QuickSort(快速排序)

def partition(a, i, j):     k = -1     while True:         while k * (a[i] - a[j]) >= 0:             if i == j:                  return i             j += k         a[i], a[j] = a[j], a[i]         i, j, k = j, i - k, -k def qsort(a, i, j):     if i <

python购物车demo

product_list = [        ('Iphone',11800),        ('Mac Pro',13800),        ('BMW CAR',480000),        ('Watch',10600),        ('Coffee',31),        ('fatyao Python book',35),]shopping_list = []salary = input("Input your salary:")if salary.isdigi

python脚本demo

import atexit import logging import os import signal import sys from logging.handlers import RotatingFileHandler logger = logging.getLogger() MAX = 10 * 1024 * 1024 BACK_UP_COUNT = 10 def setup_logging(): log_file = os.path.join(r'C:\\',"demo.log&quo

python: tk demo

# coding=utf-8 import time import tkinter as tk ######################################################################## class Window: def __init__(self, title='nms', width=300, height=120, staFunc=bool, stoFunc=bool): self.w = width self.h = height

python lmdb demo 这接口和BDB一样恶心啊!

import lmdb lmdb_img_name = "test.lmdb" env = lmdb.open(lmdb_img_name, map_size=1e6) with env.begin(write=True) as txn: txn.put("key", "xxxxxxxxxxxxxxxxxx") txn.put("key2", "?") txn.put("key3", &

python 正则表达式 demo

1.匹配大小写和数字,并且大小写数字均要有,且字符串长度为6~20位 ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,20}$ import repattern = re.compile("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,20}$")flag = pattern.match("abcDEF123")if flag: print "String matches t

1、Python基础&amp;demo

模拟用户登录name = input('请输入用户名')password = input('请输入密码')if name == 'root': if password == 'root': print('登录成功') else: print('密码错误')else: print('账号错误') num = input('请输入一个a-c的字符')if num == 'a': print('A')elif num == 'b': print('B')elif num =='c': print('C

visual studio 2015 搭建python开发环境,python入门到精通[三]

在上一篇博客Windows搭建python开发环境,python入门到精通[一]很多园友提到希望使用visual studio 2013/visual studio 2015 python做demo,这里略带一句,其实就"学习python"而言,比较建议使用pycharm,pycharm可以设置VS或者eclipse等多种IDE的编码,可以让绝大部分其他语言使用IDE的习惯者更容易上手.这一点兼容确实做的很好.不过既然这么多园友要求使用vs开发python的话,就介绍一下visual

主成分分析法原理及其python实现

主成分分析法原理及其python实现 前言: 这片文章主要参考了Andrew Ng的Machine Learning课程讲义,我进行了翻译,并配上了一个python演示demo加深理解. 本文主要介绍一种降维算法,主成分分析法,Principal Components Analysis,简称PCA,这种方法的目标是找到一个数据近似集中的子空间,至于如何找到这个子空间,下文会给出详细的介绍,PCA比其他降维算法更加直接,只需要进行一次特征向量的计算即可.(在Matlab,python,R中这个可以