码迷,mamicode.com
首页 > 编程语言 > 详细

go快速排序

时间:2019-08-08 21:34:10      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:turn   quick   star   int   ++   imp   temp   pac   quic   

 

package main

import (
	"fmt"
)

func quickSort(a []int, left int, right int) {
	if left >= right {  //一定是left >= right
		return
	}
	temp := a[left]
	start := left
	stop := right
	for right != left {
		for right > left && a[right] >= temp  {
			right --
		}
		for left < right && a[left] <= temp  {
			left ++
		}
		if right > left {
			a[right], a[left] = a[left], a[right]
		}
	}
	a[right], a[start] = temp, a[right]
	quickSort(a, start, left)
	quickSort(a, right+1, stop)
}

func main() {
	a := []int{12,3,111,23,65,45}
	quickSort(a, 0, len(a)-1)
	fmt.Println(a)
}

  

go快速排序

标签:turn   quick   star   int   ++   imp   temp   pac   quic   

原文地址:https://www.cnblogs.com/xingyunshizhe/p/11323728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!