4 kk = 15; % Insertion sort threshold, k >= 1
18 % Compute center index 19 mm = floor((ll + uu) / 2); 22 if ((mm + 1 - ll) <= kk) 23 % Sort x(ll:mm) via insertion sort 24 x = insertionsorti(x,ll,mm); 26 % Sort x(ll:mm) via insertion sort 27 x = mergesorti(x,ll,mm,kk); 30 % Sort x((mm + 1):uu) via insertion sort 31 x = insertionsorti(x,mm + 1,uu); 33 % Sort x((mm + 1):uu) via merge sort 34 x = mergesorti(x,mm + 1,uu,kk); 38 % Combine sorted arrays x(ll:mm) and x((mm + 1):uu) 39 x = merge(x,ll,mm,uu); 42 function x = insertionsorti(x,ll,uu) 48 while ((i > ll) && (x(i - 1) > pivot)) 50 indVec(i)=indVec(i-1); 59 function x = merge(x,ll,mm,uu) 61 % Note: In practice, use memcpy() or similar 63 indLVec=indVec(ll:mm); 65 % Combine sorted arrays 69 while ((k < j) && (j <= uu)) 82 % Note: In practice, use memcpy() or similar 83 x(k:(j - 1)) = L(i:(i + j - k - 1)); 84 indVec(k:(j - 1)) = indLVec(i:(i + j - k - 1)); function mergesort(in x)
Syntax: sx = mergesort(x);.
function mergesorti(in x, in ll, in uu, in kk)
Sort x(ll:uu) via merge sort Note: In practice, x xhould be passed by reference.