Created
March 11, 2019 08:27
-
-
Save AaronFlower/a2761405b23e856a5599ca327cd66ea4 to your computer and use it in GitHub Desktop.
quick sort
This file contains hidden or 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 quicksort | |
// Quicksort use partition method to sort an array. | |
func Quicksort(data []int) { | |
if len(data) > 1 { | |
i := partition(data) | |
Quicksort(data[:i]) | |
Quicksort(data[i+1:]) | |
} | |
} | |
func partition(data []int) int { | |
i, j := -1, 0 | |
L := len(data) | |
pivot := data[L-1] | |
for ; j < L-1; j++ { | |
if data[j] < pivot { | |
i++ | |
data[i], data[j] = data[j], data[i] | |
} | |
} | |
i++ | |
data[i], data[j] = data[j], data[i] | |
return i | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment