Last active
October 18, 2021 04:00
-
-
Save caryyu/a569e7ff9c829cfba00307ac6719bae9 to your computer and use it in GitHub Desktop.
Algorithm of QuickSort
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 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
解释非常清楚:https://wiki.jikexueyuan.com/project/easy-learn-algorithm/fast-sort.html