In: Computer Science
How many times are we copying elements to the right while applying insertion sort on the input sequence 3 5 2 8 3?
How many times are we copying elements to the right while applying insertion sort on the input sequence 5 4 3 2 1?
1.3,5,2,8,3:
Intially the array is empty. So no copy operation. 3 is inserted into an array. While inserting 5 it won't occur any problem due to 3<5. While inserting 2 , 5 has to be copied to right by one position and 3 by one position. So till now 2 copy operations are done. Now, while inserting 8 no copy operations. While inserting we need to perform 3 copy right operations,for making place for 3.so, totally 5 copy operations are required during insertion sort for the sequence.
5, 4,3,2,1.
While inserting 5 no need to perform any copy operation. While inserting 4 we need to perform one copy right operation. For inserting 3 we need to perform 2 copy right operations. So totally 3 copy right operations are needed upto this iteration. For inserting 2 we do require 3 copy operations. So 6 copy right operations. For inserting 1 we do require 4 copy right operations. So finally, we do require 10 copy right operations while applying insertion sort.
If you have any queries... Please comment.. Thank you