How is the time complexity of creating a max heap O(n) -
how time complexity of creating max heap o(n). child node compare parent in o(i) or o(log n).
a. complexity o(n) in case when sorting using bubble down approach.
idea here heap built blindly inserting elements array , start correcting/swapping dominance violations using bottom approach. advantage: have n/2 leaf nodes not require bubble down obey dominance automatically. move internal parents , compare children. if violation there swap/bubble down. move higher parents , on internal(parent) nodes require swapping in correspondence height of level in. n/2 leaves. n/4 @ height 1, n/8 @ height of 2...leading cost of building heap following equation.(max height:lg n)
summation 0 lg n: (n/2^h+1)h
which boils down approx. 2n
hence time complexity o(n).
b. comparison happens children given parent level.
hope answered questions
Comments
Post a Comment