Some implementation of insertion sort is very similar to the concept of bubble sort. So I was always confused and want to find ways to distinguish them better.
For insertion sort, the key assumption and loop-invariant is: the sequence infront is already sorted.
So, when inserting current node into correct position, comparison stops whenever encounter smaller nodes.
For bubble sort, there is no such assumption. So always need comparison.
References:
http://blog.csdn.net/morewindows/article/details/6665714
http://www.cnblogs.com/yeguo/archive/2013/05/14/3078202.html
http://blog.csdn.net/notalmost/article/details/7535523
No comments:
Post a Comment