File Exchange

image thumbnail

Insertion Sort MatLab Function

version 1.0 (1.12 KB) by

[output] is an array of number which is the sorted version of [input].



View License

  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.

Best Regards

Comments and Ratings (2)


wenwu (view profile)

I am wondering what's the difference or advantage of this submitted code over the built-in function 'sort', which has more functionality?

Jan Simon

Jan Simon (view profile)

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".

MATLAB Release
MATLAB 7.9 (R2009b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video