%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Lazy mergesort in Oz % Author: Peter Van Roy (upon suggestion by Miller Puckette) % Date: Aug. 27, 2008 % This sort routine has the same good incremental property as % lazy quicksort, and in addition is guaranteed logarithmic. % Getting the first element: Split the list and get the first % element of the Merge. This gets the first elements of the % two halves of the split, and so forth recursively. O(n) work % to get the first element. % Suggested by Miller Puckette at ICMC 2008 after I explained % to him how the lazy quicksort works. declare proc {Split Xs Ys Zs} case Xs of nil then Ys=nil Zs=nil [] [X] then Ys=[X] Zs=nil [] X1|X2|Xr then Yr Zr in Ys=X1|Yr Zs=X2|Zr {Split Xr Yr Zr} end end fun lazy {Merge Xs Ys} case Xs#Ys of nil#Ys then Ys [] Xs#nil then Xs [] (X|Xr) # (Y|Yr) then if X