快速排序与Python [英] Quick sort with Python
本文介绍了快速排序与Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我完全新的Python,我想在这上面实现快速排序。 可能有人请帮助我完成我的code?
我不知道如何连接三个数组进行打印。
高清排序(数组= [12,4,5,6,7,3,1,15]):
少= []
等于= []
更大= []
如果len(阵列)> 1:
透视=阵列[0]
对于x数组:
如果x<枢:
less.append(X)
如果x ==支点:
equal.append(X)
如果x>枢:
greater.append(X)
排序(少)
排序(支点)
排序(大于)
解决方案
高清排序(数组= [12,4,5,6,7,3,1,15]):
少= []
等于= []
更大= []
如果len(阵列)> 1:
透视=阵列[0]
对于x数组:
如果x<枢:
less.append(X)
如果x ==支点:
equal.append(X)
如果x>枢:
greater.append(X)
#不要忘了返回的东西!
返回排序(少)+平等+排序(较大)#只需使用+运算符连接列表
要平等#注意^^^^^没有支点
其他:#你需要喊得部分在递归的终点 - 当你只有在你的数组的一个元素,就返回数组。
返回数组
I am totally new to python and I am trying to implement quick sort in it. Could someone please help me complete my code?
I do not know how to concatenate the three arrays and printing them.
def sort(array=[12,4,5,6,7,3,1,15]):
less = []
equal = []
greater = []
if len(array) > 1:
pivot = array[0]
for x in array:
if x < pivot:
less.append(x)
if x == pivot:
equal.append(x)
if x > pivot:
greater.append(x)
sort(less)
sort(pivot)
sort(greater)
解决方案
def sort(array=[12,4,5,6,7,3,1,15]):
less = []
equal = []
greater = []
if len(array) > 1:
pivot = array[0]
for x in array:
if x < pivot:
less.append(x)
if x == pivot:
equal.append(x)
if x > pivot:
greater.append(x)
# Don't forget to return something!
return sort(less)+equal+sort(greater) # Just use the + operator to join lists
# Note that you want equal ^^^^^ not pivot
else: # You need to hande the part at the end of the recursion - when you only have one element in your array, just return the array.
return array
这篇关于快速排序与Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文