Skip to content

Commit 7ac3915

Browse files
authored
Merge pull request #3 from rakaramos/docdocs
Docs
2 parents d4a2e6b + dd15b5a commit 7ac3915

File tree

4 files changed

+98
-337
lines changed

4 files changed

+98
-337
lines changed

Guides/PartialSort.md

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
1-
# Partial Sort
1+
# Partial Sort (sortedPrefix)
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

6-
Returns a collection such that the `0...k` range contains the first k sorted elements of a sequence.
7-
The order of equal elements is not guaranteed to be preserved, and the order of the remaining elements is unspecified.
6+
Returns the first k elements of this collection when it's sorted.
87

9-
If you need to sort a sequence but only need access to a prefix of its elements,
10-
using this method can give you a performance boost over sorting the entire sequence.
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.
1112

1213
```swift
1314
let numbers = [7,1,6,2,8,3,9]
14-
let almostSorted = numbers.partiallySorted(3, <)
15-
// [1, 2, 3, 9, 7, 6, 8]
15+
let smallestThree = numbers.sortedPrefix(<)
16+
// [1, 2, 3]
1617
```
1718

1819
## Detailed Design
1920

20-
This adds the in-place `MutableCollection` method shown below:
21+
This adds the `Collection` method shown below:
2122

2223
```swift
23-
extension Sequence {
24-
func partiallySort(_ count: Int, by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows
24+
extension Collection {
25+
public func sortedPrefix(_ count: Int, by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> [Element]
2526
}
2627
```
2728

28-
Additionally, versions of this method that return a new array and abstractions for `Comparable` types are also provided:
29+
Additionally, a version of this method for `Comparable` types is also provided:
2930

3031
```swift
31-
extension MutableCollection where Self: RandomAccessCollection, Element: Comparable {
32-
public mutating func partiallySort(_ count: Int)
33-
}
34-
35-
extension Sequence {
36-
public func partiallySorted(_ count: Int, by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> [Element]
37-
}
38-
39-
extension Sequence where Element: Comparable {
40-
public func partiallySorted(_ count: Int) -> [Element]
32+
extension Collection where Element: Comparable {
33+
public func sortedPrefix(_ count: Int) -> [Element]
4134
}
4235
```
4336

4437
### Complexity
4538

46-
Partially sorting is a O(_k log n_) operation, where _k_ is the number of elements to sort
47-
and _n_ is the length of the sequence.
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)`.
40+
41+
Here are some benchmarks we made that demonstrates how this implementation (SmallestM) behaves when k increases (before implementing the fallback):
4842

49-
`partiallySort(_:by:)` is a slight generalization of a priority queue. It's implemented
50-
as an in-place heapsort that stops after _k_ runs.
43+
![Benchmark](https://i.imgur.com/F5UEQnl.png)
44+
![Benchmark 2](https://i.imgur.com/Bm9DKRc.png)
5145

5246
### Comparison with other languages
5347

54-
**C++:** The `<algorithm>` library defines a `partial_sort` function with similar
55-
semantics to this one.
48+
**C++:** The `<algorithm>` library defines a `partial_sort` function where the entire array is returned.
5649

5750
**Python:** Defines a `heapq` priority queue that can be used to manually
5851
achieve the same result.

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-
- [`partiallySorted(_:by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/PartialSort.md): Sorts a sequence only up to a specific index, leaving the remaining elements unsorted.
33+
- [`sortedPrefix(_:by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/PartialSort.md): Returns the first k elements of a sorted collection.
3434

3535
#### Other useful operations
3636

0 commit comments

Comments
 (0)