没事的时候恶心一下,学习Python基本语法。
# -*- coding: cp936 -*-
#Python在默认情况下,最大递归深度为1000
#(理论值,其实真实情况下,只有995左右,各个系统这个值的大小也不同)。
#import sys
#sys.setrecursionlimit(99999)
#Python快排神优雅实现
#def qsort(L):
# if not L: return []
# return qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + \
# qsort([x for x in L[1:] if x>=L[0]])
import sys
import random
count = 0
def godlikeQsort(data):
if not data: return []
return godlikeQsort([x for x in data[1:] if x < data[0]]) + \
data[0:1] + \
godlikeQsort([x for x in data[1:] if x >= data[0]])
def qsort(data, begin, end):
if begin < end:
#使用全局变量方法,因为出现修改
#global count
#print "Recursion: ", count
#count += 1
mid = randomPartition(data, begin, end)
qsort(data, begin, mid - 1)
qsort(data, mid + 1, end)
def randomPartition(data, begin, end):
randomNum = random.randint(begin, end)
data[randomNum], data[end] = data[end], data[randomNum]
return partition(data, begin, end)
def partition(data, begin, end):
mid = begin - 1
standard = data[end]
for num in range(begin, end):
if data[num] <= standard:
mid += 1
data[num], data[mid] = data[mid], data[num]
data[mid + 1], data[end] = data[end], data[mid + 1]
return mid + 1
if __name__ == "__main__":
array = []
size = int(raw_input ("Size of the array: "))
for i in range(0, size):
#用str强制类型转换
array.append(int(raw_input ("Please input the No." + str(i + 1) +" number:")))
print "Original array:\t\t", array
qsort(array, 0, len(array) - 1)
#print godlikeQsort(array)
print "Ascending array:\t", array