6
4

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.

Sleep SortをRxJSで書いた

Last updated at Posted at 2017-11-17

ちきさんです。

画期的なソートと言われている Sleep Sort が社内で話題になったので勢いで書きました。
(出典: 常識を覆すソートアルゴリズム!その名も"sleep sort"!)

デモ → http://jsbin.com/faloraz/7/edit?js,console

コードはたったこれだけです。

sleepsort.js
const array = [2, 4, 1, 5, 3]

Observable.from(array)
  .flatMap(x => Observable.of(x).delay(x * 100)) // xが2なら200ms後に次に流す。
  .toArray()
  .subscribe(console.log)

ちなみにflatMapmergeMapエイリアスです。

このときの出力は下記の通りです。

[1, 2, 3, 4, 5]

確かにソートされていますね、素晴らしいです。

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?