ãããããã¤ã¼ããè¦ãããã®ã§å ã®è«æãèªãã ãå®è£ ãã¦ã¿ãããã¾ãã
ä¸çä¸åç´ãª? ã½ã¼ãã¢ã«ã´ãªãºã ã
— æ°å±±ç¥ä» (Yusuke Shinyama) (@mootastic) 2021å¹´10æ6æ¥
for i=1..n:
for j=1..n:
if a[i]<a[j]:
swap(a[i], a[j])
ä¸è¦ããã«ã½ã¼ãã®ããã«è¦ããããifå¤å®ã®ä¸çå·ã®åããããã«ã½ã¼ãã¨ã¯éã«ãªã£ã¦ããã®ã«ãããã§ãã½ã¼ãããããhttps://t.co/1qSnomUrUR
注æç¹ã§ããããã®ã¢ã«ã´ãªãºã èªä½ã¯ãã使ãããã½ã¼ãã¢ã«ã´ãªãºã ã¨æ¯ã¹ã¦ç¹ã«å©ç¹ãããããã§ã¯ãªãã¦ãã¿çãªè©±ã§ãã
å
è«æ
arxiv.org
å®è£ (Python)
ãã®è¨äºã®ã½ã¼ãã§ã¯æé ã«ä¸¦ã¹ã¾ã
ICanâtBelieveItCanSort
ã¨ããããã§è«æã§è¨ãã¨ããã®ICanâtBelieveItCanSort
ãå®è£
ãã¦ã¿ã¾ããã
https://arxiv.org/abs/2110.01111
äºéã«ã¼ããã¦\(j\)çªç®ã®è¦ç´ ã\(i\)çªç®ã®è¦ç´ ããã大ããã£ãã交æããã ããªã®ã§åç´ã§ãã
åé ã®ãã¤ã¼ãã§ãæ¸ããã¦ããããã«ã以ä¸ã§å®è£
ãã¦ããä»ã®ã½ã¼ãã¨ã²ã¨ç®è¦æ¯ã¹ã¦ã¿ãã¨äº¤æããã¨ãã®å¤§å°ã®æ¡ä»¶ãéã«ãªã£ã¦ããããã«è¦ããªãããªãã§ãã
def i_cant_believe_it_can_sort(A: list): n = len(A) for i in range(n): for j in range(n): if A[i] < A[j]: A[i], A[j] = A[j], A[i] return A
ExchangeSort
è«æä¸ã«ã¯ExchangeSort
ã¨ããã®ãåºã¦ããã®ã§ãããå®è£
ãã¦ã¿ã¾ããã
ã¡ãªã¿ã«å
ã®è«æã§ã¯ãExchangeSort
ã®å
å´ã®\(i\)ããã®ã«ã¼ããå
é ããã®ã«ã¼ãã¨ééãã¦å®è£
ããã¨ãã«ICanâtBelieveItCanSort
)ãçºè¦ããã¨ãããµãã«æ¸ããã¦ãã¾ãã(ãã®å ´åã¯å¤§å°ã®æ¡ä»¶ãéãªã®ã§éé ã«ãªã)
def exchange_sort(A: list): n = len(A) for i in range(n): for j in range(i + 1, n): if A[i] > A[j]: A[i], A[j] = A[j], A[i] return A
ããã«ã½ã¼ã
åèç¨ã«åæ§ã«ããã«ã½ã¼ããå®è£
ãã¦ã¿ã¾ããã
é£å士ã®è¦ç´ ãæ¯ã¹ã¦é çªãéé ã«ãªã£ã¦ãããå
¥ãæ¿ãã¾ãã
å®è£
çã«ã¯ICanâtBelieveItCanSort
ã¨ã©ã£ã¡ãã·ã³ãã«ãã¨ããã®ã¯ãªãã¨ãè¨ãé£ãã§ã
def bubble_sort(A: list): n = len(A) for i in range(n - 1): for j in range(n - 1): if A[j] > A[j + 1]: A[j], A[j + 1] = A[j + 1], A[j] return A
ãªãã§ã½ã¼ãã§ããã®ï¼
ICanâtBelieveItCanSort
ãã©ãåããé©å½ãªé
åãä¾ã«èª¬æãã¦ã¿ã¾ãããã
æåã¯[2, 5, 9, 7, 1, 7]
ã¨ãã¾ãã
\(j\)çªç®ã®è¦ç´ ã\(i\)çªç®ã®è¦ç´ ããã大ããã£ãã交æããã®ã§ãæåã®\(i=0\)ã®ã«ã¼ãå¾ã¯é
åã®å
é ã«æ大ã®è¦ç´ ãå
¥ãã¾ãã
i=0, j=0, [2, 5, 9, 7, 1, 7]
i=0, j=1, [5, 2, 9, 7, 1, 7]
i=0, j=2, [9, 2, 5, 7, 1, 7]
i=0, j=3, [9, 2, 5, 7, 1, 7]
i=0, j=4, [9, 2, 5, 7, 1, 7]
i=0, j=5, [9, 2, 5, 7, 1, 7]
ä¸ã®ä¾ãè¦ã¦ããããããã«\(i\)çªç®ã«æ大ã®è¦ç´ ãæ¥ãã¨ãã以ä¸äº¤æã¯è¡ãããªããªãã¾ã
\(i=1\)ã®ã«ã¼ãã§ã¯ãæåã[9, 2, 5, 7, 1, 7]
ã§
i=1, j=0, [2, 9, 5, 7, 1, 7]
i=1, j=1, [2, 9, 5, 7, 1, 7]
以ä¸ç¥
\(i=0\)ã®ã«ã¼ãã§å
é ã«æ大ã®è¦ç´ ãããã®ã§ãã以é\(j\)ã«é¢ããã«ã¼ãã¯\(j=i-1\)ã¾ã§ããã°ååã«ãªã£ã¦ãã¾ã(ãã以ä¸ãã£ã¦ã交æã¯è¡ãããªã)
ãã®ãã¨ãã\( j < i \)ã¨ãªã£ã¦ãã¾ãããªã®ã§æåã«å¤§å°ã®æ¯è¼ã®æ¡ä»¶ãéã«è¦ãããã¨ãã話ãããã¾ããããiçªç®ã®æ¹ã«å¤§ããè¦ç´ ãæ¥ãã®ã¯é
åã®å¾ãã®æ¹ã«å¤§ããè¦ç´ ãæ¥ãã®ã¨åããªã®ã§å¤§å°ã®æ¡ä»¶ã¯éã§ã¯ãªãããã¨ããã®ããããã¾ã
\(i=2\)ã®ã«ã¼ãã§ã¯ãæåã[2, 9, 5, 7, 1, 7]
ã§
i=2, j=0, [2, 9, 5, 7, 1, 7]
i=2, j=1, [2, 5, 9, 7, 1, 7]
以ä¸ç¥
\(i=3\)ã®ã«ã¼ãã§ã¯ãæåã[2, 5, 9, 7, 1, 7]
ã§
i=3, j=0, [2, 5, 9, 7, 1, 7]
i=3, j=1, [2, 5, 9, 7, 1, 7]
i=3, j=2, [2, 5, 7, 9, 1, 7]
以ä¸ç¥
\(i=4\)ã®ã«ã¼ãã§ã¯ãæåã[2, 5, 7, 9, 1, 7]
ã§
i=4, j=0, [1, 5, 7, 9, 2, 7]
i=4, j=1, [1, 2, 7, 9, 5, 7]
i=4, j=2, [1, 2, 5, 9, 7, 7]
i=4, j=3, [1, 2, 5, 7, 9, 7]
以ä¸ç¥
æ®ãã¯åãæããªã®ã§çç¥ãã¾ãã
ä¸ã§è©¦ãã¦ã¿ãçµæã®ããã«ããã®ã¢ã«ã´ãªãºã ã§ã¯\(i\)ã®ã«ã¼ããçµãã£ãæç¹ã§ã¯å
é ãã\(i\)çªç®ã¾ã§ã®è¦ç´ ã«ã¤ãã¦ã次ã®è¦ç´ ã®æ¹ã大ãããããã¯çããç¶æ
ãã¤ã¾ãæé ã®ç¶æ
ã«ãªãã¾ãã
ãªã®ã§æå¾ã¾ã§\(i\)ã®ã«ã¼ããè¡ãã¨ãã¹ã¦ã®è¦ç´ ãæé ã«ãªããã¨ãè¨ãã¾ã(è«æã«ã¯è¨¼æãããã¾ã)
æåã大éæã«ç解ããããã«\(i\)ã«é¢ããããããã®ã«ã¼ãå¾ã®é åãã¿ãã¨ã\(i=1\)以éã®ã«ã¼ãã§ã¯å®è³ªçã«ã¯æ¿å ¥ã½ã¼ãã¨åãæåã«ãªã£ã¦ãããã¨ããããã¾ãã
i=0, [9, 2, 5, 7, 1, 7]
i=1, [2, 9, 5, 7, 1, 7]
i=2, [2, 5, 9, 7, 1, 7]
i=3, [2, 5, 7, 9, 1, 7]
i=4, [1, 2, 5, 7, 9, 7]
i=4, [1, 2, 5, 7, 7, 9]
æ¿å
¥ã½ã¼ãã§ã¯\(i\)çªç®ã®è¦ç´ ããã以åã®è¦ç´ ã¨è¦æ¯ã¹ã¦é©åãªä½ç½®ã«æ¿å
¥ãã¦ãã以éã®è¦ç´ ãä¸ã¤å¾ãã«ãããã¾ãã
ãã®ã¢ã«ã´ãªãºã ã§ã¯\(i\)çªç®ã®è¦ç´ ã¨ã®äº¤æã使ã£ã¦ãæ¿å
¥ã½ã¼ãã§ããã¨ããã®æ¿å
¥ãã¦è¦ç´ ãä¸ã¤ãã¤å¾ãã«ãããæä½ãå®ç¾ãã¦ãããã¨ã«ãªãã¾ãã
挿入ソート - Wikipedia
ãªã®ã§ã³ã¼ãããã¯æ³åãã¥ããã§ãã\(i=0\)ã®ã«ã¼ãã§æ大å¤ãå é ã«æã£ã¦ãã¦\(i=1\)以éã®ã«ã¼ãã§ã¯æ¿å ¥ã½ã¼ãã®ãããªãã¨ããã¦ããã¨ããæåã¨ãã¦ã¯çµæ§ãããããããã¨ããã¦ãããã¨ããããã¾ãã
ã³ã¼ããåæ²
def i_cant_believe_it_can_sort(A: list): n = len(A) for i in range(n): for j in range(n): if A[i] < A[j]: A[i], A[j] = A[j], A[i] return A
ãã¾ã
è«æä¸ã«ã¯It is difficult to imagine that this algorithm was not discovered before,
ã¨æ¸ããã¦ãã¾ãããæ¤ç´¢ãªã©ã使ã£ã¦èª¿ã¹ã人ã«ããã¨ä»åã®ã½ã¼ãã¢ã«ã´ãªãºã ã¨åããã®ã®å®è£
ä¾ã質åãçµæ§è¦ã¤ãããããã§ã
but we are unable to find any references to it.
Some links to discussions and/or accidental discoveries of this algorithm: OP (2... | Hacker News