iT邦幫忙

4

蠻可愛的 golang #43 睡覺排序法

蠻可愛的golang 系列,是 golang 還沒什麼人理他的時候,總裁要我寫些簡單的來介紹給大家,
當年寫到 #42
https://ithelp.ithome.com.tw/articles/10154811
然後接著是鐵人賽的30篇
https://ithelp.ithome.com.tw/users/20091311/ironman/734

那時候 golang 正從測試版到1.0正式發行階段,後續就逐漸較多人使用.
昨天看到有人在問 bubble sort,
還有前天回答了一篇 golang channel
https://ithelp.ithome.com.tw/questions/10199225

有使用到 goroutine.
那就來個使用 goroutine 跑跑, 沒有什麼演算法的排序方法.

// sleepSort.go
// Craeted: 2020-07-10 07:28:55 
// ---------------------
package main

import (
  "fmt"
  "math/rand"
  "sync"
  "time"
)

func main() {
  var length int = 10
  
  var originalS []int
  var sortedS []int
  var wg sync.WaitGroup
  
  rand.Seed(time.Now().Unix())
  
  for i := 0; i < length; i++ {
    originalS = append(originalS ,rand.Intn(length * 10))
  }
  
  fmt.Println("排序前 :", originalS)
  fmt.Printf("開始產生 %d 個 goroutine\n", length)

  for _, j := range originalS{
    wg.Add(1)
    go func(n int) {
      defer wg.Done()
      time.Sleep(1 * time.Second + time.Duration(n * 10) * time.Millisecond)
      fmt.Print(n, " ")
      sortedS = append(sortedS, n)
    }(j)
  }
  
  wg.Wait()
  fmt.Println()
  fmt.Println("排序後 :", sortedS)
}

執行結果:
https://ithelp.ithome.com.tw/upload/images/20200710/20050647N86gGe6PHD.png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

1
海綿寶寶
iT邦大神 1 級 ‧ 2020-07-10 09:31:12

當年寫到 #42
然後接著是鐵人賽的30篇

我承認,我一篇都沒看
/images/emoticon/emoticon25.gif

剛剛仔細看了十幾遍
才看懂睡覺排序法的原理
以及 golang 平行運算的處理範例

耶,今天又學到了新知識了
/images/emoticon/emoticon41.gif

按送出後突然想到
這個睡覺排序法
如果 time.Sleep 裡的值都一樣
或者移除 time.Sleep 整列的話
會發生什麼事?
而最後的結果
會不會每次都不一樣?

看更多先前的回應...收起先前的回應...

就是讓他們睡覺的時間不同啊,根據輸入值來睡,這樣子,就排好序了啊.

就是讓他們睡覺的時間不同啊,根據輸入值來睡,這樣子,就排好序了啊.

這段我已經看懂了,謝謝

我是在想「如果睡覺時間都一樣,或者都不要睡覺,那麼 sortedS 的內容會不會每次都一樣/不一樣?」

都一樣,那就沒排了啊..... 就變成以調度器 先執行哪個 goroutine 的順序來排了.

雷N iT邦研究生 1 級 ‧ 2020-07-13 00:47:18 檢舉

XD 真是沒想到有這招(排序一下要睡到最大的秒數XD)

左去右回 , 抓最慢三個!

離開地球表面
/images/emoticon/emoticon49.gif

叫做 Python , import 這個看看.

import antigravity
0
一級屠豬士
iT邦大師 1 級 ‧ 2020-07-10 10:08:19

不讓你睡排序法

// nonSleepSort.go
// Craeted: 2020-07-10 10:04:57 
// ---------------------
package main

import (
  "fmt"
  "math/rand"
  "sync"
  "time"
)

func main() {
  var length int = 10
  
  var originalS []int
  var sortedS []int
  var wg sync.WaitGroup
  
  rand.Seed(time.Now().Unix())
  
  for i := 0; i < length; i++ {
    originalS = append(originalS ,rand.Intn(length * 10))
  }
  
  fmt.Println("應海棉寶寶要求,改成不讓你睡排序法!")
  fmt.Println("排序前 :", originalS)
  fmt.Printf("開始產生 %d 個 goroutine\n", length)

  for _, j := range originalS{
    wg.Add(1)
    go func(n int) {
      defer wg.Done()
      //time.Sleep(1 * time.Second + time.Duration(n * 10) * time.Millisecond)
      fmt.Print(n, " ")
      sortedS = append(sortedS, n)
    }(j)
  }
  
  wg.Wait()
  fmt.Println()
  fmt.Println("排序後 :", sortedS)
}

https://ithelp.ithome.com.tw/upload/images/20200710/20050647LzOwUg4fAm.png

看更多先前的回應...收起先前的回應...

這是沒有排序的 佛系方法

應該改叫 shuffule 洗牌.

我明白這不是排序
我只是好奇「平行」運算可以「平行」到什麼程度?
單純就這個結果來看
還真的很平行哩

祝 週末愉快
/images/emoticon/emoticon41.gif

其實上面那樣,是有bug 的, 會有 race condition.

我要留言

立即登入留言