Algorithm Steps
- Choose a pivot element from the array.
- Partition the array so that all elements less than the pivot are on the left, and all greater elements are on the right.
- Recursively apply the above steps to the left and right partitions.
- Combine the partitions to form a sorted array.
Quick Sort Program Code
Python
Java
JavaScript
C
C++
C#
Kotlin
Swift
Go
Php
def quick_sort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[len(arr) // 2]
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)
if __name__ == '__main__':
arr = [6, 3, 8, 2, 7, 4]
sorted_arr = quick_sort(arr)
print("Sorted array is:", sorted_arr)