Skip to main content
Code Review

Return to Answer

replaced http://latex.codecogs.com/ with https://latex.codecogs.com/
Source Link
edited body
Source Link
MAG
  • 3k
  • 15
  • 30

You iterate over n/2 floorhttp://latex.codecogs.com/gif.latex?%5Cleft&space;%5Clfloor&space;n/2&space;%5Cright&space;%5Crfloor elements (no leaf elements). So only byKnowing this we can conclude that you know is at least O(n). The function called buildMaxHeap is actually O(h) worst case, h being the height of the node (which you call i) in the heap. So because the height of any heap is http://latex.codecogs.com/gif.latex?%5Cleft&space;%5Clfloor&space;lg&space;n&space;%5Cright&space;%5Crfloor

http://latex.codecogs.com/gif.latex?%5Csum_%7Bh=0%7D%5E%7B%5Cleft&space;%5Clfloor&space;lg&space;n&space;%5Cright&space;%5Crfloor%7D%5Cleft&space;%5Clceil&space;%5Cfrac%7Bn%7D%7Bn%5E%7Bh+1%7D%7D&space;%5Cright&space;%5Crceil&space;O%5Csum_%7Bh=0%7D%5E%7B%5Cleft&space;%5Clfloor&space;lg&space;(n)&space;%5Cright&space;%5Crfloor%7D%5Cleft&space;%5Clceil&space;%5Cfrac%7Bn%7D%7B2%5E%7Bh+1%7D%7D&space;%5Cright&space;%5Crceil&space;O(h)=&space;O&space;=&space;O(n%5Csum_%7Bh=0%7D%5E%7B%5Cleft&space;%5Clfloor&space;lg&space;n&space;%5Cright&space;%5Crfloor%7D%5Cfrac%7Bh%7D%7B2%5E%7Bh%7D%7Dn%5Csum_%7Bh=0%7D%5E%7B%5Cleft&space;%5Clfloor&space;lg&space;(n)&space;%5Cright&space;%5Crfloor%7D%5Cfrac%7Bh%7D%7B2%5E%7Bh%7D%7D)

buildMaxHeap doesn't actually builds the max heap so it shouldn't be named as it didname is not appropriate. This function is commonly called maxheapify and is to maintain the heap property. Also I would change the name of parameter named clone for something more generic, sincebecause in that context it doesn't matter if its a clone or not.

Because if both are equal why bother incrementing c. ?

This is not that important though I am just nitpicking.

Given the hidden constant factor in your function getMax (cloning the array and the hidden constant factor in building a max heap). Its its better doingto do it the straightforward way:

You iterate over n/2 floor elements (no leaf elements). So only by that you know is at least O(n). The function called buildMaxHeap is actually O(h) worst case, h being the height of the node (which you call i) in the heap. So because the height of any heap is http://latex.codecogs.com/gif.latex?%5Cleft&space;%5Clfloor&space;lg&space;n&space;%5Cright&space;%5Crfloor

http://latex.codecogs.com/gif.latex?%5Csum_%7Bh=0%7D%5E%7B%5Cleft&space;%5Clfloor&space;lg&space;n&space;%5Cright&space;%5Crfloor%7D%5Cleft&space;%5Clceil&space;%5Cfrac%7Bn%7D%7Bn%5E%7Bh+1%7D%7D&space;%5Cright&space;%5Crceil&space;O(h)=&space;O(n%5Csum_%7Bh=0%7D%5E%7B%5Cleft&space;%5Clfloor&space;lg&space;n&space;%5Cright&space;%5Crfloor%7D%5Cfrac%7Bh%7D%7B2%5E%7Bh%7D%7D)

buildMaxHeap doesn't actually builds the max heap so it shouldn't be named as it did. This function is commonly called maxheapify and is to maintain the heap property. Also I would change the name of parameter clone for something more generic, since in that context it doesn't matter if its a clone or not.

Because if both are equal why bother incrementing c. This is not that important though I am just nitpicking.

Given the hidden constant factor in your function getMax (cloning the array and the hidden constant factor in building a max heap). Its better doing it the straightforward way:

You iterate over http://latex.codecogs.com/gif.latex?%5Cleft&space;%5Clfloor&space;n/2&space;%5Cright&space;%5Crfloor elements (no leaf elements). Knowing this we can conclude that is at least O(n). The function called buildMaxHeap is actually O(h), h being the height of the node (which you call i). So because the height of any heap is http://latex.codecogs.com/gif.latex?%5Cleft&space;%5Clfloor&space;lg&space;n&space;%5Cright&space;%5Crfloor

http://latex.codecogs.com/gif.latex?%5Csum_%7Bh=0%7D%5E%7B%5Cleft&space;%5Clfloor&space;lg&space;(n)&space;%5Cright&space;%5Crfloor%7D%5Cleft&space;%5Clceil&space;%5Cfrac%7Bn%7D%7B2%5E%7Bh+1%7D%7D&space;%5Cright&space;%5Crceil&space;O(h)&space;=&space;O(n%5Csum_%7Bh=0%7D%5E%7B%5Cleft&space;%5Clfloor&space;lg&space;(n)&space;%5Cright&space;%5Crfloor%7D%5Cfrac%7Bh%7D%7B2%5E%7Bh%7D%7D)

buildMaxHeap doesn't actually builds the max heap so it name is not appropriate. This function is commonly called maxheapify and is to maintain the heap property. Also I would change the name of parameter named clone for something more generic, because in that context it doesn't matter if its a clone or not.

Because if both are equal why bother incrementing c ?

This is not that important though I am just nitpicking.

Given the hidden constant factor in your function getMax (cloning the array and the hidden constant factor in building a max heap) its better to do it the straightforward way:

edited body
Source Link
MAG
  • 3k
  • 15
  • 30
deleted 3 characters in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
deleted 7 characters in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
added 94 characters in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
added 12 characters in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
added 327 characters in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
added 327 characters in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
added 1 character in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
added 1 character in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
added 1 character in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
deleted 806 characters in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
added 792 characters in body
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
Source Link
MAG
  • 3k
  • 15
  • 30
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /