Skip to content

Commit 70973a2

Browse files
committed
Documentation fixes
1 parent 435a38c commit 70973a2

File tree

6 files changed

+13
-17
lines changed

6 files changed

+13
-17
lines changed
73.6 KB
Loading
77.4 KB
Loading

Guides/PartialSort.md renamed to Guides/SortedPrefix.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
# Partial Sort (sortedPrefix)
1+
# Sorted Prefix
22

33
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/PartialSort.swift) |
44
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/PartialSortTests.swift)]
55

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

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

1310
```swift
1411
let numbers = [7,1,6,2,8,3,9]
15-
let smallestThree = numbers.sortedPrefix(<)
12+
let smallestThree = numbers.sortedPrefix(3, by: <)
1613
// [1, 2, 3]
1714
```
1815

@@ -36,17 +33,16 @@ extension Collection where Element: Comparable {
3633

3734
### Complexity
3835

39-
The algorithm used 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 the collection), we fallback to sorting the entire array. Realistically, this means the worst case is actually `O(n log n)`.
36+
The algorithm used 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 the collection), we fall back to sorting the entire array. Realistically, this means the worst case is actually `O(n log n)`.
4037

4138
Here are some benchmarks we made that demonstrates how this implementation (SmallestM) behaves when k increases (before implementing the fallback):
4239

43-
![Benchmark](https://i.imgur.com/F5UEQnl.png)
44-
![Benchmark 2](https://i.imgur.com/Bm9DKRc.png)
40+
![Benchmark](Resources/SortedPrefix/FewElements.png)
41+
![Benchmark 2](Resources/SortedPrefix/ManyElements.png)
4542

4643
### Comparison with other languages
4744

4845
**C++:** The `<algorithm>` library defines a `partial_sort` function where the entire array is returned using a partial heap sort.
4946

50-
**Python:** Defines a `heapq` priority queue that can be used to manually
51-
achieve the same result.
47+
**Python:** Defines a `heapq` priority queue that can be used to manually achieve the same result.
5248

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
3030

3131
#### Partial sorting
3232

33-
- [`sortedPrefix(_:by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/PartialSort.md): Returns the first k elements of a sorted collection.
33+
- [`sortedPrefix(_:by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/SortedPrefix.md): Returns the first k elements of a sorted collection.
3434

3535
#### Other useful operations
3636

Sources/Algorithms/PartialSort.swift renamed to Sources/Algorithms/SortedPrefix.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension Collection {
1717
/// smallest values:
1818
///
1919
/// let numbers = [7,1,6,2,8,3,9]
20-
/// let smallestThree = numbers.sortedPrefix(3, <)
20+
/// let smallestThree = numbers.sortedPrefix(3, by: <)
2121
/// // [1, 2, 3]
2222
///
2323
/// If you need to sort a collection but only need access to a prefix of its
@@ -65,14 +65,14 @@ extension Collection {
6565
}
6666

6767
extension Collection where Element: Comparable {
68-
/// Returns the first k elements of this collection when it's sorted using
69-
/// the given predicate as the comparison between elements.
68+
/// Returns the first k elements of this collection when it's sorted in
69+
/// ascending order.
7070
///
7171
/// This example partially sorts an array of integers to retrieve its three
7272
/// smallest values:
7373
///
7474
/// let numbers = [7,1,6,2,8,3,9]
75-
/// let smallestThree = numbers.sortedPrefix(3, <)
75+
/// let smallestThree = numbers.sortedPrefix(3)
7676
/// // [1, 2, 3]
7777
///
7878
/// If you need to sort a collection but only need access to a prefix of its

Tests/SwiftAlgorithmsTests/PartialSortTests.swift renamed to Tests/SwiftAlgorithmsTests/SortedPrefixTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import XCTest
1313
import Algorithms
1414

15-
final class PartialSortTests: XCTestCase {
15+
final class SortedPrefixTests: XCTestCase {
1616
func testEmpty() {
1717
let array = [Int]()
1818
XCTAssertEqual(array.sortedPrefix(0), [])

0 commit comments

Comments
 (0)