返回顶部

收藏

Ruby快速排序法

更多

[Ruby]代码

def partition(data, front, back)
  lastS1 = front
  pivot = data[front]
  frontUnknown = front + 1

  while frontUnknown <= last do
    if data[frontUnknown] < pivot
      backS1 += 1
      data.swap(frontUnknown, backS1)
    end

    frontUnknown += 1
  end

  data.swap(front, backS1)
  backS1
end

def quicksort(data, front = 0, back = data.size - 1)
  if front < back 
    pivotIndex = partition(data, front, back)
    quicksort(data, front, pivotIndex - 1)
    quicksort(data, pivotIndex + 1, back)
  end
end

标签:排序

收藏

0人收藏

支持

0

反对

0

发表评论