Skip to content

Instantly share code, notes, and snippets.

@yasuoza
Created August 1, 2020 10:25
Show Gist options
  • Save yasuoza/9f2e58c1e207bb3c76c611a354cdf903 to your computer and use it in GitHub Desktop.
Save yasuoza/9f2e58c1e207bb3c76c611a354cdf903 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
arr := []int{10, 7, 3, 4, 6, 7, 0, 9}
arr = sort(arr)
fmt.Println(arr)
}
func sort(arr []int) []int {
if len(arr) <= 1 {
return arr
}
p := (len(arr) - 1) / 2
fmt.Println("arr", arr, "p", p, "pivot", arr[p])
var left []int
var right []int
for i, e := range arr {
if e < arr[p] {
left = append(left, e)
} else if i != p && e >= arr[p] {
right = append(right, e)
}
}
return append(append(sort(left), arr[p]), sort(right)...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment