First, let me give some context: I teach a first course in numerics for students who major in biology and mathematics. In this course, I try to give the students good habits for manipulating array of numbers, such as using the ndarray data structure of NumPy.
I am aware of the fact that no for loop should be used over a ndarray. I want to illustrate this statement with a standard sorting algorithm. Of course I know how to code this naively, and this is not what I want to do here.
I don't know how to do it with all the machinery of NumPy, but I thought I could search by myself. I found the reference page for the sort
function in NumPy, which has the following signature
numpy.sort(a, axis=-1, kind='quicksort', order=None)
This page links to the source code of the function, which is useful. But then I'm lost! I don't understand where the magic happens in the following lines:
if axis is None:
a = asanyarray(a).flatten()
axis = 0
else:
a = asanyarray(a).copy(order="K")
a.sort(axis=axis, kind=kind, order=order)
return a
So my question is: what does this piece of code do?