Skip to content

Commit 1def897

Browse files
committed
Documentation fixes
1 parent 435a38c commit 1def897

File tree

6 files changed

+12
-16
lines changed

6 files changed

+12
-16
lines changed
73.6 KB
Loading
77.4 KB
Loading

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

Lines changed: 6 additions & 10 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

@@ -40,13 +37,12 @@ The algorithm used is based on [Soroush Khanlou's research on this matter](https
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)