티스토리 뷰
문제
풀이
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
var n int
reader := bufio.NewReader(os.Stdin)
fmt.Fscanln(reader, &n)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var numbers = make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscanln(reader, &numbers[i])
}
var sortedNumbers = make([]int, n)
mergeSort(numbers, sortedNumbers, 0, n-1)
for i := 0; i < n; i++ {
fmt.Fprintln(writer, numbers[i])
}
}
func merge(numbers, sortedNumbers []int, left, mid, right int) {
var i = left
var j = mid + 1
var k = left
for true {
if !(i <= mid && j <= right) {
break
}
if numbers[i] <= numbers[j] {
sortedNumbers[k] = numbers[i]
i++
} else {
sortedNumbers[k] = numbers[j]
j++
}
k++
}
if i > mid {
for l := j; l <= right; l++ {
sortedNumbers[k] = numbers[l]
k++
}
} else {
for l := i; l <= mid; l++ {
sortedNumbers[k] = numbers[l]
k++
}
}
for l := left; l <= right; l++ {
numbers[l] = sortedNumbers[l]
}
}
func mergeSort(numbers, sortedNumbers []int, left, right int) {
if left < right {
var mid = (left + right) / 2
mergeSort(numbers, sortedNumbers, left, mid)
mergeSort(numbers, sortedNumbers, mid+1, right)
merge(numbers, sortedNumbers, left, mid, right)
}
}
728x90
'dev > algorithm' 카테고리의 다른 글
BOJ / 2108번 / 통계학 [Golang] (0) | 2020.06.29 |
---|---|
BOJ / 10989번 / 수 정렬하기 3 [Golang] (0) | 2020.06.28 |
BOJ / 2750번 / 수 정렬하기 [Golang] (0) | 2020.06.26 |
BOJ / 1436번 / 영화감독 숌 [Golang] (0) | 2020.06.25 |
BOJ / 1018번 / 체스판 다시 칠하기 [Golang] (0) | 2020.06.24 |
댓글