Created
March 2, 2017 01:20
-
-
Save ympons/3ab292a3f6e06f49f79f1abbce28cd83 to your computer and use it in GitHub Desktop.
Simple QuickSort implementation in Golang #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
func quicksort(a sort.Interface, i, j int) { | |
if j > i { | |
left, right := i, j | |
piv := j | |
for left <= right { | |
for ;a.Less(left, piv); left++ {} | |
for ;a.Less(piv, right); right-- {} | |
if left <= right { | |
a.Swap(left, right) | |
left++ | |
right-- | |
} | |
} | |
quicksort(a, i, right) | |
quicksort(a, left, j) | |
} | |
} | |
func main() { | |
a := []int{1, 3, 9, 8, 2, 7, 5} | |
fmt.Println("unsorted: ", a) | |
quicksort(sort.IntSlice(a), 0, len(a)-1) | |
fmt.Println("Sorted: ", a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment