Lua 編寫快速排序演算法
阿新 • • 發佈:2019-02-20
--快速排序 function quickSort(array,low,high) if low < high then local key = array[low] local lowTag = low local highTag = high while(lowTag<highTag) do while lowTag<highTag and key<array[highTag] do highTag = highTag - 1 end array[lowTag] = array[highTag] while lowTag<highTag and key>=array[lowTag] do lowTag = lowTag + 1 end array[highTag] = array[lowTag] end array[lowTag] = key Utils.quickSort(array,low,lowTag-1) Utils.quickSort(array,low+1,high) end end
--測試用例
local array = {2,4,9,3,6,3,1,5}