Go语言实现随机快速排序

package main                                                                                                                                                                                                                                
import (                                                                                                                  
	"fmt"                                                                                                                 
	"math/rand"                                                                                                       
)                                                                                                                                                                                                                                           

func swap(num []int, num1 int, num2 int) {                                                                                
	temp := num[num1]                                                                                                     
	num[num1] = num[num2]                                                                                                 
	num[num2] = temp                                                                                                  
}                                                                                                                                                                                                                                           

func partition(num []int, left int, right int) (int, int){                                                                
	less := left - 1                                                                                                      
	more := right                                                                                                         
	for (left < more) {                                                                                                       
		if(num[left] < num[right]){                                                                                               	
			less++                                                                                                                
			swap(num, less, left)                                                                                                 
			left++                                                                                                            
		} else if (num[left] > num[right]){                                                                                       
			more--                                                                                                                	
			swap(num, more, left)                                                                                             
		} else {                                                                                                                  
			left++                                                                                                            
		}                                                                                                                 
	}                                                                                                                     
	swap(num, more, right)                                                                                                
	return less + 1, more                                                                                             
}    

func RandomQuickSorting(num []int, left int, right int) {                                                                 
	if(num == nil || len(num) < 2) {                                                                                          
		return                                                                                                            
	}                                                                                                                                                                                                                                           
	if(left < right) {                                                                                                        
		swap(num, right, left + rand.Intn(right - left + 1))                                                                  
		start, end := partition(num, left, right)                                                                             	
		fmt.Println("Select Number: ", num[start])                                                                            
		RandomQuickSorting(num, left, start - 1)                                                                              
		RandomQuickSorting(num, end + 1, right)                                                                           
	}                                                                                                                                                                                                                                       
}                                                                                                                                                                                                                                           
func main() {                                                                                                             
	num := [...]int{7,5,9,3,4,9,2,1,6,8,4,9,3}                                                                            
	fmt.Println("Brfore Sort: ", num)                                                                                     
	RandomQuickSorting(num[:], 0, len(num) - 1)                                                                           
	fmt.Println("After Sort: ", num)                                                                                  
} 
posted @ 2019-08-29 19:03  AlexKing007  阅读(143)  评论(0编辑  收藏  举报