data structures - Is a dynamically sized heap insertion technically O(n)? -
inserting element heap involves appending end of array , propagating upwards until it's in "right spot" , satisfies heap property, operation of o(logn).
however, in c, instance, calling realloc in order resize array new element can (and will) result in having copy entirety of array location in memory, o(n) in best , worst case, right?
are heaps in c (or language, matter) done fixed, pre-allocated size, or copy operation inconsequential enough make dynamically sized heap viable choice (e.g, binary heap keep searchable list of items)?
a typical scheme double size when run out of room. doubling--and copying goes it--does indeed take o(n) time.
however, notice don't have perform doubling often. if average out total cost of doubling on operations performed on heap did not involve doubling, cost indeed inconsequential. (this kind of averaging known amortized analysis.)
Comments
Post a Comment