My task for you this time is to implement an insertion sort algorithm. Sure, everyone and his dog can do it; but how easy is it to do in the fewest characters? One must take an array (or suitable alternative in your language) of 32-bit integers and sort them by value via the insertion sort method, then return the sorted array as a copy (not damaging the original).
Examples:
Input: {20, 45, 3, 62 }
Output: {3, 20, 45, 62 }
Input: {5, 2, 3, 1, 5, 4}
Output: {1, 2, 3, 4, 5, 5}
Misc:
- No using builtin or existing implementations of any kind of sorting in libraries.
- Only the code for the actual algorithm counts; it's up to you to output the results.
- The page i specified covers all the details of insertion sort so look there if you have any questions about the algorithm.