Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- função que retorna "true" se o elemento A for logicamente menor que B
- local comp = function(a, b)
- return a <= b
- end
- function quicksort(arr, left, right)
- function partition(arr, left, right, index)
- local pivot = arr[index], _index = left
- arr[index], arr[right] = arr[right], arr[index]
- for i = left, right - 1 do
- if comp(arr[i], pivot) then
- arr[i], arr[_index] = arr[_index], arr[i]
- _index = _index + 1
- end
- end
- arr[_index], arr[right] = arr[right], arr[_index]
- return _index
- end
- if right > left then
- local new_index = partition(arr, left, right, (left + right) / 2)
- quicksort(arr, left, new_index - 1)
- quicksort(arr, new_index + 1, right)
- end
- end
- function quicksort(arr)
- quicksort(arr, 1, #arr)
- end
- array = { 1, 5, 2, 17, 11, 3, 1, 22, 2, 37 }
- quicksort(array)
- for _, v in pairs(array) do
- print(v)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement