Diminishing Gap Sort
August 19, 2016
Today’s exercise comes from my friend Dan:
I don’t have time to think about this now (very busy these days), so I’m just throwing the idea at you. I’m sure there’s some sort of mathematical theorem that relates, but I’m sure you’ll probably know off the top of your head.
I was fascinated not too long ago by the consulting firm we’ve hired to re-vamp our website. I think they were given a tip-off that the color selection for the website theme could be a sticky wicket. So their plan was to pull the colors dynamically from the main picture selected on the home page (and/or departmental pages), which could be changed easily or automatically. They were going to develop something that would pick out the most prominent 6 to 10 colors — even if there wasn’t much contrast (and even black & white photos would supposedly work) — which would be then used for the menus and general color scheme.
Anyway, all this got me thinking about sorting a list of numbers (if these were color codes, there could be lots) such that the greatest gaps would float to the top. I’m sure you get the idea, but I’ll give an example anyway — which I figured out in Excel (below):
Given a number set of 5, 12, 14, 15, 80, 121, 134, 144, 256 the descending-by-highest-difference sort would yield: 256, 80, 121, 134, 144, 12, 14, 15, 5. So in this case, the top 3 biggest gaps would be between 80, 121, and 256.
Num Diff Num Diff ---- ---- ---- ---- 256 112 256 112 144 10 80 65 134 13 121 41 121 41 134 13 80 65 144 10 15 1 12 7 14 2 5 5 12 7 14 2 5 5 15 1I have no idea if this makes sense for the colors thing — it just seemed like the type of problem you frequently do on Programming Praxis.
Hope all is well with you and your family!
-Dan-
Your task is to write a program that sorts a set of points in order by the diminishing gaps between them. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Ok, Phil. We had a major VMWare outage this afternoon that impacted a bunch of stuff, so I was C-drive-bound for a while (plus your email shamed me into writing this). When I was finally able to connect to our databases, we still had no shared drives and the application servers were mostly out to lunch. So I decided to see if I could implement the color-sort idea, with red/green/blue values – 3 values instead of just one. I sorted by red/green/blue, but I needed to get the absolute values of the aggregated gaps, since the sort yielded green and blue values going up and down. (I don’t know if that sort makes sense color-wise, but it seems logical to me.) I also took a minute to research the “Gnome sort” – or “Stupid sort”, which sounded like something made for me! So that’s what I implemented here (I can’t remember the last time I actually coded a sort algorithm!). Oracle PL/SQL code and output pasted below, which I hope is formatted so it’s readable. Like when we worked together, I fully expect you to find a bug or recommend an improvement! Take care, Phil.
Well, that turned out to be a bit more fun than I expected! Your straight-SQL solution is (as usual) both elegant and straight-forward. Here’s my output (I always like to see the starting point, interim state, and final results!):
In D…probably not suitable for large arrays due to Bubble Sort
Output:
Num: [5, 12, 14, 15, 80, 121, 134, 144, 256]
Diff: [5, 7, 2, 1, 65, 41, 13, 10, 112]
Num: [256, 80, 121, 134, 144, 12, 5, 14, 15]
Diff: [112, 65, 41, 13, 10, 7, 5, 2, 1]
Written in Common Lisp
This is my first public answer to a question. Fingers crossed I did good :D
http://paste.lisp.org/display/323870
It didn’t post the link…sorry.
In Python.
A solution in Racket.
> “They were going to develop something that would pick out the most prominent 6 to 10 colors…”
One approach to that problem is to use a clustering algorithm on points of RGB values. For example:
http://charlesleifer.com/blog/using-python-and-k-means-to-find-the-dominant-colors-in-images/
Back to the problem…
Here’s a solution in matlab.
Output: