Anonymous user
Sorting algorithms/Insertion sort: Difference between revisions
Sorting algorithms/Insertion sort (view source)
Revision as of 23:43, 12 December 2016
, 7 years ago→{{header|F Sharp|F#}}
Line 1,079:
Functional Version
<lang fsharp>
// Inserts an element into its correct place in a sorted collection▼
let rec private sinsert element collection =▼
match element, collection with▼
| x, [] -> [x]▼
| x, y::ys when x < y -> x::y::ys▼
| x, y::ys -> y :: (ys |> sinsert x)▼
// Performs Insertion Sort▼
let insertionSort collection =
▲ // Inserts an element into its correct place in a sorted collection
▲ match element, collection with
▲ | x, [] -> [x]
▲ | x, y::ys when x < y -> x::y::ys
▲ | x, y::ys -> y :: (ys |> sinsert x)
▲ // Performs Insertion Sort
let rec isort acc collection =
match collection, acc with
|