Created
September 11, 2020 07:32
-
-
Save bluven/95df9247149f5e8e0c560ef7e479b07d to your computer and use it in GitHub Desktop.
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" | |
func quicksort(nums []int) { | |
if nums == nil || len(nums) <= 1 { | |
return | |
} | |
quicksort2(nums, 0, len(nums) - 1) | |
} | |
func quicksort2(nums []int, start, end int) { | |
if start >= end { | |
return | |
} | |
pi := partition(nums, start, end) | |
quicksort2(nums, start, pi-1) | |
quicksort2(nums, pi+1, end) | |
} | |
func partition(nums []int, start, end int) int { | |
pi := start | |
pivot := nums[end] | |
for i := start; i < end; i++ { | |
if nums[i] < pivot { | |
nums[i], nums[pi] = nums[pi], nums[i] | |
pi += 1 | |
} | |
} | |
nums[end], nums[pi] = nums[pi], nums[end] | |
return pi | |
} | |
func main() { | |
nums := []int{2, 4, 3, 1, 5, 8, 10, 110, 1, 3, 4, 5, -1} | |
quicksort(nums) | |
fmt.Println(nums) | |
} | |
~ | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment