File tree Expand file tree Collapse file tree 1 file changed +13
-10
lines changed Expand file tree Collapse file tree 1 file changed +13
-10
lines changed Original file line number Diff line number Diff line change @@ -161,33 +161,36 @@ extension MutableCollection where Self: RandomAccessCollection {
161
161
Cannot partially sort with a negative amount of elements!
162
162
"""
163
163
)
164
-
165
164
assert ( k <= count, """
166
165
Cannot partially sort more than this Sequence's size!
167
166
"""
168
167
)
169
168
170
- guard k > 0 else {
171
- return
172
- }
173
- guard isEmpty == false else {
169
+ guard isEmpty == false && k > 0 else {
174
170
return
175
171
}
172
+
176
173
var heapEndIndex = 0
177
174
for i in ( count / 2 ) ..< count {
178
175
try siftDown ( i, by: areInIncreasingOrder, heapEndIndex: heapEndIndex)
179
176
}
180
- var iterator = ( 0 ..< k) . makeIterator ( )
181
- _ = iterator. next ( )
182
- swapAt ( index ( before: endIndex) , index ( startIndex, offsetBy: heapEndIndex) )
177
+
178
+ swapAt (
179
+ index ( before: endIndex) ,
180
+ index ( startIndex, offsetBy: heapEndIndex)
181
+ )
183
182
heapEndIndex += 1
184
- while let _ = iterator. next ( ) {
183
+
184
+ for i in 1 ..< k {
185
185
try siftDown (
186
186
count - 1 ,
187
187
by: areInIncreasingOrder,
188
188
heapEndIndex: heapEndIndex
189
189
)
190
- swapAt ( index ( before: endIndex) , index ( startIndex, offsetBy: heapEndIndex) )
190
+ swapAt (
191
+ index ( before: endIndex) ,
192
+ index ( startIndex, offsetBy: heapEndIndex)
193
+ )
191
194
heapEndIndex += 1
192
195
}
193
196
}
You can’t perform that action at this time.
0 commit comments