Skip to content

Instantly share code, notes, and snippets.

@urielhdz
Last active October 7, 2015 22:23
Show Gist options
  • Save urielhdz/6243dd748d0fc40f45a2 to your computer and use it in GitHub Desktop.
Save urielhdz/6243dd748d0fc40f45a2 to your computer and use it in GitHub Desktop.
package main
import(
"fmt"
)
func main() {
arr := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84,1}
quick_sort(&arr,0,len(arr)-1) //Initialize with 0 as left and length - 1 as right
fmt.Println(arr) //Sorted array
}
func quick_sort(array *[]int, left,right int) {
if left < right{
arr := *array
limit, origin := right, left //Save limit and origin for recursive call
pivot := arr[right] // Choose the rightmost element of the array as pivot
for left <= right{
for left < len(arr) && arr[left] < pivot{
left++
}
for right >= 0 && arr[right] >= pivot{
right--
}
if left <= right{
swap(array, left,right)
left++
right--
}
}
swap(array, left, limit) // Swaps the pivot into its position
quick_sort(array, origin, right)
quick_sort(array, left, limit)
}
}
func swap(array *[]int, left,right int){
arr := *array
temp := arr[left]
arr[left] = arr[right]
arr[right] = temp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment