Skip to content

Commit dd15b5a

Browse files
committed
Docs
1 parent 5bdea96 commit dd15b5a

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

Guides/PartialSort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Returns the first k elements of this collection when it's sorted.
77

8-
If you need to sort a sequence but only need access to a prefix of its
8+
If you need to sort a collection but only need access to a prefix of its
99
elements, using this method can give you a performance boost over sorting
1010
the entire collection. The order of equal elements is guaranteed to be
1111
preserved.

Sources/Algorithms/PartialSort.swift

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@
88
// See https://swift.org/LICENSE.txt for license information
99
//
1010

11-
//===----------------------------------------------------------------------===//
12-
// sortedPrefix(_:by:)
13-
//===----------------------------------------------------------------------===//
14-
1511
extension Collection {
16-
12+
/// Returns the first k elements of this collection when it's sorted using
13+
/// the given predicate as the comparison between elements.
14+
///
15+
/// This example partially sorts an array of integers to retrieve its three
16+
/// smallest values:
17+
///
18+
/// let numbers = [7,1,6,2,8,3,9]
19+
/// let smallestThree = numbers.sortedPrefix(3, <)
20+
/// // [1, 2, 3]
21+
///
22+
/// If you need to sort a collection but only need access to a prefix of its
23+
/// elements, using this method can give you a performance boost over sorting
24+
/// the entire collection. The order of equal elements is guaranteed to be
25+
/// preserved.
26+
///
27+
/// - Parameter count: The k number of elements to prefix.
28+
/// - Parameter areInIncreasingOrder: A predicate that returns true if its
29+
/// first argument should be ordered before its second argument;
30+
/// otherwise, false.
31+
///
32+
/// - Complexity: O(k log k + nk)
1733
public func sortedPrefix(
1834
_ count: Int,
1935
by areInIncreasingOrder: (Element, Element) throws -> Bool
@@ -35,17 +51,35 @@ extension Collection {
3551
var result = try self.prefix(count).sorted(by: areInIncreasingOrder)
3652
for e in self.dropFirst(count) {
3753
if let last = result.last, try areInIncreasingOrder(last, e) { continue }
38-
if let insertionIndex = try result.firstIndex (where: { try areInIncreasingOrder(e, $0) }) {
54+
if let insertionIndex = try result.firstIndex(where: { try areInIncreasingOrder(e, $0) }) {
3955
result.insert(e, at: insertionIndex)
4056
result.removeLast()
4157
}
4258
}
59+
4360
return result
4461
}
4562
}
4663

4764
extension Collection where Element: Comparable {
48-
65+
/// Returns the first k elements of this collection when it's sorted using
66+
/// the given predicate as the comparison between elements.
67+
///
68+
/// This example partially sorts an array of integers to retrieve its three
69+
/// smallest values:
70+
///
71+
/// let numbers = [7,1,6,2,8,3,9]
72+
/// let smallestThree = numbers.sortedPrefix(3, <)
73+
/// // [1, 2, 3]
74+
///
75+
/// If you need to sort a collection but only need access to a prefix of its
76+
/// elements, using this method can give you a performance boost over sorting
77+
/// the entire collection. The order of equal elements is guaranteed to be
78+
/// preserved.
79+
///
80+
/// - Parameter count: The k number of elements to prefix.
81+
///
82+
/// - Complexity: O(k log k + nk)
4983
public func sortedPrefix(_ count: Int) -> [Element] {
5084
return sortedPrefix(count, by: <)
5185
}

0 commit comments

Comments
 (0)