python - python的快速排序算法
问题描述
我尝试使用 numpy 算法为快速排序算法编写一些代码。但它似乎无法正常工作。你能帮我解决吗?
import numpy as np
def quick_sort_np(narr):
"""A numpy version of quick sort algorithm"""
narr = np.array(narr)
if len(narr)<= 1 :
return narr
p = narr[-1] # I take the last item as pivot by convension
print (f"at this level the pivot is {p}")
lnarr = narr[narr<p]
print (f"----------------------> left arr is {lnarr}")
rnarr = narr[narr>p]
print (f"----------------------> right arr is {rnarr}")
return quick_sort_np(lnarr) + p + quick_sort_np(rnarr)
如果 [1,2,6,5,4,8,7,99,33] 作为输入,我的代码什么也不返回,这就是问题所在。
解决方案
+
作用于 np.arrays 是元素加法,而不是串联。
推荐阅读
- azure-active-directory - 如何获取 Onedrive (for business) 文件的 UniqueId?
- android - AndroidManifest 添加 admob 后出现 14 个错误
- html - #document 是什么意思?以及如何选择该代码之外的元素?
- typescript - lib.es2015.promise.d.ts(33, 34):未提供“价值”的参数
- javascript - 如何防止div类jquery中的点击动作
- css - Angular Material Theme 不会更改 mat-dialog 强调色
- android - 如何在颤动中更改文本小部件的值
- unit-testing - vuejs 单元测试具有子组件的组件
- entity-framework - Fluent Api 复合主键
- configuration - 是否有任何配置库提供用于合并拆分文件中的修改的本机实现(如 .d 文件夹中所示)