# Golang快速排序算法

u011774512 · · 2053 次点击 · · 开始浏览

```package main

import (
"fmt"
)

func QSort(arr []int, start int, end int) {
var (
key  int = arr[start]
low  int = start
high int = end
)
for {
for low < high {
if arr[high] < key {
arr[low] = arr[high]
break
}
high--
}
for low < high {
if arr[low] > key {
arr[high] = arr[low]
break
}
low++
}
if low >= high {
arr[low] = key
break
}
}
if low-1 > start {
QSort(arr, start, low-1)
}
if high+1 < end {
QSort(arr, high+1, end)
}
}

func main() {
arr := []int{5, 1, 3, 0, 9, 8, 7, 4, 6}
QSort(arr, 0, len(arr)-1)
fmt.Println(arr)
}```

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传
```package main

import (
"fmt"
)

func QSort(arr []int, start int, end int) {
var (
key  int = arr[start]
low  int = start
high int = end
)
for {
for low < high {
if arr[high] < key {
arr[low] = arr[high]
break
}
high--
}
for low < high {
if arr[low] > key {
arr[high] = arr[low]
break
}
low++
}
if low >= high {
arr[low] = key
break
}
}
if low-1 > start {
QSort(arr, start, low-1)
}
if high+1 < end {
QSort(arr, high+1, end)
}
}

func main() {
arr := []int{5, 1, 3, 0, 9, 8, 7, 4, 6}
QSort(arr, 0, len(arr)-1)
fmt.Println(arr)
}```