Reputation: 13
How can I sort an array in an ascending order only by using argmin()
?
For example, if I define sort_function(x)
which returns the ascending order of x
only by using argmin()
and do:
print(sort_function(np.array([1,6,3,7]))
the result should be [1 3 6 7]
.
Upvotes: 0
Views: 434
Reputation: 402673
Here it is, in blazing fast O(N ** 2)
-
np.argmin
on the listdef sort_function(x):
y = list(x)
while y:
yield y.pop(np.argmin(y))
list(sort_function(np.array([1, 6, 3, 7])))
[1, 3, 6, 7]
Upvotes: 5