list - Is a.insert(0,x) an o(n) function? Is a.append an O(1) function? Python -


i trying move numbers in array front , odd numbers of array. problem asks in linear algorithm , in place.

i came this:

 def sort(a):     in range(0,len(a)-1):         if a[i]%2==0:             a.insert(0,a.pop(i))     return 

the issue that, told me technically, a.insert o(n) function technically considered non-linear algorithm (when including for in range part of function). since forum asked question couple months old, couldn't ask explanation.

basically believe said "technically" because since inserts @ front, not check n number of elements in array, therefore making run practical purposes @ o(n) , not o(n^2). correct assessment?

also, on forum used a.append modify above , changed odd numbers. no 1 replied wondering, a.append not o(n) function since moves end? o(1)?

thanks explanations , clarifications!

insert @ 0th index of list requires shifting every other element along makes o(n) operation. however, if use deque operation o(1).

append amortized o(1) operation since requires adding item on end of list , no shifting done. list needs grow not o(1) operation.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -