will
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: linux kernel class --- # 2023q1 Homework4 (quiz4) contributed by < `willwillhi1` > ## 測驗 `2` `Timsort` 是 **Insertion Sort** 和 **Merge Sort** 的結合,特點是速度快.在最佳狀況只有 $O(n)$,平均和最糟狀況也有 $O(n log (n))$。 整個做法的原理是先把資料分成小區塊(又可以叫做 `run`),一般來說範圍會在 `32 ~ 64` 之間,然後把小區塊用 `insertion sort` 排序,再把這些區塊用 `merge sort` 合併起來。 參考 [Wikipedia: Timsort](https://en.wikipedia.org/wiki/Timsort) > Timsort was designed to take advantage of runs of consecutive ordered elements that already exist in most real-world data ,如果陣列是由多個部分排序的數列所組成表現較好,但是如果在 `random array` 表現會較差,因為 `insertion sort` 在有排序的數列上是 `best case`。 ``` /* random array */ timsort: 171.0 us qsort: 103.0 us ``` #### Insertion sort * 在 `next_partition` 裡執行,每次先從 `first` 開始往後找有排序的數列。覺得原本程式會違反 `stable` 的特性(?,因為 `reverse` 的實作是頭尾兩兩交換。 * 原本的實作如下,但是考慮到這個例子 [5, 4~1~, 4~2~ , 4~3~, 3, 6],此時要做 `reverse` 前,`first` 指向 `5`,`next` 指向 `6`,做了 `reverse` 後會變成 [3, 4~3~, 4~2~ , 4~1~, 5, 6]。 ```c= if (cmp(cur, next)) { while (next < last && cmp(cur, next)) { ++run_len; cur += size; next += size; } } else { while (next < last && !cmp(cur, next)) { ++run_len; cur += size; next += size; } __reverse(first, next, size); } ``` * 然後判斷找到的數列長度是大於或小於 `MIN_RUN`,如果不到 `MIN_RUN` 個代表這個大小適合用插入排序法,就往後取到 `MIN_RUN` 個後做 `__insertion_sort`。 ```c= last = first + MIN_RUN * size < last ? first + MIN_RUN * size : last; ``` #### Merge 規則 * 為了達到 `balanced merge`,`timsort` 會依照以下的規則來判斷是否要執行合併,`run_length` 回傳該 `run` 長度。 ```c= run_length(stack[top]) > run_length(stack[top - 1]) run_length(stack[top]) + run_length(stack[top - 1]) > run_length(stack[top - 2]) ``` * `Timsort` 是 `stable` 排序演算法,所以在 `merge` 時只能找相鄰的 `run` 合併。 #### Minimum run size > merging is most efficient when the number of runs is equal to, or slightly less than, a power of two 如果陣列是完全隨機的,在 `merge` 時會希望可以平衡,也就是分成略小於等於 2 的冪個 `run`,這麼一來可以避免需要合併兩個大小相差很大的 `run`。 以下說明如何計算出適當的 `Minimum run size` 來讓 `run` 符合條件。 計算 `Minimum run size`: 目的是要讓整個陣列依照 `min run size` 被分成略小於或等於二的冪個。 一般來說 `run_size` 會在 `32 ~ 64` 之間, 整體的演算法是取整個陣列長度的前 6 位,之後再加上剩餘的部分的 `1` 的個數即可。 ```c= 假設 array size = 2156 = 0b100001101100 取前六位 = 0b100001 = 33 再加上剩餘的 set bits: 0b101100,總共 3 bits 所以 min run size = 33 + 3 = 36 2156/36 = 59,略小於 64 ``` #### Merge * 一般的 `linear merge`,需要用到額外的記憶體。 * `__inplace_merge`: * Merge two runs: `[1, 2, 3, 6, 10]` and `[4, 5, 7, 9, 12, 14, 17]` * 將 `cur1` 指向大於 `first2` 的第一個數,接著將 `cur2` 指向大於 `cur1` 的第一個數 ```c= while (cur_1 < last_2 && cmp(cur_2, cur_1) >= 0) cur_1 += size; while (cur_2 < last_2 && cmp(cur_2, cur_1) < 0) cur_2 += size; ``` ![](https://i.imgur.com/S8SXsmv.png) * `__reverse(start, end, size)` 的效果是把 `start` 到 `end-1` 的數字反轉 * ` __reverse(cur_1, first_2, size)` * `__reverse(first_2, cur_2, size);` ![](https://i.imgur.com/QwBtN8I.png) * 最後再反轉 `cur1 ~ cur2` * `__reverse(cur_1, cur_2, size);` ![](https://i.imgur.com/Bmqiapq.png) * 這麼一來就排完 `first1 ~ cur1` + `first2 ~ cur2 的兩個數列`,所以新的 * `first_1 = cur_1 + (cur_2 - first_2)` * `first_2 = cur_2` * 接下來以此類推持續下去,直到 `cur1 == last2` 或 `cur_1 > first_2`。 * 值得一提的是,再找 `cur1` 和 `cur2` 的位置時,原本的程式是用 `linear search`。 ```c= while (cur_1 < last_2 && cmp(cur_2, cur_1) >= 0) cur_1 += size; while (cur_2 < last_2 && cmp(cur_2, cur_1) < 0) cur_2 += size; ``` * 但因為是在一個已排序的陣列找所以可以用 `binary search` 來加速,不過在 [Wikipedia: timsort](https://en.wikipedia.org/wiki/Timsort) 裡有提到是用另一個演算法 `expotential search` 來找的。 * expotential search 簡單介紹 : * 與 `binary search` 最主要的差異在於不是直接選擇陣列中間的元素來比較,而是依序去比較索引為 2^i^ 的元素,`i` 從 `0` 開始,可以分為以下情況 1. 如果要查找的元素比索引為 2^i^ 的元素大,就繼續往下一個索引 2^i+1^ 比對。 2. 如果比索引為 2^i^ 的元素小,就可以用二分搜尋法在 2^i-1^+1 ~ 2^i^-1 的範圍內找我們要找的元素。 #### 參考資料 * [Wikipedia: timsort](https://en.wikipedia.org/wiki/Timsort)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully