在算法导论的,快速排序,第7章,直接运行代码
感谢网站 https://www.tutorialspoint.com/execute_python_online.php
代码抄袭
# Hello World program in Python
print "Hello World!\n"
def quick_sort(array, l, r):
if l < r:
q = partition(array, l, r)
quick_sort(array, l, q - 1)
quick_sort(array, q + 1, r)
def partition(array, l, r):
x = array[r]
i = l - 1
for j in range(l, r):
if array[j] <= x:
i += 1
array[i], array[j] = array[j], array[i]
array[i + 1], array[r] = array[r], array[i+1]
return i + 1
import random
def random_int_list(start, stop, length):
start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
length = int(abs(length)) if length else 0
random_list = []
for i in range(length):
random_list.append(random.randint(start, stop))
return random_list
lala=[]
lala =random_int_list(1,1000,30)
quick_sort(lala,0,29)
print lala
$python main.py
Hello World!
[1, 183, 188, 194, 229, 246, 263, 277, 295, 325, 356, 361, 441, 452, 467, 472, 481, 497, 523, 656, 657, 742, 789, 802, 829, 834, 858, 866, 886, 995]
```
网友评论