operator<
.It produces a new list that is a stable sorted copy of the original list, leaving the original unmodified.
I learned, in testing, that
Cut
was the largest detriment to speed complexity.Anyone know of a faster or more optimal sort proc?
proc/Sort(list/L) // requires operator<, stable
if (L.len <= 1)
return L.Copy()
if (L.len == 2)
var/list/result = L.Copy()
if (L[2] < L[1])
result.Swap(1, 2)
return result
var/pivot = 1 + round(L.len / 2)
var/list/L1 = .(L.Copy(1, pivot))
var/list/L2 = .(L.Copy(pivot, L.len + 1))
// merge
var/list/result = new
var/i1 = 1
var/i2 = 1
while (i1 <= L1.len && i2 <= L2.len)
if (L2[i2] < L1[i1])
result += L2[i2++]
else
result += L1[i1++]
if (i1 <= L1.len)
result += L1.Copy(i1)
else if (i2 <= L2.len)
result += L2.Copy(i2)
return result
Edit: Would need modification to handle associative lists.