About insertion sort and especially why it's said that copy is much faster than swap?

Posted by Software Engeneering Learner on Programmers See other posts from Programmers or by Software Engeneering Learner
Published on 2012-11-14T14:01:44Z Indexed on 2012/11/14 17:29 UTC
Read the original article Hit count: 318

Filed under:
|
|
|

From Lafore's "Data Structures and Algorithms in Java": (about insertion sort (which uses copy + shift instead of swap (used in bubble and selection sort)))

However, a copy isn’t as time-consuming as a swap, so for random data this algo- rithm runs twice as fast as the bubble sort and faster than the selectionsort.

Also author doesn't mention how time consuming shift is.

From my POV copy is the simplest pointer assignment operation. While swap is 3x pointer assignment operations. Which doesn't take much time. Also shift of N elemtns is Nx pointer assignment operations.

Please correct me if I'm wrong.

Please explain, why what author says is true? I don't understand.

© Programmers or respective owner

Related posts about java

Related posts about learning