6種排序的lua實現

2021-08-13 03:05:02 字數 4858 閱讀 1521

require"math"

-- straight insertion sort

a =

local b = function()

local string

for i = 1, 9 do

print(a[i])

endend

b()local length = table.getn(a)

print("a is length : ",length)

local c = function()

for i = 2, length do

if a[i] < a[i-1] then

local temp = a[i]

local j = i

while j > 1 and (a[j-1] > temp)  do

a[j] = a[j-1]

j = j - 1

enda[j] = temp

endend

endprint("straight insertion sort")

c()b()

a =

local d = function()

for i = 1,length - 1 do

for j = 1, length-i do

if a[j] > a[j+1] then

a[j], a[j+1] = a[j+1],a[j]

endend

endend

print("reset")

b()print("bubble sort")

d()b()

a =

local e = function()

for i = 1, length-1 do

local minlag = i

for j = i+1, length do

if a[j] < a[minlag] then

minlag = j

endend

if minlag > i then

local temp

temp = a[i]

a[i] = a[minlag]

a[minlag] = temp

endend

endlocal e2 = function()

for i = 1, (length+1)/2 do

local minlag = i

local maxlag = i

for j = i+1, length-i+1 do

if a[j] < a[minlag] then

minlag = j

endif a[j] > a[maxlag] then

maxlag = j

endend

if minlag > i then

local temp

temp = a[i]

a[i] = a[minlag]

a[minlag] = temp

endif maxlag > i then

local temp

temp = a[length-i+1]

a[length-i+1] = a[maxlag]

a[maxlag] = temp

endend

endprint("reset")

a =

b()print("****** selection sort")

e()b()

print("reset")

a =

b()print("****** selection sort two")

e2()

b()print("reset")

a =

b()print("quick sort")

-- quick sort 比我小的站前面,比我大的站後面

-- 遞迴的想法,把問題複雜問題簡單的抽象成乙個

--function partition is ****** pattern

local partition = function(as,low,high)

local _z = as[low]

while low < high do

while low < high and as[high] > _z do

high = high - 1

endas[low], as[high] = as[high], as[low]

while low < high and as[low] < _z do

low = low + 1

endas[low], as[high] = as[high], as[low]

return low

endend

quicksort = function(as, low, high)

if low < high then

local zhong = partition(as, low, high)

print("zhong", zhong)

print("low, left", low, zhong - 1 )

quicksort(as, low, zhong - 1) -- 1 2

print("right, high", zhong + 1,high)

quicksort(as, zhong + 1, high) -- 4 9

endend

quicksort(a,1,9)

b()print("reset")

a =

b()local shellsort = function(a, length)

local zl = math.floor(length/2)

while zl > 0  do

-- print("zl",zl)

for j = zl+1,zl*2,1 do

-- print("j",j)

for i = j,length,zl do

-- print("i",i)

if a[i] < a[i-zl] then

local temp = a[i]

while (i-zl) > 0 and temp < a[i-zl] do

a[i] = a[i-zl]

i = i - zl

enda[i] = temp

endend

endzl = math.floor(zl/2)

endend

print("shell sort")

shellsort(a,9)

b()print("reset")

a =

b()-- 調整heap 簡單原型

heapadjust = function(a,i,size)

print("math.ceil(size/2)", math.ceil(size/2))

if i <= math.floor(size/2) then

local max = i

local lchild,rchild = 2*i, 2*i+1

print("lchild,rchild", lchild, rchild)

if lchild <= size and a[lchild] > a[max] then

max = lchild

endif rchild <= size and a[rchild] > a[max] then

max = rchild

endprint("max", max)

if max ~= i then

a[max],a[i] = a[i],a[max]

heapadjust(a,max,size)

endelse

print("遞迴結束")

endend

local buildheap = function(a,size)

for i = math.floor(size/2),1,-1 do

print("i", i)

heapadjust(a,i,size)

endend

local heapsort = function(a,size)

print("buildheap action---------")

buildheap(a,size)

print("buildheap end-----------")

for i = size, 1, -1 do

a[1], a[i] = a[i], a[1]

heapadjust(a, 1, i-1)

endend

print("heapsort")

heapsort(a,9)

b()b =

local binarysearch = function(a,value) --預設index is 1 begin  找到對應value值在table 中的下標

if not a or #a ==0 then

return nil

endlocal low, high = 1, #a

while low <= high do

print("low, high",low, high)

local m = math.floor((low+high)/2)

if value > a[m] then

low = m + 1

elseif value < a[m] then

high = m - 1

else

return m

endend

endlocal index = binarysearch(b,6)

print("index is :",index)

java實現6種排序演算法

典型的二分查詢 對於二分查詢演算法要求,查詢前的資料必須是已經排好序的,然後得到陣列的開始位置start和結束位置end,取中間位置mid的資料a mid 跟待查詢資料key進行比較,若 a mid key,則取end mid 1 若 a mid key,則取start mid 1 若 a mid ...

6種內部排序演算法 Java實現

一 直接插入排序 待排序陣列 a 0.n 1 排序思路 首先我們認為a 0 是已經排好序的,在排序過程中,依次將a i i 1,2,n 1 從後往前插入到前面已經 排 好序的子陣列a 0.i 1 中的適當位置中,待所有的a i 插入完畢,即排序完畢。演算法如下 空間上,該演算法僅需要常數個輔助單元,...

c 類實現基本的6種排序演算法

選擇排序 快速排序 希爾排序 堆排序是不穩定的排序演算法 氣泡排序 插入排序 歸併排序和基數排序是穩定的排序演算法 includeusing namespace std class sort sort void print int arr,int size const void quicksort ...