資工期中超渡團
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    --- title: Alg chapter 2 --- # Getting Started * Sorting problem * Input: A list of n numbers * Output: Arrange the numbers in increasing order * If the list is already sorted, we can search a number in the list faster. ## 2.1 Insertion Sort * A good algorithm for sorting a small number of elements. ![](https://i.imgur.com/6DEFJxE.png) ![](https://i.imgur.com/GZ3SLer.png) ### Correctness of Insertion sort * Loop invariant(insertion sort as an example): * Subarray $A[1..j-1]$ consists of elements originally from $A[1..j-1]$, but in sorted order$ * Three properties for Loop Invariant: * Initialization: It is true prior to the first iteration of the loop * Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration * Termination: When the loop terminates, array is sorted. * If the firs two properties hold, loop invariant is true prior to every iteration of the loop.r ### Analyzing the Running Times * Standard assumption: Our computer is a RAM (Random Access Machine), so that each arithmetic (such as +, −, $\times$, $\div$), memory access, and control (such as conditional jump, subroutine call, return) takes constant amount of time * Suppose that our algorithms are now described in terms of RAM operations * we can count # of each operation used * we can measure the running time! * Running time is usually measured as a function of the input size * E.g., n in our sorting problem ### Insertion sort(Running time) * The following is a pseudo-code for Insertion Sort. Each line requires constant RAM operations. ![](https://i.imgur.com/S4IcfNC.png) * Let T(n) denote the running time of insertion sort, on an input of size n. * By combining terms, we have $T(n) = c_1n +(c_2+c_4+c_8)(n-1)+c_5\mathop{\sum_{}^{}}t_j+(c_6+c_7)\mathop{\sum_{}^{}}(t_j-1)$ * The values of $t_j$ are dependent on the input (not the input size) * Best: The input list is sorted, so that all $t_j=1$. Then,T(n) = $c_1n+(c_2+c_4+c_5+c_8)(n-1)=Kn+c -> linear function of n$ * worst: The input list is sorted in decreasing order, so that all $t_j = j-1$ * Then, T(n) = $k_1n^2+k_2n+k_3$ * Quadratic function of n ### Worst-Case Running Time * In our course (and in most CS research), we concentrate on worst-case time ## Divide and conquer * Divide a big problem into smaller sub-problems * Solve (Conquer) smaller sub-problems recursively * Combine the results to solve original one ### Merge sort ![](https://i.imgur.com/0QcOaiG.png) 1. Divide list to two halves, A and B 2. Sort A using Merge Sort 3. Sort B using Merge Sort 4. Merge sorted lists of A and B ![](https://i.imgur.com/5l8ISwh.png) ### Merge Sort (Running Time) * Suppose we know that Merge( ) of two lists of total size n runs in $c_1n$ time * $T(n) = 2T(n/2) + c_1n$ * when n>1 * $T(n) = c_1$ * when n=1 * Solving the recurrence, we have $T(n) = c_1\ nlogn + c_2n$ ![](https://i.imgur.com/MNVLTW9.png) #### Why? (補充) ![](https://i.imgur.com/dxB10q3.png) ![](https://i.imgur.com/nzPdjKn.png) ### which is faster * if n is VERY large, worst-case time of Merge Sort must be smaller than that of Insertion Sort # Exercises ch1 ## Exercise ### 1.2-2 * Question: * Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in $8n^2$ steps, while merge sort runs in $64n lg n$ steps. For which values of n does insertion sort beat merge sort? * Answer * $8n^2<64nlgn$ -> $n<8lgn$ * Largest n is 43 ### 1.2-3 * Question * What is the smallest value of $n$ such that an algorithm whose running time is $100n^2$ runs faster than an algorithm whose running time is $2^n$ on the same machine? * Answer * use $100n^2<2^n$ to find the largest n and plus 1 for the first $n$ to let $100n^2 > 2^n$ ## problem ### 1.1 * Question: * For each function f(n) and time t in the following table, determine the largestsize n of a problem that can be solved in time t, assuming that the algorithm tosolve the problem takes f(n) microseconds. ![](https://i.imgur.com/arbokpd.png) * answer * [Sladder Answer](https://www.slader.com/textbook/9780262033848-introduction-to-algorithms-3rd-edition/14/problems/1/) # Exercises ch2 ## Exercise ### 2.2-1 * Question * Express the function $n^3/1000 - 100n^2 - 100n + 3$ in terms of ‚$\theta$ notation * Answer * $\theta(n^3)$ ### 2.3-3 * Question * Use mathematical induction to show that when n is an exact power of 2, the solution of the recurrence $T(n)= \begin{cases}{} 2 &\text{if n=2}\\ 2T(n/2) + n&\text{if n>1} \end{cases}$. is $T(n)=nlgn$ * Answer * 假設 T(n) = nlgn * 讓 n = $2^k$ k=1 ,if n=2 T(2) = 2 = 2lg2 * 證明$n=2^{k+1}成立$ * $T(2^{K+1}) = 2T(2^{k+1}/2)+2^{k+1}\\ =2(2^klg2^k)+2^{k+1}\\=k2^{k+1}+2^{k+1}\\ =(k+1)2^{k+1}\\ =2^{k+1}lg(2^{k+1}) = nlgn$ ### 2.3-6 * question: * Observe that the while loop of lines 5–7 of the INSERTION-SORT procedure in Section 2.1 uses a linear search to scan (backward) through the sorted subarray A[1..j-1]. Can we use a binary search (see Exercise 2.3-5) instead to improve the overall worst-case running time of insertion sort to $\theta(nlgn)$? * answer: * The linear search not only scans the sub-array, but also moves the element to it's position, the worst case is $\theta(j)$. Binary search only takes $\theta(lgj)$,but we still have to move the element, which takes$\theta(j)$, so this part still costs $\theta(j)$, therefore the worst case isn't improved ## problem ### 2-1 * Although merge sort runs in $\theta(nlgn)$ worst-case time and insertion sort $\theta(n^2)$ worst-case time, the constant factors in insertion sort can make it faster in practice for small problem sizes on many machines. Thus, it makes sense to coarsen the leaves of the recursion by using insertion sort within merge sort when subproblems become sufficiently small. Consider a modification to merge sort in which n/k sublists of length k are sorted using insertion sort and then merged using the standard merging mechanism, where k is a value to be determined. * a. Show that insertion sort can sort the n/k sublists, each of length k, in $\theta(nk)$ worst-case time * Sorting each of the sublists will take$\theta(k^2)$. Sorting n/k lists will take $\theta(n/k \times k^2) = \theta(nk)$ * b. Show how to merge the sublists in $\theta(nlg(k/n))$ worst-case time. * With a coarseness of k. Merge sort is conducted above the length k.The depth of the merge tree is $lg(n)-lg(k)=lg(n/k)$. Merging on each level still takes cn time, therefore the merge sort part takes $\theta(nlg(k/n))$ * c. Given that the modified algorithm runs in $\theta(nk+nlg(n/k))$ worst-case time, what is the largest value of k as a function of n for which the modified algorithm has the same running time as standard merge sort, in terms of $\theta$ notation? * if $k(n) \in \theta(lgn)$, time complexity will still be$\theta(nlgn)$ * d. How should we choose k in practice? ###### tags: `Algorithm` `CSnote`

    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