Certainly! Below is an example of a Python function that implements the Quicksort algorithm using the Lomuto partition scheme:
python
def quicksort(arr):
def _quicksort(items, low, high):
if low < high:
# Partition the array and get the partition index
p = partition(items, low, high)
# Sort the elements in the left of partition index
_quicksort(items, low, p - 1)
# Sort the elements in the right of partition index
_quicksort(items, p + 1, high)
def partition(items, low, high):
# Choose the rightmost element as pivot
pivot = items[high]
i = low
for j in range(low, high):
# If current element is smaller than or equal to pivot
if items[j] <= pivot:
# Swap elements at i and j
items[i], items[j] = items[j], items[i]
i += 1
# Swap the pivot element with the element at the partition index
items[i], items[high] = items[high], items[i]
# Return the partition index
return i
_quicksort(arr, 0, len(arr) - 1)
return arr
# Example usage:
arr = [10, 7, 8, 9, 1, 5]
sorted_arr = quicksort(arr)
print(f"Sorted array: {sorted_arr}")
This function takes an array arr and sorts it in place using the Quicksort algorithm. The _quicksort is a helper function that recursively sorts sub-arrays, and partition is another helper function that partitions the array around a pivot element and returns the index of the pivot after partitioning.
Quicksort is a divide-and-conquer algorithm, which works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.
Please note that this implementation sorts the array in place, meaning that it does not return a new array but rather modifies the original array passed to it. The function returns the array for convenience.