You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Basically we created wrapper around standard Array type. However we need to carefully take a look at the two important pieces of the implenentation:
152
-
- T type which is a generic placeholder
153
-
- Custom initializer that accepts iterator element of Sequence protocol - this is simply a complementary initializer that allows client-developer to pass other Stack structs, Arra or any other type that conforms to Sequnce protocol to use it at initialization phase. Convenience and compatability with standard library - nothing sophisticated.
154
-
155
-
156
-
The following extension adds support for debugging capabilites for both regular "print" and "debugPrint" statements
Adds support for Sequence protocol. The Swift's runtime will call the makeIterator() method to initialize the for...in loop. All we need to do is to return some soft of iterator instance that conforms to IteratorProtocol. Iterator protocol allows us to return an iterator based on the type of elements out target type contains - in this particular case it is Stack.
// MARK: - Heap Sort. Should be decomposed into separete file for sorting algorithms.
226
-
// - Note that it is very similar to Selection sort approach.
227
-
// - Complexity of the sorting algorithms is O(n log n) in best, worst and average cases. The heap is traversed once (which is a list and is O(n)) and then every time the elements are swapped, a shift down operation is performed which is O(log n). As a result we have O(n log n) coomplexity.
228
-
extensionHeap{
229
-
func sorted()->[T]{
230
-
varheap=Heap(array: nodes, order: order)
231
-
232
-
forindexinself.nodes.indices.reversed(){
233
-
heap.nodes.swapAt(0, index)
234
-
heap.shiftDown(from:0, until: index)
235
-
}
236
-
return heap.nodes
237
-
}
238
-
}
239
-
240
-
241
-
// MARK: - Searching
242
-
extensionHeapwhere T:Equatable{
243
-
244
-
/// Gets the index of a node from the Heap
245
-
///
246
-
/// - Parameter node: is a Node of type T that is going to be searched
0 commit comments