Just don't get irritated by the resizing of the arrays. The algorithm itself does nothing else than loop through the array to match a position based on a value given. So, to sort a pre-existing array, you would need to call the sorting again and again, for every value. Just retrieve the value to sort, replace the cell with a value that won't normally occur (e.g. -1 in an array that only stores positive values, or a string in an array that stores numbers) and add an exception condition that ignores such replaced cell. The position value you get, is the exact position (no need to +1 it). Copy the cell at that position to the replaced one and enter the current value at the new position.
I will try to do an example for pre-existing arrays, but I'm very busy with a project right now, so please be patient (Or maybe you figure it out for yourself? Would like to hear it then, so I wouldn't need to do the example)
Happy developing :)
EDIT: I just read a bit more about Timsort, the algorithm used in Python. It actually is a quite clever one, recognizing already sorted parts and working only on subsets that need to be sorted. Not a bad choice to use it.tulamide2012-06-16 10:33:32