Sorting a list
In this recipe, we will write a pseudo-quick sort using recursion. We call it pseudo-quick sort because it looks deceptively such as quick sort, but does not have a performance anywhere near it.
Getting ready
Use Stack to create a new project, pseudo-qsort, with the simple template and build it, after changing directory to the project folder:
> stack new pseudo-qsort simple > stack build
How to do it...
- Open
src/Main.hsand write theqsortimplementation. Theqsortinvolves the following:- Choosing an element of the list to be sorted
- Using the chosen element as a pivot, divide the input list into two parts:
- Subset of the list smaller than the pivot element
- Subset of the list greater than or equal to the pivot element
- Recursively sorting two parts in a similar way
- In Haskell, we can implement a method like quick sort quite easily.
Ord ain theqsortdeclaration signifies that the elements of the list[a]can be compared for inequality, and it is possible to use the...