Thanks Don On 27/05/2014 11:01 PM, 'Pascal Jasmin' via Programming wrote:
sorry. bubble sort is dumb. http://en.wikipedia.org/wiki/Bubble_sort (includes explicit version)Its fun (or mesmerizing to me) to see the intermediate steps. It is hoplessly innefficient, the optimization included. I considered it an interesting way to traverse a list with / and ^:a: in terms of steps to understand: 3 ((<.{.),(>.{.)) 2 2 3 looks a bit more complicated then it needs to be for just 2 arguments, but it takes the minimum and maximum between x and the head of y, appending those 2 items in order. The same verb idea could have been writen as: (([ <. {.@:]) , [ >. {.@:]) ((<.{.) , (>.{.))/ 3 5 2 2 3 NB. applies first as 5 bs 2, then 3 bs 2 (}.@:] ,~ (<. {.) , (>. {.))/ 2 5 1 4 3 1 2 5 3 4 adds the rest of the list. Bubble sort does not complete in a single pass, which is why it needs to be fed to ^:_ or ^:a: a different way taking the same function. (}.@:] ,~ (,`(,~)@.> {.))/ 2 5 3 6 4 2 3 5 4 6 ----- Original Message ----- From: Don Kelly <[email protected]> To: [email protected] Cc: Sent: Wednesday, May 28, 2014 12:50:36 AM Subject: Re: [Jprogramming] a silly bubble sort Why? (there is a built in sort involved in a=:/:a) If I am sorting, something built into the language may be more appropriate. So, Is it easier or more efficient than the sorting now available?. It would help me, and many others,in learning, to add documentation (I can look up "bubble sort" concepts and go through the steps in the one liner. --------------but------------------- from a learning approach some breakdown would be appreciated-possibly an explicit approach- with comments- leading to a tacit form- this forum does present modifications and improvements which are welcome. Be it known that I am an old fart with a "devil's advocate" view -being critical- often right-more often wrong! (first program written was in MAD in 1961- card input, and reams of paper output if lucky- otherwise an ascii picture of Alfred E Neuman with "this is Mad!' graduated to APL when it became available and recently trying to learn the best so far--J). However, I enjoy your contributions and the support I have received from this forum. Don Kelly On 27/05/2014 7:38 AM, 'Pascal Jasmin' via Programming wrote:bs =: }.@:] ,~ (<. {.) , (>. {.) NB. name not used below one liner (}.@:] ,~ (<. {.) , (>. {.))/^:a: ? ($ ]) 25 23 0 22 7 14 15 8 18 4 5 2 6 17 22 7 3 15 12 22 20 0 13 8 3 6 0 23 0 22 7 14 15 8 18 4 5 2 6 17 22 7 3 15 12 22 20 3 13 8 6 0 0 23 2 22 7 14 15 8 18 4 5 3 6 17 22 7 3 15 12 22 20 6 13 8 0 0 2 23 3 22 7 14 15 8 18 4 5 3 6 17 22 7 6 15 12 22 20 8 13 0 0 2 3 23 3 22 7 14 15 8 18 4 5 6 6 17 22 7 8 15 12 22 20 13 0 0 2 3 3 23 4 22 7 14 15 8 18 5 6 6 7 17 22 8 12 15 13 22 20 0 0 2 3 3 4 23 5 22 7 14 15 8 18 6 6 7 8 17 22 12 13 15 20 22 0 0 2 3 3 4 5 23 6 22 7 14 15 8 18 6 7 8 12 17 22 13 15 20 22 0 0 2 3 3 4 5 6 23 6 22 7 14 15 8 18 7 8 12 13 17 22 15 20 22 0 0 2 3 3 4 5 6 6 23 7 22 7 14 15 8 18 8 12 13 15 17 22 20 22 0 0 2 3 3 4 5 6 6 7 23 7 22 8 14 15 8 18 12 13 15 17 20 22 22 0 0 2 3 3 4 5 6 6 7 7 23 8 22 8 14 15 12 18 13 15 17 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 23 8 22 12 14 15 13 18 15 17 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 23 12 22 13 14 15 15 18 17 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 23 13 22 14 15 15 17 18 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 23 14 22 15 15 17 18 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 23 15 22 15 17 18 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 15 23 15 22 17 18 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 15 15 23 17 22 18 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 15 15 17 23 18 22 20 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 15 15 17 18 23 20 22 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 15 15 17 18 20 23 22 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 15 15 17 18 20 22 23 22 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 15 15 17 18 20 22 22 23 22 0 0 2 3 3 4 5 6 6 7 7 8 8 12 13 14 15 15 17 18 20 22 22 22 23 A recursive "optimised" version ((}.@:] ,~ (<.{.) , (>.{.))/^:(7<#)@:}.@:] ,~ (<.{.) , (>.{.))/^:a: ? ($ ]) 25 1 1 6 19 14 13 11 9 7 1 9 15 3 15 5 18 16 2 4 13 4 24 24 15 19 1 1 1 2 3 6 4 19 4 14 5 13 7 11 9 9 13 15 15 15 16 18 19 24 24 1 1 1 2 3 4 4 5 6 7 9 9 11 13 13 19 14 15 15 15 16 18 19 24 24 1 1 1 2 3 4 4 5 6 7 9 9 11 13 13 14 15 15 15 16 18 19 19 24 24 the name bs can be substituted for the leftmost part. ^:(7<#) -- only applies bs to the rest of the list if it is at least 7 items long. The number is arbitrary, though an error occurs if it is not at least ^:(1<#) items long. The "optimization" just eliminates the number of outer paths through the data, and so there is no need to make this number as small as possible, since there are likely further passes anyway. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
