I’m currently in the processes of learning functional languages such as Lisp and DrScheme, but I’ve been asked to write a variance of the Quicksort algorithm in DrScheme. However, I’m a bit clueless as where to start.
Could someone give me some pointers as which functions and datatypes to use? I obviously know lists, car, cdr, and append are going to play a huge part into what to do.
By, the way, I’m only really looking for general idea to launch off from. I don’t necessarily want the full answer. Kind of ruins the adventure and fun of it!
Quicksort is one of the simplest sorting algorithms to be implemented in a functional style. Use this pseudocode as an starting point for sorting in ascending order a list of numbers, noticing that the only data structure that you need is a standard Lisp list:
The “tricky” part, getting all the elements less than (or all the elements greater than or equal to) the first element in the list, can be easily expressed in terms of the
filterprocedure. If you’re not allowed to use it, it’s easy enough to implement its basic functionality from scratch.Also notice that the
+operators in my pseudocode denote anappendof three lists: the list of elements less than the first element in the list, the singleton list with the first element in the list (the pivot) and the list of elements greater than or equal to the first element in the list.In a real-world implementation of Quicksort, much more care would be taken in picking an appropriate pivot element, but for this simple example is enough to take the first element.