Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <div id="mocha"></div>
            
          
!

CSS

            
              
            
          
!

JS

            
              //LeetCode #912
//Heap sort: O(n log(n)), space O(1)
//Visualization http://www.algomation.com/player?algorithm=58b44c8f0e406f04000c713d
class MinHeap {
  constructor() {
    this.heap = [null];
  }
  
  peak() {
    return this.heap[1];
  }
  
  isEmpty() {
    return this.heap.length <= 1;
  }
  
  getParentIndex(index) {
    return Math.floor(index / 2);
  }
  
  bubbleUp(index) {
    const heap = this.heap;
    let parentIndex = this.getParentIndex(index);
    while (parentIndex >= 1 && heap[parentIndex] > heap[index]) {
      [heap[parentIndex], heap[index]] = [heap[index], heap[parentIndex]];
      index = parentIndex;
      parentIndex = this.getParentIndex(index);
    }
  }
  
  bubbleDown(index) {
    const heap = this.heap;
    let leftIndex = index * 2;
    let rightIndex = index * 2 + 1;
    while (
      heap[leftIndex] !== undefined && heap[index] > heap[leftIndex] ||
      heap[rightIndex] !== undefined && heap[index] > heap[rightIndex]
    ) {
      if (heap[rightIndex] !== undefined && heap[rightIndex] < heap[leftIndex]) {
        [heap[rightIndex], heap[index]] = [heap[index], heap[rightIndex]];
        index = rightIndex;
      } else {
        [heap[leftIndex], heap[index]] = [heap[index], heap[leftIndex]];
        index = leftIndex;
      }
      leftIndex = index * 2;
      rightIndex = index * 2 + 1;
    }
  }
  
  insert(num) {
    this.heap.push(num);
    this.bubbleUp(this.heap.length - 1)
  }
  
  inserts(nums) {
    for (let num of nums) {
      this.insert(num);
    }
  }
  
  remove(num) {
    const heap = this.heap;
    const index = num === undefined ? 1 : heap.indexOf(num);
    const last = heap.pop();
    if (index === heap.length) {
      return last;
    }
    const removed = heap[index];
    heap[index] = last;
    this.bubbleUp(index);
    this.bubbleDown(index);
    return removed;
  }
}

function heapSort(nums) {
  const minHeap = new MinHeap();
  minHeap.inserts(nums);
  const res = [];
  while (!minHeap.isEmpty()) {
    res.push(minHeap.remove());
  }
  return res;
}

const sortArray = (nums) => {
  return heapSort([...nums]);
}

mocha.setup('bdd');
describe('MinHeap insert', function () {
  it('should return empty', function () {
    chai.expect((new MinHeap()).heap).to.eql([null]);
  });
  it('should return by insert order', function () {
    const minHeap = new MinHeap();
    minHeap.insert(1)
    chai.expect(minHeap.heap).to.eql([null, 1]);
    minHeap.insert(2)
    chai.expect(minHeap.heap).to.eql([null, 1, 2]);
    minHeap.insert(3)
    chai.expect(minHeap.heap).to.eql([null, 1, 2, 3]);
  });
  it('should return by heap order', function () {
    const minHeap = new MinHeap();
    minHeap.inserts([3, 2, 1])
    chai.expect(minHeap.heap).to.eql([null, 1, 3, 2]);
  });
});
describe('MinHeap remove', function () {
  it('should return empty when remove all', function () {
    const minHeap = new MinHeap();
    minHeap.insert(1)
    minHeap.remove()
    chai.expect(minHeap.heap).to.eql([null]);
  });
  it('should remove top', function () {
    const minHeap = new MinHeap();
    minHeap.inserts([1, 2, 3, 4, 5])
    chai.expect(minHeap.heap).to.eql([null, 1, 2, 3, 4, 5]);
    minHeap.remove();
    minHeap.insert(6);
    chai.expect(minHeap.heap).to.eql([null, 2, 4, 3, 5, 6]);
    minHeap.remove();
    minHeap.insert(7);
    chai.expect(minHeap.heap).to.eql([null, 3, 4, 6, 5, 7]);
  });
});



describe('sort empty', function () {
  it('should return empty', function () {
    chai.expect(sortArray([])).to.eql([]);
  });
  it('should return one element', function () {
    chai.expect(sortArray([1])).to.eql([1]);
  });
});

describe('sort numbers', function () {
  it('should sort by asc', function () {
    chai.expect(sortArray([5,2,3,1])).to.eql([1,2,3,5]);
  });
  it('should able to sort same elements', function () {
    chai.expect(sortArray([5,1,1,2,0,0])).to.eql([0,0,1,1,2,5]);
  });
  it('should able to sort negative numbers', function () {
    chai.expect(sortArray([5,1,1,2,0,0,-1])).to.eql([-1,0,0,1,1,2,5]);
  });
});
mocha.run();
            
          
!
999px

Console