Пример #1
0
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
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
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
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
	}
}