• CanadaPlus@lemmy.sdf.org
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    6 hours ago

    Isn’t this basically an implementation of spaghetti sort? I’ve seriously taken the delay approach before in distributed memory situations.

    • sus@programming.dev
      link
      fedilink
      arrow-up
      10
      ·
      10 hours 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.

  • lugal@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    13
    ·
    16 hours ago

    Would this lead to problems if there are multiple identical and close by values? Like for example you have 100 elements each between 1 and 5

    • rbn@sopuli.xyz
      link
      fedilink
      arrow-up
      32
      ·
      14 hours ago

      To reduce the chance of errors, you can multiply all numbers by a factor of 10, 100, 1000, 10000, … for the timeout. The higher the factor, the lower the chances of an incorrect result. And as no one asked about performance…

      • BlueKey@fedia.io
        link
        fedilink
        arrow-up
        2
        ·
        4 hours ago

        Maybe not peak performance but heigh CPU efficency, it’s load ist mostly 0.

      • filcuk@lemmy.zip
        link
        fedilink
        arrow-up
        34
        ·
        13 hours ago

        As added benefit, you can then opyimise the code by dividing the number by 2, making it twice as fast. Think of the savings!

  • kender242@lemmy.world
    link
    fedilink
    English
    arrow-up
    21
    ·
    20 hours ago

    This is almost a bucket sort, which is practically O(n).

    (I’ll leave it to the other readers to state the trade-offs)

    • scrion@lemmy.world
      link
      fedilink
      arrow-up
      75
      ·
      22 hours 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.

      • ptu@sopuli.xyz
        link
        fedilink
        arrow-up
        4
        ·
        9 hours ago

        So all items in the array are launched simultaneuously and ran in parallel instead of sequentially?

        • scrion@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          7 hours ago

          They are launched sequentially, but run simultaneously, yes - at least some of them. And they run concurrently but not in parallel - using a single execution context, there is only a single thread, so no parallelism exist.

          • ptu@sopuli.xyz
            link
            fedilink
            arrow-up
            1
            ·
            7 hours ago

            I see, I was only aware of sleep but that makes sense. Thanks for your insight.

    • theunknownmuncher@lemmy.world
      link
      fedilink
      arrow-up
      24
      ·
      edit-2
      22 hours ago

      The program goes through the collection of numbers and prints each one after a delay of milliseconds equal to that number: “Print the number 20 after a 20 millisecond delay. Print the number 5 after a 5 millisecond delay. Print the number 100 after a 100 millisecond delay… etc…” effectively sorting the collection because the numbers will be printed in order from smallest to largest.

      This is a clever (but impractical) way to sort a collection, because it does not require comparing any of the elements of the collection.

      • ulterno@programming.dev
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 hours ago

        because it does not require comparing any of the elements of the collection

        Well, if you are comparing x + a to y and x + b to y and then both to y', then y'' and so on, then are you really not comparing a to b?