Clarifying a recursive sorting algorithm
I have this Haskell question as homework, and the details are a little complicated. I'd just like to clarify what I am trying to do in this question:
Define the function select which takes a list of integers and returns the list whose head is the smallest element in the list and whose tail is the result of recursively sorting the list obtained by deleting the smallest element of the list from the list.
Does this mean something like "the first element of the list is the smallest, and the last element of the list is the int just bigger than the smallest and smaller t开发者_运维百科han everything else"?
For example if I have this List:
[ 2, 3, 4, 6, 8, 7]
,should the answer be
[2, 4, 6, 8, 7, 3]
or
[2, 4, 6, 7, 8, 3]
?
What you want is: Given a list [5 3 9 8 1], you want the following:
- A list where the head is the smallest (1), and the tail is the result of sorting the rest of the list ([5 3 9 8]).
- The tail of the original list has the head as the smallest (3), and the tail is the result of sorting [5 9 8]
- And so on.
It means that you should get the sorted list [2, 3, 4, 6, 7, 8]. The expected answer is not the list, but the implementation. The question tells you how the implementation should work, namely by extracting the smallest element, then calling itself for sorting the remainder, and finally glueing things together.
"the list whose head is the smallest element in the list"
Ok, that's easy
and whose tail is the result of recursively sorting the list obtained by deleting the smallest element of the list from the list.
So the result should be sorted: [2, 3, 4, 6, 7, 8]
.
(Your professor wants you to implement selection sort.)
It seems that the question is asking you to sort an array in ascending order (e.g. sort([5,4,3]) ==> [3,4,5]).
精彩评论