首页 > 代码库 > Ruby一行代码实现快速排序

Ruby一行代码实现快速排序

 1 def quick_sort(a)
 2 
 3     return a if a.size < 2
 4 
 5     (x = a.pop) ?  quick_sort(a.select{|i| i <=x }) + [x] + quick_sort(a.select{|i| i > x}) : []
 6 
 7 end
 8 
 9 array = [72,6,57,88,60,42,83,73,42,48,85] 
10 
11 p quick_sort(array)    #=> [6, 42, 42, 48, 57, 60, 72, 73, 83, 85, 88]