0
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 3 years have passed since last update.

【Python】50文字でワンライナースターリンソート

Posted at

#はじめに
###スターリンソートとは
昇順(降順)になっていない要素を粛清する(取り除く)ことで$O(n)$の計算量を実現したソートアルゴリズム?
去年話題になったらしいが、自分は出会わなかった。

Python3.8以降ならすごくスッキリ書ける。

#実装

stalin_sort = lambda x:[m:=x[0]] + [m:=i for i in x[1:] if i>=m]

#動作

arr = [1, 2, 1, 1, 4, 3, 9]
print(stalin_sort(arr))
#---> [1, 2, 4, 9]

#長さ

print(len('lambda x:[m:=x[0]] + [m:=i for i in x[1:] if i>=m]'))
#---> 50

#まとめ
粛清というより存在を無視する感じの実装になったが短くてステキ

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