Python 快排
不同点:
Perl 传递列表slice 实际上还指向父列表相同的元素,这是由Perl 本身的数据结构所决定的。而Python 传递slice 则是一个新的列表,所以参数传递稍有不同。
一个例子说明Perl传参的不同:
[plain] view plaincopy
sub test{
$_[2] = 'testing';
}
@x = (1..10);
test(@x[3..8]);
print "@x";
输出:
1 2 3 4 5 testing 7 8 9 10
快排代码:
[python]
import random
def qsort2(array):
if len(array) <= 1: return array www.zzzyk.com
else: return qsort2([i for i in array[1:] if i<array[0]]) + [array[0]] + qsort2([i for i in array[1:] if i>=array[0]])
def qsort(array, left, right):
if left >= right: pass
else:
print array,left,right
low = left+1
high = right
index = left
while low <= high:
while low<=high and array[low]<array[index]:
low += 1
while low<=high and array[high]>=array[index]:
high -= 1
if low<high:
array[low],array[high] = array[high],array[low]
if high > index:
array[index],array[high]=array[high],array[index]
print array, high
print
qsort(array, left, high-1)
qsort(array, high+1, right)
x = [random.randint(0,100) for i in range(10)]
print x
qsort(x,0,9)
print x
作者:USTBHacker
补充:Web开发 , Python ,