quick sort in python

Here is an example implementation of the Quick Sort algorithm in Python:

def quick_sort(arr):
    """Sort a list of numbers using the Quick Sort algorithm."""
    # Check base case where arr has only one element
    if len(arr) == 1:
        return arr
    
    pivot = arr[len(arr) // 2]  # Select the middle value as the pivot
    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 quick_sort(left) + middle + quick_sort(right)

You can test it by passing a list into the quick_sort function like this:

my_list = [-34, -56, 0, 5789, 14, 67, -10, 224]
sorted_list = quick_sort(my_list)
print(sorted_list)

Copied

The output should be [-34, -56, -10, 0, 14, 224, 5789, 67]. Please note that this is just one possible implementation of Quick Sort in Python, and there might exist variations based on specific use cases.





Model: OpenAssistant/oasst-sft-6-llama