Skip to content

Instantly share code, notes, and snippets.

@caryyu
Last active October 18, 2021 04:00
Show Gist options
  • Save caryyu/a569e7ff9c829cfba00307ac6719bae9 to your computer and use it in GitHub Desktop.
Save caryyu/a569e7ff9c829cfba00307ac6719bae9 to your computer and use it in GitHub Desktop.
Algorithm of QuickSort
package main
import "fmt"
func main() {
var nums []int = []int{6, 1, 2, 7, 9, 3, 4, 5, 10, 8}
quicksort(nums, 0, len(nums)-1)
fmt.Println(nums)
}
func quicksort(arr []int, idx1 int, idx2 int) {
if idx2-idx1 <= 0 {
return
}
var i int = idx1 + 1
var j int = idx2
var pivot = arr[idx1]
for i < j {
for arr[j] >= pivot {
j--
}
for arr[i] <= pivot {
i++
}
if i < j {
var tmp int = arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
}
if arr[j] < pivot {
arr[idx1] = arr[j]
arr[j] = pivot
}
quicksort(arr, idx1, j-1)
quicksort(arr, j+1, idx2)
}
@caryyu
Copy link
Author

caryyu commented Sep 22, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment