TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Quicksort in Swift

30 pointsby fjcaetanoalmost 11 years ago

13 comments

gilgoomeshalmost 11 years ago
If anyone&#x27;s wondering, yes, there&#x27;s a quickSort function already in the Swift standard library (no import required):<p><pre><code> var ar = [3456,45,53567,356,7225,47858] quickSort(&amp;ar, Range(start: ar.startIndex, end: ar.endIndex)) &#x2F;&#x2F; [45, 356, 3456, 7225, 47858, 53567] </code></pre> There&#x27;s also a version that let&#x27;s you specify the &quot;less&quot; comparator.
评论 #7850176 未加载
dstaleyalmost 11 years ago
I was surprised this example didn&#x27;t take advantage of Swift&#x27;s built-in Comparable protocol, so I rewrote it! I also wrote an example of how you can implement the Comparable protocol for a struct, so that they can then be sorted.<p><a href="https://gist.github.com/dstaley/e7171375d3f5cb9f2fe4" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;dstaley&#x2F;e7171375d3f5cb9f2fe4</a>
shawkinawalmost 11 years ago
This actually highlights one area where I find Swift slightly deficient. I was just thinking earlier today that Swift is remarkably close to my dream language, i.e. incorporates the great features of many languages I&#x27;ve come in contact with. But one thing it lacks is Haskell&#x27;s amazing list comprehension, which helps make Haskell quicksort so succinct.
评论 #7850150 未加载
djb_hackernewsalmost 11 years ago
Had a chat about Swift over lunch today, one topic was the choice of using .. and ... to represent ranges. Interesting to see the comments of the gist focus on this as well. Surely is a confusing and potentially dangerous syntax.
评论 #7850201 未加载
brudgersalmost 11 years ago
Hoare&#x27;s original paper from 1962.<p><a href="http://comjnl.oxfordjournals.org/content/5/1/10.full.pdf+html" rel="nofollow">http:&#x2F;&#x2F;comjnl.oxfordjournals.org&#x2F;content&#x2F;5&#x2F;1&#x2F;10.full.pdf+htm...</a><p>There are reasons not to perform quickSort the way people did fifty years ago. Data sets are larger and worst case time for quickSort approach 0(n^2) when data is already ordered and pivots are selected in-order. Randomizing pivot selection mitigates this back toward O(log n).<p>All that mutation and pointer swapping are artifacts of the way things used to have to be done by programmers. And they&#x27;re useful as academic exercises. Hoare&#x27;s algorithm is really fucking brilliant. But mapping and filtering in a functional style, makes more sense than dotting the &#x27;i&#x27;s and &#x27;j&#x27;s if someone is going to be reading and maintaining the code. There&#x27;s a reason people don&#x27;t use C to program mobile devices.<p>Moreover, while Hoare&#x27;s original algorithm was designed to work in place, the idea of working in place is less coherent today. When we strive to distribute and parallelize big nasty resource intensive jobs, what does &quot;in-place&quot; mean? See Leslie Lamport, <i>Thinking for Programmers</i> at about time = 36:00<p><a href="http://channel9.msdn.com/Events/Build/2014/3-642" rel="nofollow">http:&#x2F;&#x2F;channel9.msdn.com&#x2F;Events&#x2F;Build&#x2F;2014&#x2F;3-642</a><p>And even on a non-distributed system, when are we really coming up against the bounds of &#x27;fast memory&#x27; these days? A serious problem that stretches 16 GigaBytes of RAM is less than $10,000 from 64 GigaBytes...or $200 from an SSD and using &#x27;slow memory&#x27;.<p>Of course if we are parallelizing, we can even do it on one machine and again, what is &#x27;in-place&#x27;? The caches? Again which ones and at what level. The Swift code (or Java or C#) may look like C, but it&#x27;s not. It may be garbage collected, JIT&#x27;d, byte coded, and micro-coded all before it hits the mulit-stage branch predicting pipeline from several levels of cache reading from code pages that may have gone in and out to disk as threads and tasks switch.<p>&#x27;Place&#x27; might just mean somewhere. But probably somewheres.
imkevinxualmost 11 years ago
I can&#x27;t wait until GitHub or someone adds in Swift syntax highlighting
alaynealmost 11 years ago
What kind of monster writes i += 1?
评论 #7849912 未加载
评论 #7849918 未加载
评论 #7850132 未加载
评论 #7850108 未加载
supersillyusalmost 11 years ago
From that, it&#x27;s not too hard how people could think it was influenced by Go (though it apparently wasn&#x27;t): <a href="http://play.golang.org/p/oWNRd0D2Zp" rel="nofollow">http:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;oWNRd0D2Zp</a> looks superficially very similar
nextstepalmost 11 years ago
I like the syntax of Swift
评论 #7849868 未加载
kiwiwearablesalmost 11 years ago
was waiting for someone to put this out :) awesome.
hadoukenioalmost 11 years ago
Until Apple open Swift, I&#x27;m sorry, it&#x27;s DOA.
评论 #7849766 未加载
评论 #7849771 未加载
评论 #7849780 未加载
评论 #7849883 未加载
seanewestalmost 11 years ago
I put your code on npm: <a href="https://www.npmjs.org/package/quicksort.swift" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.org&#x2F;package&#x2F;quicksort.swift</a><p>Edit: ...and then swiftly unpublished. It is unclear to me the legal issues involved in forking and publishing unlicensed gists.<p><a href="http://stackoverflow.com/questions/4007674/whats-the-default-license-of-code-published-at-github" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;4007674&#x2F;whats-the-default...</a>
评论 #7849991 未加载
Alupisalmost 11 years ago
Wow... seems like a nightmare for several reasons:<p>&gt;Naming Constants and Variables<p>&gt;You can use almost any character you like for constant and<p>&gt;variable names, including Unicode characters:<p>&gt;let π = 3.14159<p>&gt;let 你好 = &quot;你好世界&quot;<p>&gt;let 🐶🐮 = &quot;dogcow&quot;<p><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_399" rel="nofollow">https:&#x2F;&#x2F;developer.apple.com&#x2F;library&#x2F;prerelease&#x2F;ios&#x2F;documenta...</a>
评论 #7850138 未加载
评论 #7850140 未加载