• scrion@lemmy.world
      link
      fedilink
      arrow-up
      54
      ·
      13 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.

    • theunknownmuncher@lemmy.world
      link
      fedilink
      arrow-up
      17
      ·
      edit-2
      13 days 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
        ·
        12 days 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?