I’ve been learning functional programming for some time, but I haven’t read somewhere about sorting with the functional programming languages.
I know the sorting algorithms that are based on value exchanges are hard to implement with the functional idea, but I want to know that are there any sorting algorithms for use in functional programming? What are they?
Thank you.
In a functional language you write a function that given a list returns a sorted list, not touching (of course) the input.
Consider for example merge sorting… first you write a function that given two already sorted lists returns a single sorted list with the elements of both in it. For example:
then you can write a function that sorts a list by merging the resulting of sorting first and second half of the list.
About Python syntax:
L[0]is the first element of listLL[1:]is the list of all remaining elementsL[:n]is the list of up to the n-th element,L[n:]the restA + BifAandBare both lists is the list obtained by concatenation[x]is a list containing just the single elementxPS: Note that python code above is just to show the concept… in Python this is NOT a reasonable approach. I used Python because I think it’s the easiest to read if you know any other common imperative language.