You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defpartition(arr, start, end):
# Choose the last element as the pivotpivot=arr[end]
# Index for the smaller elementpartition_index=start-1forcurrent_indexinrange(start, end):
ifarr[current_index] <=pivot:
partition_index+=1# Swap the elements at partition_index and current_indexarr[partition_index], arr[current_index] =arr[current_index], arr[partition_index]
# Place the pivot element in its correct positionarr[partition_index+1], arr[end] =arr[end], arr[partition_index+1]
returnpartition_index+1defquick_sort(arr, start, end):
ifstart<end:
# Partition the array and get the partitioning indexpivot_index=partition(arr, start, end)
# Recursively sort elements before and after the partitionquick_sort(arr, start, pivot_index-1)
quick_sort(arr, pivot_index+1, end)
A= [5, 6, 7, 2, 1, 4]
quick_sort(A, 0, len(A) -1)
print("Sorted array:", A) # Sorted array: [1, 2, 4, 5, 6, 7]
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: