Last active
July 31, 2019 11:29
-
-
Save linnv/12d82de6cf1f8a2cdbd0651e7eaf9de3 to your computer and use it in GitHub Desktop.
merge sort parallelly, reuse slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"sync" | |
) | |
const parallelBySliceLength = 1 << 10 * 3 | |
func MergeSortLoop(src []int64) { | |
srcReadOnly := make([]int64, len(src))//update only in merge stage | |
copy(srcReadOnly, src) | |
mergeSortDivideLoop(src, srcReadOnly, 0, len(src)) | |
} | |
func mergeSortDivideLoop(srcWriteOnly, srcReadOnly []int64, indexLeftPart, indexRightPart int) { | |
srcLen := indexRightPart - indexLeftPart | |
if indexRightPart <= indexLeftPart || srcLen == 1 { | |
return | |
} | |
mid := (indexRightPart + indexLeftPart) / 2 | |
const parallelBySliceLength = 1 << 10 | |
if mid-indexLeftPart > parallelBySliceLength || indexRightPart-mid > parallelBySliceLength { | |
var wg sync.WaitGroup | |
wg.Add(2) | |
go func() { | |
defer wg.Done() | |
mergeSortDivideLoop(srcWriteOnly, srcReadOnly, indexLeftPart, mid) | |
return | |
}() | |
go func() { | |
defer wg.Done() | |
mergeSortDivideLoop(srcWriteOnly, srcReadOnly, mid, indexRightPart) | |
return | |
}() | |
wg.Wait() | |
mergeSortConqueLoop(srcWriteOnly, srcReadOnly, indexLeftPart, indexRightPart) | |
return | |
} | |
mergeSortDivideLoop(srcWriteOnly, srcReadOnly, indexLeftPart, mid) | |
mergeSortDivideLoop(srcWriteOnly, srcReadOnly, mid, indexRightPart) | |
mergeSortConqueLoop(srcWriteOnly, srcReadOnly, indexLeftPart, indexRightPart) | |
return | |
} | |
func mergeSortConqueLoop(srcWriteOnly, srcReadOnly []int64, indexLeftPart, indexRightPart int) { | |
mid := (indexRightPart + indexLeftPart) / 2 | |
indexLeftPartMove, indexRightPartMove, IndexRetMove := indexLeftPart, mid, indexLeftPart | |
for { | |
if indexLeftPartMove < mid && indexRightPartMove < indexRightPart { | |
if srcReadOnly[indexLeftPartMove] <= srcReadOnly[indexRightPartMove] { | |
srcWriteOnly[IndexRetMove] = srcReadOnly[indexLeftPartMove] | |
indexLeftPartMove++ | |
} else { | |
srcWriteOnly[IndexRetMove] = srcReadOnly[indexRightPartMove] | |
indexRightPartMove++ | |
} | |
IndexRetMove++ | |
} else { | |
break | |
} | |
} | |
for i := indexLeftPartMove; i < mid; i++ { | |
srcWriteOnly[IndexRetMove] = srcReadOnly[i] | |
IndexRetMove++ | |
} | |
for i := indexRightPartMove; i < indexRightPart; i++ { | |
srcWriteOnly[IndexRetMove] = srcReadOnly[i] | |
IndexRetMove++ | |
} | |
for i := indexLeftPart; i < indexRightPart; i++ { | |
srcReadOnly[i] = srcWriteOnly[i] | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
original, no reuse no parallel processing
reuse src only
parallel processing, no reuse merge slice
parallel processing, reuse merge slice by global slice