Golang快速排序算法

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)
}

如果转载请注明出处:http://blog.csdn.net/gophers/article/details/23346587


    原文作者:排序算法
    原文地址: https://blog.csdn.net/gophers/article/details/23346587
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞