Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(Made sort generic so it works with any type containing an Ordering) |
|||
Line 1,797: | Line 1,797: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<lang scala>def |
<lang scala>def insertSort[X](list: List[X])(implicit ord: Ordering[X]) = { |
||
def insert(list: List[X], value: X) = list.span(x => ord.lt(x, value)) match { |
|||
case (lower, upper) => lower ::: value :: upper |
|||
} |
|||
} |
|||
def insertSort(list: List[Int]) = list.foldLeft(List[Int]())(insert)</lang> |
|||
list.foldLeft(List.empty[X])(insert) |
|||
}</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |