Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -0,0 +1,55 @@
|
|||
fn mergesort arr =
|
||||
(
|
||||
local left = #()
|
||||
local right = #()
|
||||
local result = #()
|
||||
if arr.count < 2 then return arr
|
||||
else
|
||||
(
|
||||
local mid = arr.count/2
|
||||
for i = 1 to mid do
|
||||
(
|
||||
append left arr[i]
|
||||
)
|
||||
for i = (mid+1) to arr.count do
|
||||
(
|
||||
append right arr[i]
|
||||
)
|
||||
left = mergesort left
|
||||
right = mergesort right
|
||||
if left[left.count] <= right[1] do
|
||||
(
|
||||
join left right
|
||||
return left
|
||||
)
|
||||
result = _merge left right
|
||||
return result
|
||||
)
|
||||
)
|
||||
|
||||
fn _merge a b =
|
||||
(
|
||||
local result = #()
|
||||
while a.count > 0 and b.count > 0 do
|
||||
(
|
||||
if a[1] <= b[1] then
|
||||
(
|
||||
append result a[1]
|
||||
a = for i in 2 to a.count collect a[i]
|
||||
)
|
||||
else
|
||||
(
|
||||
append result b[1]
|
||||
b = for i in 2 to b.count collect b[i]
|
||||
)
|
||||
)
|
||||
if a.count > 0 do
|
||||
(
|
||||
join result a
|
||||
)
|
||||
if b.count > 0 do
|
||||
(
|
||||
join result b
|
||||
)
|
||||
return result
|
||||
)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
a = for i in 1 to 15 collect random -5 20
|
||||
#(-3, 13, 2, -2, 13, 9, 17, 7, 16, 19, 0, 0, 20, 18, 1)
|
||||
mergeSort a
|
||||
#(-3, -2, 0, 0, 1, 2, 7, 9, 13, 13, 16, 17, 18, 19, 20)
|
||||
Loading…
Add table
Add a link
Reference in a new issue