Skip to content

Instantly share code, notes, and snippets.

@lysu
lysu / sort.rb
Last active December 15, 2015 18:59
buddle_sort
def bubble_sort(list)
(list.length() - 1).downto(0).each { |bubble_to_index|
(0..(bubble_to_index - 1)).each { |bubbling_index|
if list[bubbling_index] > list[bubbling_index + 1]
tmp = list[bubbling_index]
list[bubbling_index] = list[bubbling_index + 1]
list[bubbling_index + 1] = tmp
end
}
}
@lysu
lysu / sort.rb
Last active December 15, 2015 18:59
quick sort
def partition(list, low, high)
i = low
j = high + 1
base_num = list[low]
while true
i += 1
while list[i] < base_num
if i == high
break
@lysu
lysu / sort.rb
Last active December 15, 2015 19:08
heap sort
def left_child(parent)
2 * parent + 1
end
def find_max_child_index(child, length, list)
if child + 1 < length && list[child] < list[child + 1]
child += 1
end
child
end
@lysu
lysu / sort.rb
Last active December 15, 2015 19:09
selection_sort
def selection_sort(list)
list.each_index do | index |
min_index = index
((index + 1)...list.length).each do | find_index |
if list[find_index] < list[min_index]
min_index = find_index
end
end
temp = list[index]
list[index] = list[min_index]
@lysu
lysu / sort.rb
Created April 4, 2013 10:09
shell sort
def get_increase(len)
h = 1
while h < len / 3
h = h * 3 + 1
end
h
end
def shell_sort(list)
inc = get_increase(list.length)
@lysu
lysu / sort.rb
Created April 4, 2013 12:30
merge sort
def merge(list, low, mid, high)
aux = []
low.upto(high) do |copier_index|
aux[copier_index] = list[copier_index]
end
i = low
j = mid + 1
low.upto(high) do | index |
@lysu
lysu / Trie.coffee
Created May 3, 2013 09:11
trie.coffee
class Trie
constructor: ->
@root = null
push: (key, value) ->
return null if key.empty
@root = @push_recursive(@root, key, 0, value)
value
get: (key) ->
@lysu
lysu / SuffixArray.coffee
Created May 3, 2013 12:00
suffixArray.coffee
class SuffixArray
constructor: (@original_string) ->
@suffixes = []
@suffixes.push @original_string[index..-1] for index in [0..@original_string.length - 1]
@suffixes.sort @compare
compare: (str1, str2) ->
if str1 > str2 then 1 else if str1 < str2 then -1 else 0
@lysu
lysu / enumHandler.java
Created September 10, 2013 07:27
enum handler
public interface EnumTrait {
Object getValue();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface EnumTypeHandler {
Class enumClass();
int jdbcType();
}
@lysu
lysu / introrx.md
Last active August 29, 2015 14:06 — forked from staltz/introrx.md

The introduction to Reactive Programming you've been missing

(by @andrestaltz)

So you're curious in learning this new thing called (Functional) Reactive Programming (FRP).

Learning it is hard, even harder by the lack of good material. When I started, I tried looking for tutorials. I found only a handful of practical guides, but they just scratched the surface and never tackled the challenge of building the whole architecture around it. Library documentations often don't help when you're trying to understand some function. I mean, honestly, look at this:

Rx.Observable.prototype.flatMapLatest(selector, [thisArg])

Projects each element of an observable sequence into a new sequence of observable sequences by incorporating the element's index and then transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.