• Jankatarch@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    10 days 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.

    • scrion@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      18 days 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
        0
        ·
        17 days ago

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

        • scrion@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          17 days 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.

    • sus@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      17 days 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
        1
        ·
        17 days 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!