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]
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....