Problem 4 (Quicksort)
For this problem, you must implement the recursive quicksort algorithm covered in the lecture to sort points based on their distance from the point (0, 0). Create a Python file called quicksort.py and implement a function called quicksort(list) that does the following:
1. Takes a 2D list argument that contains information representing x/y points in 2D space. Each element in the list will be a list with 2 items – an x and a y coordinate. For example, the list could be [[0, 1], [2, 1], [3, 3], [1, 1], ...].
2. Performs an in-place sort (i.e., does not create a new list, but modifies the original) using the quicksort algorithm. This must sort the 2D list such that points with lower Euclidean distance to the origin (0, 0) appear earlier in the list. In this case, you are comparing distances instead of directly comparing list values – it may be useful to implement and use a distance calculation function. Note – the Euclidean distance of a point (x, y) from the origin (0, 0) can be calculated with the following equation:
distance(x, y) = √(x^2 + y^2).
3. Does not return a value. As the input list is sorted in place, it will be modified directly and these modifications will be reflected outside the function, so a return value is not needed.
While the first function that will be called is quicksort(somelist), you will likely want to create helper functions to simplify your code.
Example outputs of original lists and the same lists after the quicksort function has been called:
List before sorting: [[2, 1], [2, 2], [3, 3], [3, 1], [1, 1], [1, 2]]
List after sorting: [[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 3]]
List before sorting: [[3, 3], [2, 2], [1, 2], [2, 1], [3, 1], [1, 1]]
List after sorting: [[1, 1], [2, 1], [1, 2], [2, 2], [3, 1], [3, 3]]
List before sorting: [[1, 1], [3, 3], [2, 1], [2, 2], [1, 2], [3, 1]]
List after sorting: [[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 3]]