lemmy.4d2.org
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
cm0002@lemmy.world to Programmer Humor@programming.dev · 1 year ago

timeoutSort

lemmy.ml

message-square
11
link
fedilink
  • cross-posted to:
  • programmerhumor@lemmy.ml
9

timeoutSort

lemmy.ml

cm0002@lemmy.world to Programmer Humor@programming.dev · 1 year ago
message-square
11
link
fedilink
  • cross-posted to:
  • programmerhumor@lemmy.ml
alert-triangle
You must log in or register to comment.
  • Eager Eagle@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    finally, sorting in linear time /s

  • mcmodknower@programming.dev
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    I know this under the name sleepSort.

  • Tartas1995@discuss.tchncs.de
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    For better usage: you don’t need to write it into console. Just write it in an array!

  • Kairos@lemmy.today
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    Race condition

  • Jankatarch@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    This algorithm takes K seconds where K is the value of the greatest element.

    This means if you just multiply everything by -1 it will take negative time to sort.

    Then you can simply unmultiply and read from end to beginning from now on.

    This is faster than having it presorted.

  • Dyskolos@lemmy.zip
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    6 months ago

    deleted by creator

  • Nat (she/they)@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    Wait till you find out how the runtime manages multiple concurrent timers

    • sus@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      1 year ago

      it’s

      while (true) {
          let t = Date.now();
          if (timeoutMap.has(t)) timeoutMap[t]();
      }
      

      of course. Clearly O(n).

      disclaimer

      Feel free to use it. I guarantee it is bug free. Comes with express warranty. This notice is legally binding.

      • FooBarrington@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        I found a way to optimize your code without affecting the result. By making it branchless, I was able to get my CPU to 100% utilization!

  • JargonWagon@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    I’m dumb, can someone ELI5 please?

    • scrion@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      The output is sorted due to the fact that for each number, a timer is started that prints out the number after waiting a number of milliseconds equal to said number.

      Therefore, 1 is printed first after delaying for 1 millisecond, 5 is printed second after 5 milliseconds etc.

      • JargonWagon@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        Perfectly explained, thank you!

Programmer Humor@programming.dev

programmer_humor@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmer_humor@programming.dev

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics
    • If the mod doesn’t find it funny, you’re banned. Ha-ha!.. For real: do not use the community for “statements”. There are other places for such content. Keep it chill and funny.
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 986 users / day
  • 5.8K users / week
  • 9.04K users / month
  • 16.4K users / 6 months
  • 10 local subscribers
  • 33.9K subscribers
  • 1.96K Posts
  • 47.7K Comments
  • Modlog
  • mods:
  • adr1an@programming.dev
  • Feyter@programming.dev
  • BurningTurtle@programming.dev
  • Pierre-Yves Lapersonne@programming.dev
  • BE: 0.19.20
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org