-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize Insertion Sort #593
base: master
Are you sure you want to change the base?
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.