蔣立元
    • 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

      This note has no invitees

    • 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
    • Note Insights
    • 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 Note Insights 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

    This note has no invitees

  • 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
    # 基礎排序 2020竹C, jeeeerrrpop --- ## 生活中的例子 - 一堆散落的紙有頁碼,我們想把它重新排成一本書。 - 要把學生的成績由大到小排序。 - ........ --- {%youtube WaNLJf8xzC4%} --- 給你一個長度為n的序列 你要把他們由小到大排序 --- ## Swap 先介紹基於比較、交換的演算法。 ```python= a, b = b, a ``` --- 何謂演算法? 我們能自己想到排序演算法嗎? --- ## Bubble Sort - 從序列的最前面開始,一次比較陣列中兩兩相鄰的元素,然後根據將大的移到後面。 - 當我們比較過所有元素一次後,可以確保數值最大的元素在最後面。 - 接著扣掉陣列中的最後一個元素,接著重複上面的步驟進行兩兩比較。 --- ## Bubble Sort ```cpp void BubbleSort(int arr[], int n) { for(int i = 0; i < n - 1; i++) { for(int j = 0; j < n - i - 1; j++) { if(arr[j] > arr[j + 1]) swap(arr[j], arr[j + 1]); } } } ``` --- ## Selection Sort - 在序列中找到最小的元素,並將他與第一個元素交換。 - 在序列中找到第二小的元素(也就是撇除掉第一個元素後的最小的元素),並將他與第二個交換。 - 重複以上操作,直到排序好。 --- ## Selection Sort ```cpp void SelectionSort(int arr[], int n) { for(int i = 0; i < n - 1; i++) { int min = i; for(int j = i + 1; j < n; j++) { if(arr[j] < arr[min]) min = j; } swap(arr[i], arr[min]); } } ``` --- ## Insertion Sort - 想像序列中有一部分是已經排序好的,那每次新增一個元素就去看他要插在排序好的序列的哪裡。 - 對於陣列中第 i 個值,假設 0 ~ i - 1 都排序好了,把 i 往左比較找到第一個小於他的元素並插在他右邊。 --- ## Insertion Sort ```cpp void InsertionSort(int arr[], int n) { for(int i = 1; i < n; i++) { int j = i - 1; while (j >= 0 && arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); j--; } } } ``` --- ## Quick Sort [Quick Sort](http://alrightchiu.github.io/SecondRound/comparison-sort-quick-sortkuai-su-pai-xu-fa.html) --- ## Merge Sort [Merge Sort](http://alrightchiu.github.io/SecondRound/comparison-sort-merge-sorthe-bing-pai-xu-fa.html) --- ## 複雜度分析 - Bubble Sort - Best: $O(n^2)$, Worst $O(n^2)$ - Selection Sort - Best: $O(n^2)$, Worst $O(n^2)$ - Insertion Sort - Best: $O(n)$, Worst $O(n^2)$ - Quick Sort - Worst $O(n^2)$, Average $O(nlogn)$ - Merge Sort - $O(nlogn)$ --- Bubble Sort 若在迴圈執行時,判斷這一輪是否都沒有任何元素交換,可以把最優情況下的複雜度降低到 $O(n)$。 Insertion Sort 若把內層迴圈改成二分搜,就可以優化成 $O(nlogn)$。 --- 有複雜度比 $O(nlogn)$ 更好的排序方法嗎? --- 基於比較的排序演算法最好就是 $O(nlogn)$ [優質文章](https://tmt514.github.io/algorithm-analysis/sorting/comparison-based-sorting.html#定理-18) --- ## 不基於比較的排序方法 - Counting Sort - Bucket sort - radix sort --- ## Counting Sort - 假如數值介於 0 ~ 999,我們就開一個長度為1000的陣列去記每一種數字出現幾次。 - 最後由小到大輸出 --- ## Counting Sort ```cpp const int size = 1000; void CountingSort(int arr[], int n) { int cnt[size] = {0}; for(int i = 0; i < n; i++) { cnt[arr[i]]++; } int id = 0; for(int i = 0; i < size; i++) { while(cnt[i] > 0) { arr[id] = i; id++; cnt[i]--; } } } ``` --- ## Counting Sort 時間複雜度是 $O(n)$ 嗎? --- ## Counting Sort k 為數字範圍 時間複雜度 $O(n + k)$ --- ## 上課練習 [neoj 369](https://neoj.sprout.tw/problem/369/) --- 既然都有 $O(nlogn)$ 的排序演算法了,那些$O(n^2)$的演算法根本不會被用到吧? --- 當序列長度不大的時候,Insertion Sort等等反而會比Quick Sort, Merge Sort還來得有效率。 [reference](http://alrightchiu.github.io/SecondRound/comparison-sort-insertion-sortcha-ru-pai-xu-fa.html) --- 我不想自己寫SortQQ [std::sort](http://www.cplusplus.com/reference/algorithm/sort/) --- ```cpp #include <algorithm> int a[1000]; std::sort(a, a + 5); ``` --- 酷酷的影片 {%youtube kPRA0W1kECg%} --- Homework [neoj 2219](https://neoj.sprout.tw/problem/2219/)

    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