Diminishing Gap Sort
August 19, 2016
Dan and I met as programmers supporting an ERP system written in Oracle. Dan and I have both moved to new employers since then, but both of us still work on the same ERP. Since we both program databases, I decided to write my solution in SQL:
$ sqlplus username@sid/password SQL*Plus: Release 11.2.0.3.0 Production on Thu Aug 18 08:07:47 2016 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> create table dan (val number(4,0), gap number(4,0)); Table created. SQL> insert into dan values (80,null); 1 row created. SQL> insert into dan values (256,null); 1 row created. SQL> insert into dan values (144,null); 1 row created. SQL> insert into dan values (5,null); 1 row created. SQL> insert into dan values (12,null); 1 row created. SQL> insert into dan values (134,null); 1 row created. SQL> insert into dan values (15,null); 1 row created. SQL> insert into dan values (14,null); 1 row created. SQL> insert into dan values (121,null); 1 row created. SQL> commit; Commit complete. SQL> update dan a 2 set gap = val - ( 3 select nvl(max(val),0) 4 from dan b 5 where b.val < a.val ); 9 rows updated. SQL> commit; Commit complete. SQL> select * from dan order by gap desc; VAL GAP ---------- ---------- 256 112 80 65 121 41 134 13 144 10 12 7 5 5 14 2 15 1 9 rows selected. SQL> drop table dan; Table dropped. SQL> exit
The solution creates a table of value/gap tuples, then enters the data into the table. The update
statement computes the gaps, and the final select
displays the output in decreasing order by gap: 256, 80, 121, 134, 144, 12, 5, 14, 15.
A Scheme solution appears on the next page.
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: