Optimize Insertion Sort #593
Conversation
|
I don't get the advantage of this change. |
Consider we want to insert 5-th element (value = 2) in correct place in this list: In pervious algorithm, we had to swap 4 times, (6, 2) and (5, 2) and (4, 2) and (3, 2). Since each swap needs assignment 2 times as least, we'll have variable assignment 8 times in minimum. In general, if we need swaps k times, we'll have 2 * k assignments. But in new algorithm in same condition, we shift 3, 4, 5 and 6 then insert 2 in correct index. then we'll have k + 2 assignments which is better than 2 * k |
@AryanAhadinia Thanks for your explaination.
|
|
Yes, it is correct I think, I'll work on this and I'll commit changes ASAP. |
Actually, there is a little mistake in this code, I'll correct this and commit in few seconds. |
|
Now it is optimized! |
No description provided.