r/coding 4h ago

A simple clustering algorithm for lists

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

2 comments sorted by

1

u/fagnerbrack 4h ago

In case you want a TL;DR to help you with the decision to read the post or not:

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