diff --git a/Guides/MinMax.md b/Guides/MinMax.md index 9cb5a74d..117fc871 100644 --- a/Guides/MinMax.md +++ b/Guides/MinMax.md @@ -79,7 +79,7 @@ extension Sequence where Element: Comparable { The algorithm used for minimal- or maximal-ordered subsets is based on [Soroush Khanlou's research on this matter](https://khanlou.com/2018/12/analyzing-complexity/). The total complexity is `O(k log k + nk)`, which will result in a runtime close -to `O(n)` if *k* is a small amount. If *k* is a large amount (more than 10% of +to `O(n)` if *k* is a small amount. If *k* is a large amount (more than log n of the collection), we fall back to sorting the entire array. Realistically, this means the worst case is actually `O(n log n)`. diff --git a/Sources/Algorithms/MinMax.swift b/Sources/Algorithms/MinMax.swift index 5d35e13c..47584cb9 100644 --- a/Sources/Algorithms/MinMax.swift +++ b/Sources/Algorithms/MinMax.swift @@ -270,9 +270,9 @@ extension Collection { return [] } - // If we're attempting to prefix more than 10% of the collection, it's + // If we're attempting to prefix more than log n of the collection, it's // faster to sort everything. - guard prefixCount < (self.count / 10) else { + guard prefixCount <= self.count._binaryLogarithm() else { return Array(try sorted(by: areInIncreasingOrder).prefix(prefixCount)) } @@ -326,9 +326,9 @@ extension Collection { return [] } - // If we're attempting to prefix more than 10% of the collection, it's + // If we're attempting to suffix more than log n of the collection, it's // faster to sort everything. - guard suffixCount < (self.count / 10) else { + guard suffixCount <= self.count._binaryLogarithm() else { return Array(try sorted(by: areInIncreasingOrder).suffix(suffixCount)) }