<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">do you mean this?</pre><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">public func binarySearch<T: Comparable>(array: [T], key: T, range: Range<Int>, sorted: Bool) -> Int?</pre><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><p dir="ltr" style="color: rgb(96, 96, 96); font-size: 15px; line-height: 22.5px; margin: 1em 0px; padding: 0px;" class="">Best regards, Igor Vasilenko <br class=""></p><p dir="ltr" style="color: rgb(96, 96, 96); font-size: 15px; line-height: 22.5px; margin: 1em 0px; padding: 0px;" class="">iOS Developer at Yota</p><p dir="ltr" style="color: rgb(96, 96, 96); font-size: 15px; line-height: 22.5px; margin: 1em 0px; padding: 0px;" class="">+7 (999) 527 - 07 - 59<br class=""><span style="line-height: 1.6em;" class=""><a href="mailto:name.surname@e-legion.com" target="_blank" class="">spb.vasilenko@gmail.com</a></span><br class=""><a href="http://www.e-legion.com/" target="_blank" style="line-height: 1.6em; word-wrap: break-word; color: rgb(109, 198, 221);" class="">www.spbvasilenko.github.io</a></p></div></div><br class="Apple-interchange-newline"></div><br class="Apple-interchange-newline"></div><br class="Apple-interchange-newline"></div><br class="Apple-interchange-newline"></div><br class="Apple-interchange-newline"></div><br class="Apple-interchange-newline"><br class="Apple-interchange-newline">
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On 07 Sep 2016, at 13:08, Guillaume DIDIER <<a href="mailto:guillaume.didier.2014@polytechnique.org" class="">guillaume.didier.2014@polytechnique.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Basically for a binary search to work it needs to operate on a sorted array (it is a necessary invariant).</div><div class=""><br class=""></div><div class="">It is really interesting when you make a lot of search in the same sorted array, hence I would +1 the sorted array, with initializer from an array.</div><div class=""><br class=""></div><br class=""><div class="">
<div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; margin: 0px; line-height: normal;" class=""><font face="Georgia" size="2" color="#073763" class=""><b class=""><span style="-webkit-text-stroke-color: rgb(7, 55, 99); -webkit-text-stroke-width: initial;" class="">Guillaume </span><span style="-webkit-text-stroke-width: initial;" class=""> DIDIER</span></b></font></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: initial; -webkit-text-stroke-color: rgb(7, 55, 99); font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; margin: 0px; line-height: normal;" class=""><span style="color: rgb(7, 55, 99); font-family: 'Century Gothic'; font-size: 13px; -webkit-text-stroke-width: initial;" class="">—</span></div><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(7, 55, 99); -webkit-text-stroke-color: rgb(7, 55, 99); font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal;" class=""><span class=""><span class=""><span class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-color: rgb(7, 55, 99); -webkit-text-stroke-width: 0px; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal;" class=""><div style="font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: initial; color: rgb(7, 55, 99); -webkit-text-stroke-color: rgb(7, 55, 99); margin: 0px; line-height: normal; font-family: 'Lucida Sans'; min-height: 15px;" class=""><b style="font-family: 'Century Gothic'; font-size: 11px; -webkit-text-stroke-width: initial;" class="">ÉCOLE POLYTECHNIQUE</b><i class=""></i></div><div style="font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: initial; color: rgb(7, 55, 99); -webkit-text-stroke-color: rgb(7, 55, 99); margin: 0px; font-size: 11px; line-height: normal; font-family: 'Century Gothic';" class="">91128 PALAISEAU CEDEX<br class=""><span style="color: rgb(30, 73, 125); -webkit-text-stroke-color: rgb(30, 73, 125);" class="">M.</span> <span style="color: rgb(30, 73, 125); -webkit-text-stroke-color: rgb(30, 73, 125);" class="">+33 (</span>0<span style="color: rgb(30, 73, 125); -webkit-text-stroke-color: rgb(30, 73, 125);" class="">)7</span> 70 43 18 40<br class=""><a href="mailto:guillaume.didier@polytechnique.edu?subject=" class=""><span style="line-height: normal; -webkit-text-stroke-color: rgb(71, 135, 255);" class="">guillaume.didier@polytechnique.edu</span></a></div><div style="font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: initial; color: rgb(71, 135, 255); margin: 0px; font-size: 11px; line-height: normal; font-family: 'Century Gothic'; -webkit-text-stroke-color: rgb(71, 135, 255);" class=""><span style="text-decoration: underline;" class=""><a href="http://www.polytechnique.edu/" class="">www.polytechnique.edu</a></span></div><div style="font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: initial; color: rgb(7, 55, 99); font-size: 13px; -webkit-text-stroke-color: rgb(7, 55, 99); margin: 0px; line-height: normal; font-family: 'Century Gothic';" class="">—</div></span></span></span></span></span></div></div></div></div></div></div>
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">Le 7 sept. 2016 à 12:04, Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><blockquote type="cite" class="">On 7 Sep 2016, at 10:08, Charlie Monroe via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">Aside from this being additive (i.e. out of scope for Swift 4), this requires the array to be sorted in order for the search to work - who will guarantee this? The caller? What happens when this is called on an array that is not sorted? You likely get nil, while the item is in the array (false negative).<br class=""><br class="">This would probably make sense by not extending Array itself, but introducing SortedArray which would automatically keep its members sorted instead - this way there would be a guarantee that the array is sorted and the user won't have to deal with sorting the array. It would however be at the cost of O(log N) for insertion…<br class=""></blockquote><br class="">I don't think this is really a problem, just needs to be clear that behaviour is undefined if the array wasn't previously sorted (or not in the same order).<br class=""><br class="">On this topic there was a previous proposal that was undergoing refinements after being initially rejected, you can find it here:<br class=""><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0074-binary-search.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0074-binary-search.md</a><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>