This function performing insertion sort on a list takes as arguments a compariso
ID: 3666790 • Letter: T
Question
This function performing insertion sort on a list takes as arguments a comparison function less and a list l of elements to be sorted. The code compiles and runs correctly:
fun sort(less, nil) = nil |
sort(less, a : : l) =
let
fun insert(a, nil) = a : : nil |
insert(a, b : : l) = if less(a,b) then a : : (b: : l)
else b : : insert(a, l)
in
insert(a, sort(less, l))
end;
What is the type of this sort function? Explain briefly, including the type of the subsidiary function insert. You do not have to run the ML algorithm on this code; just explain why an ordinary ML programmer would expect the code to have this type.
Explanation / Answer
Based on above Question
Insertion Sort algorithm written in standard ML.
Standard ML offers both mutable and immutable data-structures
So we will show an imperative implementation to sort an array in-place and a functional implementation to sort a list.
Type of insertion sort comes under
We will sort the data of type 'a with a comparator function cmp : 'a * 'a -> order.
Insert each element Insertion Sort
To insert each element, we need to create a hole in the array at the place where the element belongs, then place the element in that hole.
We can combine the creation of the hole with the searching for the place by starting at the end and shifting each element up by one until we find the place where the element belongs.
The type constraint overloading the comparison operasiontor. All the sorting functions have a type constraint.
The function requires deep recursion.
The execution of the sort is order n2(n square).
Iteration
Sort(4.0,6.0)
Insertion element
[4.0,6.0] : a list
Insertion element
Sort(8.0,l)
Insertion element
[4.0,6.0,8.0]: a list
Insertion element
Sort(5.0,l)
[4.0,5.0,6.0,8.0]Based on above Question
Insertion Sort algorithm written in standard ML.
Standard ML offers both mutable and immutable data-structures
So we will show an imperative implementation to sort an array in-place and a functional implementation to sort a list.
Type of insertion sort comes under
We will sort the data of type 'a with a comparator function cmp : 'a * 'a -> order.
Insert each element Insertion Sort
To insert each element, we need to create a hole in the array at the place where the element belongs, then place the element in that hole.
We can combine the creation of the hole with the searching for the place by starting at the end and shifting each element up by one until we find the place where the element belongs.
The type constraint overloading the comparison operasiontor. All the sorting functions have a type constraint.
The function requires deep recursion.
The execution of the sort is order n2(n square).
Iteration
Sort(4.0,6.0)
Insertion element
[4.0,6.0] : a list
Insertion element
Sort(8.0,l)
Insertion element
[4.0,6.0,8.0]: a list
Insertion element
Sort(5.0,l)
[4.0,5.0,6.0,8.0]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.