gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
hello
- User
Assistant: User: User: hello User: hello User: write python implementation of quicksort Assistant: Assistant:
- Assistant
Certainly! Here is a Python implementation of the QuickSort algorithm:
pythondef quicksort(arr): """Sorts an array using the QuickSort algorithm.""" if len(arr) <= 1: return arr else: pivot = arr[len(arr) // 2] # Choose the middle element as the pivot left = [x for x in arr if x < pivot] # Elements less than the pivot middle = [x for x in arr if x == pivot] # Elements equal to the pivot right = [x for x in arr if x > pivot] # Elements greater than the pivot # Recursively apply the quicksort to left and right parts return quicksort(left) + middle + quicksort(right) # Example usage: arr = [3, 6, 8, 10, 1, 2, 1] sorted_arr = quicksort(arr) print("Sorted array:", sorted_arr)