Skip to content

Instantly share code, notes, and snippets.

@DrJackilD
Created March 31, 2017 12:07
Show Gist options
  • Save DrJackilD/c9912f531c624edef1fceb81ea488efe to your computer and use it in GitHub Desktop.
Save DrJackilD/c9912f531c624edef1fceb81ea488efe to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"errors"
)
func Sort(list []int) ([]int, error) {
if len(list) < 2 {
return nil, errors.New("Slice must contain at least two elements")
}
left := 0
right := len(list) - 1
for i := range list {
if list[i] < list[right] {
list[i], list[left] = list[left], list[i]
left++
}
}
list[left], list[right] = list[right], list[left]
Sort(list[:left])
Sort(list[left+1:])
return list, nil
}
func main() {
l := make([]int, 2*10)
for i := 0; i < len(l); i++ {
l[i] = rand.Intn(10000)
}
fmt.Printf("Before sorting: %v\n", l)
Sort(l)
fmt.Printf("After sorting: %v\n", l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment