Skip to content
Tactiverge

○ Manufacturing · sorting and counting comparisons

Sort parts by weight

Order a parts list by mass and count how many comparisons insertion sort makes; watch it step by step.

  • Level 3 · Innovator
  • 8 tests
  • technically checked

Your task

A packing robot loads the lightest parts first. Write:

  • sortParts(parts): parts is an array of { name, grams }. Return the names ordered by grams, lightest first; equal weights are ordered alphabetically by name.
  • insertionComparisons(weights): run insertion sort on a copy of weights and return how many comparisons it makes. Use this exact algorithm: for i from 1 to n−1, set j = i; while j > 0, compare weights[j − 1] > weights[j] (count it); if true swap them and decrease j, otherwise stop.

Run the starter to watch insertion sort in the visualiser: the helpers less(arr, i, j) and swap(arr, i, j) record each step.

Hints (0 of 3 shown)

    What gets checked

    • sortParts([{ name: "bolt", grams: 12 }, { name: "nut", gram…) → ["nut", "bolt", "gear"]
    • insertionComparisons([3, 2, 1]) → 3
    • insertionComparisons([1, 2, 3]) → 2

    Plus 5 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.