Skip to content

Instantly share code, notes, and snippets.

@bluven
Last active September 11, 2020 07:32
Show Gist options
  • Save bluven/a3deca23bbafa334319305704b62a0ce to your computer and use it in GitHub Desktop.
Save bluven/a3deca23bbafa334319305704b62a0ce to your computer and use it in GitHub Desktop.
package main
import "fmt"
func quicksort(nums []int) {
if nums == nil || len(nums) <= 1 {
return
}
quicksort2(nums, 0, len(nums) - 1)
}
func quicksort2(nums []int, start, end int) {
if start >= end {
return
}
pi := partition(nums, start, end)
quicksort2(nums, start, pi-1)
quicksort2(nums, pi+1, end)
}
func partition(nums []int, start, end int) int {
pi := start
pivot := nums[end]
for i := start; i < end; i++ {
if nums[i] < pivot {
nums[i], nums[pi] = nums[pi], nums[i]
pi += 1
}
}
nums[end], nums[pi] = nums[pi], nums[end]
return pi
}
func main() {
nums := []int{2, 4, 3, 1, 5, 8, 10, 110, 1, 3, 4, 5, -1}
quicksort(nums)
fmt.Println(nums)
}
~
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment