I am working on classifying simple data using KNN with Euclidean distance. I have seen an example of what I would like to do that is done with the MATLAB knnsearch function as shown below:

load fisheriris

x = meas(:,3:4);

gscatter(x(:,1),x(:,2),species)

newpoint = [5 1.45];

[n,d] = knnsearch(x,newpoint,'k',10);

line(x(n,1),x(n,2),'color',[.5 .5 .5],'marker','o','linestyle','none','markersize',10)

The above code takes a new point i.e. [5 1.45] and finds the 10 closest values to the new point. Can anyone please show me a MATLAB algorithm with a detailed explanation of what the search function does? Is there any other way to do this?