Skip to content

Instantly share code, notes, and snippets.

@harkce
Created February 13, 2021 10:55
Show Gist options
  • Select an option

  • Save harkce/1625c3b1814d001f9bbba65e848e9942 to your computer and use it in GitHub Desktop.

Select an option

Save harkce/1625c3b1814d001f9bbba65e848e9942 to your computer and use it in GitHub Desktop.
Quicksort
package main
import "fmt"
func quicksort(A []int) []int {
if len(A) < 2 {
return A
}
left := 0
right := len(A) - 1
for i := 0; i < len(A)-1; i++ {
if A[i] < A[right] {
A[i], A[left] = A[left], A[i]
left++
}
}
A[left], A[right] = A[right], A[left]
quicksort(A[:left])
quicksort(A[left+1:])
return A
}
func main() {
unsorted := []int{5, 3, 2, 8, 6, 1, 4}
fmt.Printf("Unsorted: %v\n", unsorted)
fmt.Printf("Sorted : %v\n", quicksort(unsorted))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment