Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Swift/Swift BASIC

[Swift] Swift์˜ sort() ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•˜์—ฌ

๊ฐ์ž ๐Ÿฅ” 2023. 3. 10. 18:36
๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ์‹œ์ž‘ํ•˜๋ฉฐ

์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ sort()์— ๋Œ€ํ•ด์„œ ๊ถ๊ธˆํ•ด์กŒ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ ์ •๋ ฌ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค. ์ฝ”ํ…Œ์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„ ์ •๋ ฌ์„ ์›ํ•  ๋•Œ Sort() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ •๋ ฌ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹จ์ˆœํžˆ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€์•ผ๋งŒ ํ•˜๋Š” ๊ฐœ๋…์ธ๊ฑด์ง€, ์•„๋‹ˆ๋ฉด ๊ตฌํ˜„๊นŒ์ง€ ๋ชจ๋‘ ์™„๋ฒฝํ•˜๊ฒŒ ํ•  ์ค„ ์•Œ์•„์•ผํ•˜๋Š”๊ฑด์ง€?
 

โšซ๏ธ Swift์˜ sort() ๋‚ด๋ถ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณ€ํ™”

โ–ช๏ธ 2018๋…„ ์ด์ „

Swift๋Š” 2018๋…„ sort์˜ ๋‚ด๋ถ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ต์ฒด๊ฐ€ ๋๋‹ค. ์›๋ž˜๋Š”, introsort๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค.

introSort

introsort๋ž€ ํ‰๊ท ์ ์œผ๋กœ ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ๋‚ด๋ฉด์„œ ์ตœ์•…์˜ ์ƒํ™ฉ์—์„œ๋„ ์ ์ง„์ ์œผ๋กœ ์ตœ์ ํ™”๋œ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. C++ STL์—์„œ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ์ •๋ ฌ ํ•จ์ˆ˜์ด๋‹ค.
intro ์ •๋ ฌ์€ ํž™์ •๋ ฌ, ํ€ต์ •๋ ฌ, ์‚ฝ์ž…์ •๋ ฌ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํ€ต์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ํ€ต์ •๋ ฌ์€ ์ตœ์•…์˜ ๊ฒฝ์šฐ์— ๋Š๋ ค์ง€๊ฒŒ ๋˜๊ณ , ์ด ๋‹จ์ ์„ ๋ณด์™„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ฐ”๋กœ introSort๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.
์ตœ์•…์˜ ๊ฒฝ์šฐ ๋‹จ์ ์„ ๋ณด์™„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋„ ์‹œ๊ฐ„๋ณต์žก๋„ O(nlogn)์„ ๋ณด์žฅํ•œ๋‹ค.

introsort ๋™์ž‘๋ฐฉ์‹

  1. ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๊ฐ€ 16์ดํ•˜์ธ ๊ฒฝ์šฐ๋ผ๋ฉด, ์‚ฝ์ž…์ •๋ ฌ์„ ์ฑ„ํƒํ•œ๋‹ค.
  2. ์ „์ฒด ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด์„œ ํ€ต์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  3. (ํ€ต ์ •๋ ฌ์€ ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n^2) โ†’ ๋ถ„ํ•  ํšŸ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๋ฉด ์„ฑ๋Šฅ์ด ์ €ํ•˜, ๋ถ„ํ•  ํšŸ์ˆ˜๊ฐ€ ํŠน์ • ์ž„๊ณ„์น˜์— ๋„๋‹ฌํ•˜๋ฉด sort ๋ฐฉ์‹์„ ์Šค์œ„์นญ ํ•˜๋Š” ๋ฐฉ์‹์„ ์ฑ„ํƒ โ†’ ๊ทธ๋ž˜์„œ ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋„ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(nlogn)์ž„)
  4. ์ˆ˜ํ–‰ ๋„์ค‘ ์žฌ๊ท€ ํ˜ธ์ถœ์˜ ๊นŠ์ด๊ฐ€ 2logn์„ ๋„˜์–ด๊ฐ€๋ฉด 4๋ฒˆํ•ญ๋ชฉ(ํž™์ •๋ ฌ๋กœ ์Šค์œ„์นญ)์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
  5. ์ชผ๊ฐœ์ง„ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๊ฐ€ 16์ดํ•˜๋ผ๋ฉด ๊ทธ๋Œ€๋กœ ๋†”๋‘”๋‹ค.
  6. 16๋ณด๋‹ค ํฌ๋ฉด, ํ•ด๋‹น ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ํž™์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  7. 3, 4๋ฒˆ ํ•ญ๋ชฉ์ด ๋ชจ๋‘ ์™„๋ฃŒ๋œ ํ›„, ๋Œ€๋ถ€๋ถ„ ์ •๋ ฌ์ด ๋œ ์ „์ฒด ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด์„œ ์‚ฝ์ž…์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

๋ญ ๋Œ€์ถฉ,, ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ •๋ ฌ ๋ฐฉ์‹์˜ ์žฅ์ ๋งŒ ๊ณจ๋ผ์„œ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์•Œ๊ณ  ์žˆ์–ด๋ณด์ž. (์•„์ง ์ž˜ ์–ด์ผ€ ๋™์ž‘ํ•œ๋‹ค๋Š”์ง€ ์ดํ•ด๋Š” ์•ˆ๋˜์ง€๋งŒ,,)
 

โ–ช๏ธ 2018๋…„ 10์›” ์ดํ›„

Swift์˜ sort ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด Timsort๋กœ ๊ต์ฒด๋๋‹ค.

https://github.com/apple/swift/pull/19717

[stdlib] Switch to a stable sort algorithm by natecook1000 ยท Pull Request #19717 ยท apple/swift

This switches the standard library's sort algorithm from an in-place introsort to use a modified timsort, a stable, adaptive sort that merges runs using a temporary buffer. This implementation perf...

github.com

 
ํ•ด์„ํ•ด๋ณด์ž๋ฉด,

This switches the standard library's sort algorithm from an in-place introsort to use a modified timsort, a stable, adaptive sort that merges runs using a temporary buffer.

ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ in-place introsort์—์„œ ์ž„์‹œ buffer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰์„ ๋ณ‘ํ•ฉํ•˜๋Š” ์•ˆ์ •์ ์ด๊ณ  ์ ์‘์ ์ธ(?) ์ •๋ ฌ์ธ modified timsort๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ „ํ™˜ํ•œ๋‹ค.
(in-place sorting์ด๋ž€, ์ œ์ž๋ฆฌ ์ •๋ ฌ์„ ์˜๋ฏธํ•˜๋Š”๋ฐ, ์ž„์‹œ๋ฐฐ์—ด ํ•„์š” ์—†์ด ๋ฐฐ์—ด ๋‚ด๋ถ€์—์„œ ์ •๋ ฌ์„ ํ•˜๋Š” ๊ฒƒ์„ ์ œ์ž๋ฆฌ์ •๋ ฌ์ด๋ผ๊ณ  ํ•˜๋Š”๊ฐ€๋ณด๋‹ค. ์ž„์‹œ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๋‹ค๋ฉด, inplace ์ •๋ ฌ์ด ์•„๋‹˜! (์ฐธ๊ณ  - https://gmlwjd9405.github.io/2018/05/08/algorithm-merge-sort.html)

In addition to maintaining the relative order of equal/non-comparable elements, this algorithm outperforms the introsort on data with any intrinsic structure, such as runs of ascending or descending elements or a significant number of equality collisions.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ equal / non-comparable ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ (stable, ์•ˆ์ •์ •๋ ฌ์ด๋ผ๋Š” ๋œป์ธ๊ฑฐ๊ฐ™์Œ) ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ ์š”์†Œ ์‹คํ–‰ / equality collisions์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์—์„œ introsort๋ฅผ ํ›จ์”ฌ ๋Šฅ๊ฐ€ํ•œ๋‹ค.
๋ญ ์–ด์จŒ๋“ , introsort๋ณด๋‹ค๋Š” ๋” ์ข‹์€ ์„ฑ๋Šฅ์˜ timsort๋ฅผ ์ด์šฉํ•˜๊ฒŒ๋” ๋ฐ”๊ฟจ๋‹ค๋Š” ๋ง์ด๋‹ค. ์Œโ€ฆ ์ง€๊ธˆ ๋‚˜์˜ ์ถ”์ธก์œผ๋กœ๋Š” ํ€ต์ •๋ ฌ, ํž™์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” introsort๋Š” ๋ถˆ์•ˆ์ •์ •๋ ฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ •์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋„๋ก ๋ฐ”๊ฟจ๋‹ค! ๊ทธ๋ž˜์„œ ๋ญ ์ด๋Ÿฐ์ด๋Ÿฐ ์ถฉ๋Œ ์ด๋Ÿฐ๊ฒƒ์—์„œ ๋Šฅ๊ฐ€ํ•œ๋‹ค. ์ด๋Ÿฐ ๋Š๋‚Œ์“ฐ ์ธ๊ฒƒ ๊ฐ™์•˜๋‹ค.
(๋ถˆ์•ˆ์ • ์ •๋ ฌ / ์•ˆ์ •์ •๋ ฌ ์ฐธ๊ณ ์ž๋ฃŒ https://velog.io/@good159897/์•ˆ์ •-์ •๋ ฌ-VS-๋ถˆ์•ˆ์ •-์ •๋ ฌ-ํŒŒ์ด์ฌ-์•Œ๊ณ ๋ฆฌ์ฆ˜-์ธํ„ฐ๋ทฐ)

 

โšซ๏ธ timsort

โ–ช๏ธ timsort๊ฐ€ ๋ญ˜๊นŒ

timsort๋Š” ์‚ฝ์ž…์ •๋ ฌ๊ณผ ๋ณ‘ํ•ฉ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ด ๋‘๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์•Œ์•„๋ณด์ž.

Insertion sort (์‚ฝ์ž…์ •๋ ฌ)

 
์‚ฝ์ž…์ •๋ ฌ์€ ๋ฐฐ์—ด์˜ ์ผ๋ถ€๋ฅผ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ, ๋‹ค์Œ ์›์†Œ๊ฐ€ ์žˆ์–ด์•ผํ•  ์œ„์น˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด์„œ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์•„๋ž˜ ๋‚ด๊ฐ€ ์ž˜ ์ •๋ฆฌํ•ด๋‘์—ˆ์œผ๋‹ˆ, ์‚ดํŽด๋ณด์ž.
https://desert-opossum-095.notion.site/b962ec3c29fb45aab515af5765e7c784

์‚ฝ์ž…์ •๋ ฌ

Contents

desert-opossum-095.notion.site

Merge sort (๋ณ‘ํ•ฉ์ •๋ ฌ)

merge sort๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์ž‘์€ ํฌ๊ธฐ๋กœ ์ชผ๊ฐœ์„œ ์ •๋ ฌํ•œ ๋’ค์—, ์ด๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํ•ฉ์น˜๋Š” ์ •๋ ฌ ๋ฐฉ์‹์ด๋‹ค. ๋ถ„ํ• ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ์ฃผ๋กœ ์ ˆ๋ฐ˜์”ฉ ์ชผ๊ฐœ์„œ ํ•ฉ์น˜๋Š” ๋ถ„ํ•  ์ •๋ณต ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.

https://jcsoohwancho.github.io/2019-11-20-Swift%EC%9D%98-%EA%B8%B0%EB%B3%B8-%EC%A0%95%EB%A0%AC-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-Timsort/

๋ณ‘ํ•ฉ์ •๋ ฌ์€ ๋ถ„ํ• ์„ ํ•  ๋•Œ๋งˆ๋‹ค ๋ฌธ์ œ ํฌ๊ธฐ๊ฐ€ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์–ด๋“ ๋‹ค. ๋Œ€์‹ , ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜๋Š” 2๋ฐฐ๋กœ ๋Š˜์–ด๋‚˜๊ฒŒ ๋œ๋‹ค. (๋ณ‘ํ•ฉํ•ด์•ผํ•˜๋Š” ๋ฐฐ์—ด์ด 2๋ฐฐ์”ฉ ๋Š˜์–ด๋‚œ๋‹ค๋Š” ๋œป) ์ด ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค์„ O(1) ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์„ ๋•Œ ๊นŒ์ง€ (์›์†Œ๊ฐ€ 1๊ฐœ์ผ๋•Œ๊นŒ์ง€) ์ˆ˜ํ–‰๋˜๊ณ , ์ด ๋ฌธ์ œ๋“ค์€ O(N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•ฉ์ณ์ง„๋‹ค. ์ด ํ•ฉ์ณ์ง€๋Š” ๊ณผ์ •์€ ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜์— ๋Œ€ํ•ด์„œ ๋น„๋ก€ํ•ด์„œ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(nlogn)์ด ๋œ๋‹ค.
๋ณ‘ํ•ฉ์ •๋ ฌ์€ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ์˜ ์˜ํ–ฅ์„ ๋œ ๋ฐ›๋Š” stableํ•œ ์ •๋ ฌ ๋ฐฉ๋ฒ•์ด๋‹ค. ์ฆ‰, ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌด์—‡์ด๋“  ๊ฐ„์—, ์ •๋ ฌ๋˜๋Š” ์‹œ๊ฐ„์€ O(nlogn)์œผ๋กœ ๋™์ผํ•˜๋‹ค.
์‹œ๊ฐ„๋ณต์žก๋„ ์ฆ๋ช…์€ ์ด ๊ธ€์„ ์ฐธ๊ณ ํ•˜์ž https://gmlwjd9405.github.io/2018/05/08/algorithm-merge-sort.html

โ–ช๏ธ timsort

์œ„์—์„œ ์„ค๋ช…ํ•œ ๋‘ ๊ฐ€์ง€์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.
Run ์ด๋ผ๋Š” ๋‹จ์œ„๋กœ ๋ฐฐ์—ด์„ ๋ถ„ํ• ํ•˜๊ณ , ์ด ๋ถ„ํ• ๋œ ๋‹จ์œ„์— ๋Œ€ํ•ด์„œ ์‚ฝ์ž…์ •๋ ฌ์œผ ์ˆ˜ํ–‰ํ•œ ๋’ค, ์ด๋ฅผ ํ•ฉ์ณ์„œ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. (๋ณ‘ํ•ฉ์ •๋ ฌ)

๋™์ž‘ ๋ฐฉ์‹

  1. minRun ์ƒ์ˆ˜ ๊ตฌํ•˜๊ธฐ - ๋ฐฐ์—ด์„ ๋ถ„ํ• ํ•  ํฌ๊ธฐ ๋‹จ์œ„๋ฅผ ์ •ํ•˜๋Š” ๊ณผ์ •
  2. run ๋งŒ๋“ค๊ธฐ - ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํฌ๊ธฐ๋กœ ๋ฐฐ์—ด์„ ๋ถ„ํ• ํ•˜๋Š” ๊ณผ์ •
    • ๋ถ„ํ• ํ•œ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ minRun๋ณด๋‹ค ์ž‘์œผ๋ฉด, minRun์ด ๋˜๋„๋ก ๋ฐฐ์—ด์„ ํ™•์žฅํ•œ๋’ค, ์‚ฝ์ž…์ •๋ ฌ๋กœ ์ •๋ ฌ
    • ํ™•์žฅ์ด์œ ? ์ตœ๋Œ€ํ•œ ์ ์€ ํšŸ์ˆ˜๋กœ mergeํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํฌ๊ธฐ๊ฐ€ ๋™์ผํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ!
    • ์ดํ›„ ์ด run๋“ค์„ stack์— ๋„ฃ๋Š”๋‹ค.
  3. merge ํ•˜๊ฑฐ๋‚˜, ๋‹ค์Œ run ๊ตฌํ•˜๊ธฐ - stack์— ์Œ“์ธ run๋“ค์„ ๋จธ์ง€ํ• ์ง€๋ง์ง€ ๊ฒฐ์ •ํ•ด์•ผํ•œ๋‹ค.
    • ์Šคํƒ์˜ ์œ„์—์„œ๋ถ€ํ„ฐ 4๊ฐœ์˜ run์„ x, y, z, w ๋ผ๊ณ  ํ•˜๋ฉด
      • ์Šคํƒ์˜ ํฌ๊ธฐ๊ฐ€ 4 ์ด์ƒ์ธ ๊ฒฝ์šฐ |w| > |Z| + |Y|
      • ์Šคํƒ์˜ ํฌ๊ธฐ๊ฐ€ 3์ผ ๊ฒฝ์šฐ |Z| > |X| + |Y|
      • ์Šคํƒ์˜ ํฌ๊ธฐ๊ฐ€ 2์ผ ๊ฒฝ์šฐ |Y| > |X|
    • ๋งŒ์•ฝ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด, Y๋ฅผ X์™€ Z์ค‘ ์ž‘์€์ชฝ๊ณผ ํ•ฉ์นจ
    • ์œ„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๊ฑฐ๋‚˜, stack์˜ ๊ธธ์ด๊ฐ€ 1์ด ๋˜๋ฉด merge๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ๋‹ค์Œ run์„ ๊ตฌํ•จ

์‹œ๊ฐ„๋ณต์žก๋„

timsort๋Š” ๋ฐฐ์—ด์ด ๋ฌด์ž‘์œ„๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ์ ์—์„œ ์ •๋ ฌ์„ ์ตœ์ ํ™”ํ•œ ์ข‹์€ ์˜ˆ๋กœ, ์ตœ์„  O(N), ์ตœ์•… O(nlogn)์„ ๋ณด์žฅํ•œ๋‹ค.
(์ฐธ๊ณ  - https://jcsoohwancho.github.io/2019-11-20-Swift์˜-๊ธฐ๋ณธ-์ •๋ ฌ-์•Œ๊ณ ๋ฆฌ์ฆ˜-Timsort/)

 

โšซ๏ธ swift sort ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์„œ ๋ณผ ์ˆ˜ ์žˆ์Œ

์ง€๊ธˆ ํ•ด์„ํ•ด๋ณด์ง„ ์•Š์„๊ฑฐ๊ณ  ์‹œ๊ฐ„๋˜๋ฉด ์‚ดํŽด๋ณด๊ธฐ..
https://github.com/apple/swift/blob/main/validation-test/stdlib/Sort.swift.gyb

GitHub - apple/swift: The Swift Programming Language

The Swift Programming Language. Contribute to apple/swift development by creating an account on GitHub.

github.com

 
 

๐Ÿ”– ์œ„์—์„œ ์ž‘์„ฑํ•˜์ง€ ์•Š์€ ์ถ”๊ฐ€์˜ ์ฐธ๊ณ ์ž๋ฃŒ 

https://brunch.co.kr/@joonwonlee/8
https://justicehui.github.io/medium-algorithm/2019/03/24/IntroSort/

๋ฐ˜์‘ํ˜•