How to change the position of an element in an array (list... in python)?
Suppose I've a list and now I want to change the position of one of it's elements. For instance, I want to change the position of A[i] to j. How can I do it efficiently (with less runtime and memory)? #if you are non-pythonistas then please share the algorithm **not swapping ex. [1,2,3,4,5] > [1,4,2,3,5] #moving 4 from position 3 to 1 **its INSERTION **I've got one: A.insert(j, A.pop(i)) #It is not an efficient approach, because pop is deleting an element, and reducing the size of the list, and again insert is adding an element, and increasing the size of list. So, the length of the array is changing for 2 times, and change of length is expensive for any type of array. [Updated on March 5, 2021] Hello everyone! For the last few days I was trying to solve the above problem in the fastest way possible. Here goes my implementation: https://code.sololearn.com/cr6MgZwo82P1/?ref=app . If anyone has got some other idea to solve the same, then please let us know. Thanks!