奇数长度的数组(15):
a = np.array([5,5,5,5,5,5,5,5,5,5,5,5,5,5,5])
np.argsort(a)
输出= [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
奇数长度数组(17):
a = np.array([5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5])
np.argsort(a)
输出= [0 14 13 12 11 10 9 15 8 6 5 4 3 2 1 7 16]
奇数长度的数组(35):
a = np.array([5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5])
np.argsort(a)
输出= [0 19 20 21 22 23 24 18 25 27 28 29 30 31 32 26 33 17 15 1 2 3 4 5 6 16 7 9 10 11 12 13 14 8 34]
偶数长度数组(16):
a = np.array([5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5])
np.argsort(a)
输出= [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
偶数长度数组(18):
a = np.array([5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5])
print len(a)
输出= [0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 16 17]
偶数长度数组(36):
a = np.array([5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5])
np.argsort(a)
输出= [0 20 21 22 23 24 25 19 26 28 29 30 31 32 33 27 18 17 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35]
那么有人会解释我这个算法在数字相同的情况下是如何工作的吗?
答案 0 :(得分:1)
argsort()
使用可配置排序算法;您可以选择 quicksort , mergesort 或 heapsort 。来自argsort
documentation:
numpy.argsort(a, axis=-1, kind='quicksort', order=None)
[...]
善意: {'quicksort','mergesort','heapsort'},可选
排序算法。
默认值为quicksort,维基百科声明:
在高效的实现中,它不是一个稳定的排序,这意味着不保留相等排序项的相对顺序。
Numpy的实现很有效,并且它不保留相等值的相对顺序。
如果您需要稳定排序,请改用mergesort:
>>> a = np.array([5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5])
>>> np.argsort(a, kind='mergesort')
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34])
Heapsort也不稳定。