<div dir="ltr">On Tue, Jun 28, 2016 at 9:58 PM, Erica Sadun <span dir="ltr"><<a href="mailto:erica@ericasadun.com" target="_blank">erica@ericasadun.com</a>></span> wrote:<div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Most everyone is doing two passes, one to get the minimum value, another to get its index.<br>
I aesthetically prefer using enumerate to do both at once.<br>
<span><font color="#888888"><br>
-- E<br></font></span></blockquote><div><br></div><div>Makes sense. Here’s a revision. It’s not as simple, but it does just one pass through the array:</div><div><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz;min-height:16px"><span style="font-variant-ligatures: no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="color:#bb2ca2">func</span><span style="font-variant-ligatures: no-common-ligatures"> selectionSort(</span><span style="color:#bb2ca2">_</span><span style="font-variant-ligatures: no-common-ligatures"> array: [</span><span style="color:#703daa">Int</span><span style="font-variant-ligatures: no-common-ligatures">]) -> [</span><span style="color:#703daa">Int</span><span style="font-variant-ligatures: no-common-ligatures">] {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="color:#bb2ca2">guard</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="color:#bb2ca2">let</span><span style="font-variant-ligatures: no-common-ligatures"> first = array.</span><span style="color:#703daa">first</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="color:#bb2ca2">else</span><span style="font-variant-ligatures: no-common-ligatures"> { </span><span style="color:#bb2ca2">return</span><span style="font-variant-ligatures: no-common-ligatures"> [] }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="color:#bb2ca2">let</span><span style="font-variant-ligatures: no-common-ligatures"> (index, minValue) = array.</span><span style="color:#3d1d81">enumerated</span><span style="font-variant-ligatures: no-common-ligatures">().</span><span style="color:#3d1d81">reduce</span><span style="font-variant-ligatures: no-common-ligatures">((</span><span style="color:#272ad8">0</span><span style="font-variant-ligatures: no-common-ligatures">, first)) { (carry, item) </span><span style="color:#bb2ca2">in</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="color:#bb2ca2">if</span><span style="font-variant-ligatures: no-common-ligatures"> item.element < carry.</span><span style="color:#272ad8">1</span><span style="font-variant-ligatures: no-common-ligatures"> { </span><span style="color:#bb2ca2">return</span><span style="font-variant-ligatures: no-common-ligatures"> item } </span><span style="color:#bb2ca2">else</span><span style="font-variant-ligatures: no-common-ligatures"> { </span><span style="color:#bb2ca2">return</span><span style="font-variant-ligatures: no-common-ligatures"> carry }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz;min-height:16px"><span style="font-variant-ligatures: no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="color:#bb2ca2">let</span><span style="font-variant-ligatures: no-common-ligatures"> ranges = [</span><span style="color:#272ad8">0</span><span style="font-variant-ligatures: no-common-ligatures">..<index, index.</span><span style="color:#3d1d81">advanced</span><span style="font-variant-ligatures: no-common-ligatures">(by: </span><span style="color:#272ad8">1</span><span style="font-variant-ligatures: no-common-ligatures">)..<array.</span><span style="color:#703daa">endIndex</span><span style="font-variant-ligatures: no-common-ligatures">]</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="color:#bb2ca2">return</span><span style="font-variant-ligatures: no-common-ligatures"> [minValue] + </span><span style="color:#31595d">selectionSort</span><span style="font-variant-ligatures: no-common-ligatures">(ranges.</span><span style="color:#3d1d81">flatMap</span><span style="font-variant-ligatures: no-common-ligatures"> { array[$0] })</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="font-variant-ligatures: no-common-ligatures">}</span></p></div><div><br></div><div>~Dan<br></div></div></div></div>