3
0

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でスターリンソート

Last updated at Posted at 2019-09-04

#なぜやろうと思ったか
皆さんご存知のスターリンソートを止まっていたGoとdockerの勉強がてら作成してみます。
##ソースコード


package main

import(
  "fmt"
)
func unset(s []int, i int) []int {
  if i >= len(s) {
      return s
  }
  return append(s[:i], s[i+1:]...)
}

func main() {
  stalin := [] int{3, 6, 7, 1, 9, 7, 11, 2}
  fmt.Println(stalin)
  for i := 1; i < len(stalin); i++ {
    fmt.Println("対象:",stalin[i])
    if stalin[i] < stalin[i - 1] {
      stalin = unset(stalin, i)
      fmt.Println("有罪")
    } 
  }
  fmt.Println(stalin)
}

##結果

対象: 6
対象: 7
対象: 1
有罪
対象: 7
有罪
対象: 2
有罪
[3 6 7 9 11]

粛清されましたね。
スターリンソートって表現も面白いし、ソートを粛清って言うのも笑っちゃう。

追加
よく見たら全要素見ていないのでまた修正します

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?