Skip to content

Add - AVL Tree, Binary Search Tree & Heap Sort. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions sorts/HeapSort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extension Heap {
public mutating func sort() -> [T] {
for i in stride(from: (nodes.count - 1), through: 1, by: -1) {
nodes.swapAt(0, i)
shiftDown(from: 0, until: i)
}
return nodes
}
}

/*
Sorts an array using a heap.
Heapsort can be performed in-place, but it is not a stable sort.
*/
public func heapsort<T>(_ a: [T], _ sort: @escaping (T, T) -> Bool) -> [T] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test for the implementation is missing. Add test cases for demonstration.

let reverseOrder = { i1, i2 in sort(i2, i1) }
var h = Heap(array: a, sort: reverseOrder)
return h.sort()
}
Loading