You can simply copy and paste this function to the folder you keep your function m-files.
Insertion Sort is of n^2 order and performs well for up to 10,000 inputs.
Examples of how you can use this function include, but aren't limited to:
plot(floor(100 * rand(1,100))); hold on;
plot(insertion_sort(floor(100 * rand(1,100))),'r');
a = insertion_sort([1 2 3 2 1 2 3 5 4 3 2 1 4 6 8])
I would appreciate your suggestions, further examples, comments and criticisms.
I am wondering what's the difference or advantage of this submitted code over the built-in function 'sort', which has more functionality?
The most important improvement would be a documentation: The first comment section after the "function ..." line is displayed by the "help" command. Without a documentation, which explains e.g. the type and dimensions of inputs and outputs as well as the applied procedure, a function is not usable, although it might be useful.
Look at Matlab's toolbox functions to learn more about the usual documentation style, e.g. the "H1-line".
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.