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.

Cycle Sort

55 pointsby surajover 14 years ago

5 comments

mfukarover 14 years ago
I believe that the author is referring to the case when the array to be sorted contains only duplicates of a small number of items, where a perfect hash function can speed up insertion; this turns cycle sort's time complexity into Θ(n+k), with <i>k</i> being the number of hashes. In such a case, <i>k</i> is not negligible compared to <i>n</i>, so I wouldn't feel comfortable saying cycle sort is O(n), as he does.<p>In the general case, cycle sort is Θ(n^2) with a total space complexity of Θ(n).<p>edit:typos.
评论 #1929873 未加载
tsewlliwover 14 years ago
If you must restrict the input to permutations of [0,...,N], you already know the result! Finding cycles is neat, but this works for the same data, minus having bounds:<p>(define (trivialsort vals) (lambda (i) i))
stingraycharlesover 14 years ago
It's nice to say this algorithm is virtually O(n) in practice, but that's about the "cycling" mechanism only. It needs to prepare a dictionary with offsets, which has to loop over all the keys, perform an O (log n) insert operation on all the keys, and allocate memory on the heap. This already makes it (almost) O (n log n), without even doing the actual sorting.<p>It's a nice idea, but it's not O(n).
评论 #1929340 未加载
评论 #1929957 未加载
miseover 14 years ago
Nice use of Slovenia's TLD.
ancymonover 14 years ago
I wonder how it sounds ;)