Tazmilur Saad
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Topic 3: Sorting and Selection ## Problem 1 Describe and analyze (correctness and runtime) an efficient algorithm for the following task: Given $k$ sorted arrays, each of length $n$, combine them into a single sorted array of all $kn$ items. ### Solution: We create $k$ different queues where each queue consists of one of the $n$ length arrays. We then create an empty $n \cdot k$ length array to fill up. We then compare the top element of each of the $k$ queues, and then pop the smallest element from this comparison and put it in into the array. Since we have $k$ queues and each queue has one top element, finding the minimum from the top level elements will involve $k$ comparisons, assuming all of the queues are filled. We then repeat this until each queue is flushed of all of its elements. If all of the queues are flushed at the end, the runtime of the algorithm is $O(k^{2}n)$. The space complexity is $O(k)$ to store the pointers. ## Problem 2 You have a bag of $n$ nuts and $n$ bolts of close but different sizes. In this bag, each nut matches exactly one bolt. Your goal is to pair up each nut with its correct matching bolt. You cannot look at a nut or bolt and assess its size, however you can perform a test: try to attach a single bolt and nut, and you'll find that the attachment is either perfect, too loose (the nut is bigger than the bolt), or doesn't fit (the nut is smaller than the bolt). Design and analyze (in terms of correctness and runtime) an efficient algorithm to pair up the nuts and bolts. The runtime should be evaluated in terms of the number of nut-bolt tests you perform. ### Solution: ``` array bolts[n] array nuts[n] new array nutsSmallerThanBolti new array nutsLargerThanBolti for (each i in bolts, each j in nuts; i++, j++) { if (bolts[i] != nuts[j]) sortBolt(bolts[i], nuts[n]) else { if (bolts[i] < bolts[i+1]) sortBolt(bolts[i], nutsLargerThanBolti) else if (bolts[i] > bolts[i+1]) sortBolt(bolts[i], nutsSmallerThanBolti) } } helper function sortBolt(bolt, nutsArray) { for (each j in nutsArray) { if (bolt > nutsArray[j]) add nuts[j] to nutsSmallerThanBolti else if (bolt < nutsArray[j]) add nuts[j] to nutsLargerThanBolti } } ``` Analysis: Worst case run time = $n^2$ Best case run time = $n \log n$ ## Problem 3 You are given two sorted lists of size $m$ and $n$ and a parameter $k \leq m+n$. Describe and analyze an efficient algorithm to compute the $k$th smallest element in the union of the two lists. ### Solution: Description: We look at the index $\frac{k}{2}$ in both of the lists. This way the combined sublists to the left of these indices contain at most the k smallest elements. Thus, we can guarantee that the value of the kth smallest element will be *at most* one of the indices we have selected. This happens if $m[\frac{k}{2}] = n[\frac{k}{2}]$, in which case we take one of them and are done. If $m[\frac{k}{2}] \neq n[\frac{k}{2}]$, we take the left part (indices 0 to $\frac{k}{2}$) of the array where the element at index $\frac{k}{2}$ is smaller and discard it. We know we can do this because there is at least one element in the other array (the one at index $\frac{k}{2}$) which is bigger than all of them, which means that these elements are within at most the $\frac{k}{2} + (\frac{k}{2} - 1) = k-1$ smallest elements, and therefore none of them can be the nth smallest element. Similarly, we can discard the right part (indices $\frac{k}{2}$ to $m$ or $n$) of the array where the element at index $\frac{k}{2}$ is bigger (since there were $\frac{k}{2}$ elements smaller than the right side in both of the arrays, there is a total of at least $k$ smaller elements, which means that the kth smallest element is not in there). Now we have two smaller arrays left, and we do the same for $\frac{k}{4}$, and then continue until $\frac{k}{2^{log_2k}}$. Analysis: Since we shave off half of the remaining elements on each iteration, the efficiency of this algorithm will be $O(log(k))$. ## Problem 4 You are given $n$ nonvertical lines in the plane: line $L_{i}$ is described by the equation $y=L_{i}(x)=m_{i}x+b_{i}$, and you are given the slope $m_{i}$ and $y$-intercept $b_{i}$. Assume that no three lines intersect at a point. We say that line $L_{i}$ is uppermost at some $x$-coordinate $x_{0}$ if its $y$-value is greater than the $y$-value of all other lines at $x_{0}$; that is, $L_{i}$ is uppermost at $x_{0}$ if $L_{i}(x_{0}) \geq L_{j}(𝑥_{0})$ for all $j$. We say that line $L_{i}$ is visible if it is uppermost at some $x$ coordinate. (In other words: imagine you are standing at $y=\infty$ and are "looking down" at all the lines; you can see the visible ones, while the others are hidden by other lines closer to you.) Describe and analyze an efficient algorithm to determine which lines are visible. ### Solution: For the positive side --- We sort the lines based on their $m$ values. We then find the line with the highest $b$. At $x=0$, this is the line that will be visible. We set this line's gradient as the current gradient, $m_{cur}$. We then look through the sorted list of $m$ values for lines with $m$ > $m_{cur}$. We then calculate the intersection of those lines with the current line. We choose the line with the earliest intersection to be the current line at their intersection point, $x_{1}$. Thus, from $x=0$ to $x=x_{1}$, the line with the greatest $b$ will be visible so we save this in a separate array. We then set $m_{cur}$ to be the $m$ of the new line after the intersection point, set this to the current visible line, and we repeat this procedure to find a line with $m > m_{cur}$. For the negative side, we repeat this procedure but we look at $m < m_{cur}$. The runtime of this algorithm will be $O(n^2)$ at the worst case, where every line besides the one with the highest $b$ has an $m$ greater than it, and $O(nlog(n))$ in the best case.

    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