Last active
February 26, 2022 22:46
-
-
Save echohes/9aabdc36d416a16f7e44b3670de05419 to your computer and use it in GitHub Desktop.
Sort Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local a={} | |
for i=1,5000 do | |
a[i]=math.random(0, 650000) | |
end | |
local function bs(t) | |
for k,v in pairs(t) do | |
for kk,vv in pairs(t) do | |
if k~=kk then | |
if vv>v then | |
t[k],t[kk]=t[kk],t[k] | |
end | |
end | |
end | |
end | |
return t | |
end | |
a=bs(a) | |
for k,v in pairs(a) do | |
print(k,v) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local a={} | |
for i=1,1000 do | |
a[i]=math.random(0, 650000) | |
i=i+1 | |
end | |
local function bs(t) | |
local b={} | |
local i=1 | |
local p1=0 | |
local sort_affect = false | |
for k,v in pairs(t) do | |
local ins=false | |
if k > p1 then | |
for kk,vv in pairs(t) do | |
if k~=kk and kk>p1 then | |
if v<=vv then | |
b[i]=v | |
i=i+1 | |
if k>p1 then | |
p1=k | |
end | |
ins=true | |
break | |
else | |
b[i]=vv | |
i=i+1 | |
p1=kk | |
sort_affect = true | |
end | |
end | |
end | |
if not ins then | |
b[i]=v | |
i=i+1 | |
end | |
end | |
end | |
if not sort_affect then | |
return b | |
else | |
return bs(b) | |
end | |
end | |
a=bs(a) | |
for k,v in pairs(a) do | |
print(k,v) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment