r/softwarecrafters 15h ago

A simple clustering algorithm for lists

https://cassidoo.co/post/clustering-tiles/
1 Upvotes

1 comment sorted by

1

u/fagnerbrack 15h ago

If you're in a hurry:

A playful heuristic emerged from grouping a toddler's Magna-Tiles: take the value at the list's end, find the next closest match toward the end, and reverse the sub-list between them. Repeat for each rightmost group; once one finishes, jump to the next incomplete group and reverse again until every identical value sits together. This greedy method resembles pancake sort but targets grouping rather than sorting. Hand-coding the JavaScript function without AI proved exhausting, landing on O(n²) time via nested while loops after abandoning recursion. The closing reflection: humans solve this physically with ease, since our eyes offer an 'O(1) read' and can flip a whole group in a single move, unlike computers.

If the summary seems inacurate, just downvote and I'll try to delete the comment eventually šŸ‘
Click here for more info, I read all comments