Insertion sort works by maintaining a fully sorted list and inserting unsorted data one at a time then immediately sorting it.
So to begin with you have a single element, and that is considered sorted.
Then you add an item to the right of it. You compare these with each other and you might have to swap them. Then you have a sorted list of 2.
Then you add another item to the right of your list. You compare this with the one before it, if they're already sorted then you're done comparing for that iteration, otherwise swap them and move to the left. This goes on till you have no more data to insert.
No comments:
Post a Comment