You should choose a data structure depending on what operations you want. In this post, I would like to introduce a heap data structure. For operations such as insert, find or delete min, heap is the first option that you should consider. The following table shows that how efficient the heap is compare to other data structures.

  insert search find min delete min
unsorted array O(1) O(n) O(n) O(n)
sorted array O(n) O(logn) O(1) O(1)
unsorted linked list O(1) O(n) O(n) O(n)
min heap O(logn) O(n) O(1) O(logn)

Heap is an almost complete binary tree which means that if all leaves present in one level then they should be filled from left to right. So the previous level should be completely filled first before going to the next level.

Side Note:
Given n elements, if the list is already sorted either a max or min heap, you should not sort it to make a heap. The reason is that it takes O(n * logn). If your purpose is just to construct a heap, there is a algorithm which takes only O(n).

Height & Nodes

In a complete binary tree, the relation between the height and the number of nodes is following:

H 1 2 3 4
Max 3 7 15 31

Note that H represents the height of a tree and Max represents the number of maximum nodes relative to the height of the tree.

Therefore, Max = (2^(h+1) - 1) and H = floor(log n).

Implementation

The index of leaves starts from floor(n/2) + 1 to n and you can say that every leaf is a heap.

def max_heapify(array, i)
  l = (2*i)
  r = (2*i)+1

  # we should subtract since given array includes 'nil' at position 0
  heap_size = array.size - 1

  # l <= heap_size condition ensures that the left child exists
  if l <= heap_size && array[l] > array[i]
    largest = l
  else
    largest = i
  end

  # r <= heap_size condition ensures that the right child exists
  if r <= heap_size && array[r] > array[largest]
    largest = r
  end

  # if root is the largest then no need to exchange
  if largest != i
    array[i], array[largest] = array[largest], array[i]
    max_heapify(array, largest)
  end
end

The time and space complexity is both O(log N).

def build_max_heap(array)
  # array.size / 2 => last index of non-leaf
  # leaf starts at the index of floor(array.size / 2) + 1
  (array.size/2..1).each do |i|
    max_heapify(array, i)
  end
end

The time complexity is O(n) and the space complexity is O(log n).

array = [nil, 5, 10, 1, 20, 3, 11, 100]
p build_max_heap(array) # => [nil, 100, 20, 11, 10, 3, 5, 1]
def extract_max(array)
  # base case
  if array.size <= 1
    return
  end

  # last element becomes the root then
  # max heapify with array which the last element is already removed
  max = array[1]
  A[1] = array[-1]
  max_heapify(array[0..array.size-1], 1) # => takes O(log n)

  return max
end

In extract_max function, it takes constant time until max_heapify is called. We have already seen that max_heapify takes the O(h) which is the height of the tree and therefore O(log n). The space complexity is also O(log n).

def increase_key(array, i, key)
  if key < array[i]
    return
  end
  
  array[i] = key

  # i > 1 condition ensures not to go beyond the root
  # if the parent is less than the child then exchange
  while i > 1 && array[i/2] < array[i]
    array[i], array[i/2] = array[i/2], array[i]
    i = i/2
  end
end

The main operation occurs in while loop. The worst case happens when it starts from the deepest leaf and to the root. Then, it takes O(log n). The space complexity requires O(1) since it does not have a recursion.

Conclusion

Heap is optimized for the operations below:

  Find max Delete max Insert key Increase key Decrease key
Max Heap O(1) O(log n) O(log n) O(log n) O(log n)

We should consider a better data structures if these operations are required below:

  Find min Search Delete
Max Heap O(n) O(n) O(n)

References

Sample Post
Image source: dargadgetz

Below is just about everything you’ll need to style in the theme. Check the source code to see the many embedded elements within paragraphs.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Body text

Lorem ipsum dolor sit amet, test link adipiscing elit. This is strong. Nullam dignissim convallis est. Quisque aliquam.

Smithsonian Image

This is emphasized. Donec faucibus. Nunc iaculis suscipit dui. 53 = 125. Water is H2O. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. The New York Times (That’s a citation). Underline. Maecenas ornare tortor. Donec sed tellus eget sapien fringilla nonummy. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus.

HTML and CSS are our tools. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus.

If you want to show just a part of your post in the come page, add the <!-- more --> tag to your post content. Everything after this tag will be hidden from the page listing page.

Blockquotes

Lorem ipsum dolor sit amet, test link adipiscing elit. Nullam dignissim convallis est. Quisque aliquam.

List Types

Ordered Lists

  1. Item one
    1. sub item one
    2. sub item two
    3. sub item three
  2. Item two

Unordered Lists

  • Item one
  • Item two
  • Item three

Tables

Header1 Header2 Header3
cell1 cell2 cell3
cell4 cell5 cell6
cell1 cell2 cell3
cell4 cell5 cell6
Foot1 Foot2 Foot3

Code Snippets

Syntax highlighting via Pygments

#container {
  float: left;
  margin: 0 -240px 0 0;
  width: 100%;
}

Non Pygments code example

<div id="awesome">
    <p>This is great isn't it?</p>
</div>

Buttons

Make any link standout more when applying the .btn class.

<a href="#" class="btn btn-success">Success Button</a>
Primary Button
Success Button
Warning Button
Danger Button
Info Button

Post Featured Image

To show an image banner at top of your post, use add the following code:

image:
    feature: <name of the image>
    credit: <source of the image - optional>
    creditlink: <source of the image (url) - optional>

The image must be in the /images folder.

Syntax highlighting is a feature that displays source code, in different colors and fonts according to the category of terms. This feature facilitates writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct. Highlighting does not affect the meaning of the text itself; it is intended only for human readers.1

Rouge Code Blocks

To modify styling and highlight colors edit /_sass/_rouge.scss.

#container {
    float: left;
    margin: 0 -240px 0 0;
    width: 100%;
}
<nav class="pagination" role="navigation">
    {% if page.previous %}
        <a href="{{ site.url }}{{ page.previous.url }}" class="btn" title="{{ page.previous.title }}">Previous article</a>
    {% endif %}
    {% if page.next %}
        <a href="{{ site.url }}{{ page.next.url }}" class="btn" title="{{ page.next.title }}">Next article</a>
    {% endif %}
</nav><!-- /.pagination -->
module Jekyll
  class TagIndex < Page
    def initialize(site, base, dir, tag)
      @site = site
      @base = base
      @dir = dir
      @name = 'index.html'
      self.process(@name)
      self.read_yaml(File.join(base, '_layouts'), 'tag_index.html')
      self.data['tag'] = tag
      tag_title_prefix = site.config['tag_title_prefix'] || 'Tagged: '
      tag_title_suffix = site.config['tag_title_suffix'] || '&#8211;'
      self.data['title'] = "#{tag_title_prefix}#{tag}"
      self.data['description'] = "An archive of posts tagged #{tag}."
    end
  end
end

Standard Code Block

<nav class="pagination" role="navigation">
    {% if page.previous %}
        <a href="{{ site.url }}{{ page.previous.url }}" class="btn" title="{{ page.previous.title }}">Previous article</a>
    {% endif %}
    {% if page.next %}
        <a href="{{ site.url }}{{ page.next.url }}" class="btn" title="{{ page.next.title }}">Next article</a>
    {% endif %}
</nav><!-- /.pagination -->

This is a sample post to show the dark post theme. To use this dark theme, just add:

    layout: dark-post

on your post. This theme adds a dark background with a white panel to show your content.

Here be a sample post with a custom background image. To utilize this “feature” just add the following YAML to a post’s front matter.

image:
  background: filename.png

This little bit of YAML makes the assumption that your background image asset is in the /images folder. If you place it somewhere else or are hotlinking from the web, just include the full http(s):// URL. Either way you should have a background image that is tiled.

If you want to set a background image for the entire site just add background: filename.png to your _config.yml and BOOM — background images on every page!

Background images from Subtle Patterns (Subtle Patterns) / CC BY-SA 3.0