示例#1
0
文件: qsorts.go 项目: xzer/go-qsort
func qsort_goroutine(list []int, start int, end int, semaphoreChannel chan<- int) {
	if start >= end {
		semaphoreChannel <- 0
		return
	}

	index := part.Partition(list, start, end)

	subChannel := make(chan int)
	var subCount int = 0

	if start < index-1 {
		go qsort_goroutine(list, start, index-1, subChannel)
		subCount++
	}
	if index+1 < end {
		go qsort_goroutine(list, index+1, end, subChannel)
		subCount++
	}
	if subCount > 0 {
		for i := 0; i < subCount; i++ {
			<-subChannel
		}
	}
	semaphoreChannel <- 0
}
示例#2
0
文件: qsorts.go 项目: xzer/go-qsort
func qsort_classical(list []int, start int, end int) {
	if start >= end {
		return
	}

	index := part.Partition(list, start, end)
	if start < index-1 {
		qsort_classical(list, start, index-1)
	}

	if index+1 < end {
		qsort_classical(list, index+1, end)
	}
}
示例#3
0
文件: qsorts.go 项目: xzer/go-qsort
func qsort_goroutine_single_channel(list []int, start int, end int, semaphoreChannel chan<- int) {
	if start >= end {
		semaphoreChannel <- -1
		return
	}

	index := part.Partition(list, start, end)

	if start < index-1 {
		semaphoreChannel <- 1
		go qsort_goroutine_single_channel(list, start, index-1, semaphoreChannel)
	}
	if index+1 < end {
		semaphoreChannel <- 1
		go qsort_goroutine_single_channel(list, index+1, end, semaphoreChannel)
	}

	semaphoreChannel <- -1
}
示例#4
0
文件: qsorts.go 项目: xzer/go-qsort
func qsort_goroutine_ultimate_optimization(list []int, start int, end int, semaphoreChannel chan<- int) {
	if start >= end {
		if semaphoreChannel != nil {
			semaphoreChannel <- -1
		}
		return
	}

	index := part.Partition(list, start, end)

	if end-start > 10000 {
		if start < index-1 {
			if semaphoreChannel != nil {
				semaphoreChannel <- 1
			}
			go qsort_goroutine_ultimate_optimization(list, start, index-1, semaphoreChannel)
		}
		if index+1 < end {
			if semaphoreChannel != nil {
				semaphoreChannel <- 1
			}
			go qsort_goroutine_ultimate_optimization(list, index+1, end, semaphoreChannel)
		}
	} else {
		if start < index-1 {
			qsort_goroutine_ultimate_optimization(list, start, index-1, nil)
		}
		if index+1 < end {
			qsort_goroutine_ultimate_optimization(list, index+1, end, nil)
		}
	}

	if semaphoreChannel != nil {
		semaphoreChannel <- -1
	}
}