write quicksort in python in the most simple, short and easy to understand way
- User
write quicksort in python in the most simple, short and easy to understand way
Assistant
pythondef quicksort(arr): if len(arr) <= 1: return arr pivot = arr[0] left = [x for x in arr[1:] if x < pivot] right = [x for x in arr[1:] if x >= pivot] return quicksort(left) + [pivot] + quicksort(right)This implementation:
- Uses the first element as the pivot
- Splits the array into elements less than the pivot (left) and greater/equal (right)
- Recursively sorts both halves and combines them with the pivot in between
- Base case: arrays with 0 or 1 element are already sorted
Example usage:
pythonnumbers = [3, 6, 8, 10, 1, 2, 1] sorted_numbers = quicksort(numbers) print(sorted_numbers) # [1, 1, 2, 3, 6, 8, 10]
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.