Pseudocode
procedure quick_sort(array, low, high)
if low < high
pivot = array[high]
i = low - 1
for j = low to high - 1
if array[j] <= pivot
i = i + 1
swap array[i] and array[j]
end if
end for
swap array[i + 1] and array[high]
pivot = i + 1
quick_sort(array, low, pivot - 1)
quick_sort(array, pivot + 1, high)
end if
end procedure
Python Implementation
def quick_sort(array, low, high):
if low < high:
pivot = array[high]
i = low - 1
for j in range(low, high):
if array[j] <= pivot:
i += 1
array[i], array[j] = array[j], array[i]
array[i + 1], array[high] = array[high], array[i + 1]
pivot = i + 1
quick_sort(array, low, pivot - 1)
quick_sort(array, pivot + 1, high)
# Example usage:
array = [5, 2, 4, 1, 3]
quick_sort(array, 0, len(array) - 1)
print(array) # Output: [1, 2, 3, 4, 5]