Quick Sort
Algorithm Description
Implementation in Go
Standard QuickSort
func quickSort(arr []int, low, high int) {
if low < high {
// Find pivot element such that
// elements smaller than pivot are on the left
// elements greater than pivot are on the right
pi := partition(arr, low, high)
// Separately sort elements before and after partition
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
}
}
func partition(arr []int, low, high int) int {
// Choose rightmost element as pivot
pivot := arr[high]
// Index of smaller element
i := low - 1
// Compare each element with pivot
for j := low; j < high; j++ {
if arr[j] <= pivot {
i++
arr[i], arr[j] = arr[j], arr[i]
}
}
// Place pivot in its correct position
arr[i+1], arr[high] = arr[high], arr[i+1]
return i + 1
}Random Pivot Selection
Three-Way QuickSort (for duplicates)
Time Complexity: O(n log n) average case
Space Complexity: O(log n)
Characteristics
Advantages
Disadvantages
Related LeetCode Problems
Easy
Medium
Hard
Practice Tips
Common Variations
Resources
Last updated