Quick Sort - Python

 def quick_sort_simple(arr):

    if len(arr) <= 1:

        return arr  # Base case: a list with 0 or 1 element is already sorted

    else:

        pivot = arr[0]  # Choosing the first element as the pivot

        less = [x for x in arr[1:] if x < pivot]

        equal = [x for x in arr[1:] if x == pivot]

        greater = [x for x in arr[1:] if x > pivot]

        return quick_sort_simple(less) + [pivot] + equal + quick_sort_simple(greater)

        # Note: The above line is slightly modified from source to handle duplicate pivots correctly


# Example Usage:

my_list = [3, 6, 8, 10, 1, 2, 1]

sorted_list = quick_sort_simple(my_list)

print(f"Original list: {my_list}")

print(f"Sorted list: {sorted_list}")





Comments