- def merge_sort(a, b)
- if a.length > 1
- temArr = split_array(a)
- a = merge_sort(temArr[0], temArr[1])
- if b.length > 1
- temArr = split_array(b)
- b = merge_sort(temArr[0], temArr[1])
- end
- end
- final = []
- until a.empty? or b.empty?
- final << (a.first < b.first ? a.shift : b.shift)
- end
- final + a + b
- end
- def split_array a
- tem = []
- tem << a.shift((a.length + 1)/2)
- tem << a
- end
- a=*0..20
- a.reverse!
- a.shuffle!
- p a
- s = split_array a
- p merge_sort s[0], s[1]
- #该片段来自于http://www.codesnippet.cn/detail/170520133433.html
来源: http://www.codesnippet.cn/detail/170520133433.html