1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【ネタ】Go言語 で Sleep Sort

Last updated at Posted at 2017-07-12

まえおき

最近 Golang を学習し始めた。
良い題材が浮かばなかったので、少し前に流行った Sleep Sort を実装してみた。

やってみた

package main

import (
	"fmt"
	"time"
)

func main() {

	array := []int{5, 3, 6, 3, 6, 3, 1, 4, 7}
	c := make(chan int)

	for _, v := range array {
		s := v
		go func() {
			time.Sleep(time.Duration(s) * time.Second)
			c <- s
		}()
	}

	for _ = range array {
		fmt.Printf("%d\n", <-c)
	}
}

正直、学習し始めなので良し悪しの判断がつかない...
詳しい方教えてください!

元ネタ

【ネタ】Swift で Sleep Sort
【ネタ】JavaScriptでSleep Sort
【ネタ】Java で Sleep Sort
【ネタ】PythonでSleep Sort
【ネタ】Pythonでスレッドセーフな Sleep Sort関数 (threading編)
【ネタ】SmalltalkでスレッドセーフなSleepSort

参考にさせていただきました

golang チートシート
Goルーチンで並行化する方法: 6秒かかる処理を3秒にしよう
Golang Goの並列処理を学ぶ(goroutine, channel)
Goのforとgoroutineでやりがちなミスとたった一つの冴えたgo vetと

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?