Skip to content

Instantly share code, notes, and snippets.

@chtrinh
chtrinh / heap_sort.js
Created February 24, 2016 23:00
Heapsort
// Assume you sorting an array
// [5,1,2,3] => [1,2,3,5]
// [2,3,5,5,2,0] => [0,2,2,3,5,5]
function _swap(array, index1, index2) {
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
function _buildHeap(array) {
@chtrinh
chtrinh / bucket_sort.js
Created February 24, 2016 11:11
BucketSort - generalized form of a counting sort.
// Assume you are sorting floating point values.
// [0.33, 0.22, 0.4, 0.1, 0.0] => [0.0, 0.1, 0.22, 0.33, 0.4]
function _hash(value) {
return Math.floor(value / 0.1); // create buckets with sized 0.1
}
function _createBucket(array) {
var buckets = {}; // A hash that has keys that represent a range of values.
for (var i = 0; i < array.length; i++) {
@chtrinh
chtrinh / insertion_sort.js
Last active February 24, 2016 10:17
Insertion sort - with recursion implementation.
// Sort an array of integers.
// [1,4,1,9,5,10] => [1,1,4,5,9,10]
function _insert(array, position, value) {
var i = position - 1; // Check the previous value
// Walk backwards in the array and shifts the values of the array
// when the previous value is larger than the value we want to insert
while ((i >= 0) && (array[i] > value)) {
array[i + 1] = array[i];
i--;
@chtrinh
chtrinh / counting_sort.js
Created February 24, 2016 07:17
Counting sort algorithm with annotated source code. Sorting numeric values using an array as bucket and alphabetic values using an object as a bucket
// Assume you sorting an array within the range of 0-9
// [5,1,2,3] => [1,2,3,5]
// [2,3,5,5,2,0] => [0,2,2,3,5,5]
function countingSort(array) {
// We can assume a range of values or we can determine the range ourselves.
// [0, k) where k is the max value of the range.
// k must be less than or equal to N
// Each value in the range is a represented a key in a bucket
var bucket = [], // We use the array's index as the key for the bucket
index = 0;
map = {};
_ref = form.serializeArray();
_ref1 = this.model.parameters;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
o = _ref[_i];
if ((o.value != null) && jQuery.trim(o.value).length > 0) {
if (_ref1[_i].allowMultiple) {
map[o.name] = o.value.split(",");
@chtrinh
chtrinh / development.rb
Created December 7, 2011 18:08 — forked from MyArtChannel/development.rb
Use Pry as IRB replacement in rails 3 console
# Add this to the end of your development.rb and add
#
# gem 'pry'
#
# to your Gemfile and run bundle to install.
silence_warnings do
begin
require 'pry'
IRB = Pry
/**
* What this does:
* converts current.docx to "current pdf"
* Adds a timestamp on the footer of each page of "current pdf"
* Appends the "current pdf" to comprehensive pdf
* Zip the "current pdf" to archive.zip
* Deletes the "current pdf"
*
* Usage example: java ELabnotes archive.zip comprehensive.pdf current.docx
*