// //------------------ DOK FILE -------------------------------------------------- +Header core.util.insertsort =pkg core.util.insertsort =title core.util.insertsort =short the insert sort classical algorithm =desc The insert sort algorithm is known to be quite slow, with an O(n²) time complexity. However when the input list is almost ordered, it proves to be faster than [[quicksort]]. // // //------------------ PROTO ----------------------------------------------------- insertsort +Proto insertsort =type fun list a1 (fun a1 a1 -> Bool) -> list a1 =mode function =pkg core.util.insertsort =impl mcy =link =desc This function applies the insertsort algorithm to the list src, using the lambda function fBefore to compare elements. When the input list is almost ordered, it proves to be faster than [[quicksort]]. In order to achieve this result, the implementation of insertsort first reverses the order of the input list. > insertsort (3::2::5::4::nil, lambda (a, b)=a-> list Int: > Int: 2 (0x2) > Int: 3 (0x3) > Int: 4 (0x4) > Int: 5 (0x5) // +arg src =type list a1 =desc A list // +arg fBefore =type fun a1 a1 -> Bool =desc A lambda function which returns true if the first element should be before the second // +arg result =type list a1 =desc The ordered list