Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(GHOP, Josip Dzolonga) |
(Haskell Example) |
||
Line 12: | Line 12: | ||
Writing the algorithm for integers will suffice. |
Writing the algorithm for integers will suffice. |
||
=={{header|Haskell}}== |
|||
insert x [] = [x] |
|||
insert x (y:ys) | x <= y = x:y:ys |
|||
insert x (y:ys) | otherwise = y:(insert x ys) |
|||
insertionSort :: Ord a => [a] -> [a] |
|||
insertionSort = foldr insert [] |
|||
-- Example use: |
|||
-- *Main> insertionSort [6,8,5,9,3,2,1,4,7] |
|||
-- [1,2,3,4,5,6,7,8,9] |
|||
=={{header|Java}}== |
=={{header|Java}}== |