Implement the quicksort algorithm and discuss its average-case and worst-case time and space complexity.