gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
write python implementation of quicksort
- User
Assistant: User: write python implementation of quicksort Assistant:
- Assistant
Certainly! Here is a Python implementation of the QuickSort algorithm:
pythondef quicksort(arr): """Sorts the array using the QuickSort algorithm.""" if len(arr) <= 1: return arr pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quicksort(left) + middle + quicksort(right) # Example usage if __name__ == "__main__": array = [3, 6, 8, 10, 1, 2, 1] sorted_array = quicksort(array) print("Sorted array:", sorted_array)