Skip to content

Commit

Permalink
Merge pull request #334 from mattThousand/swift-3-heap-sort
Browse files Browse the repository at this point in the history
Updating Heap Sort to Swift 3
  • Loading branch information
kelvinlauKL authored Jan 3, 2017
2 parents 429237e + 6a15e73 commit 82f5750
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ script:
# - xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests
# - xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
Expand Down
6 changes: 3 additions & 3 deletions Heap Sort/HeapSort.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extension Heap {
public mutating func sort() -> [T] {
for i in (elements.count - 1).stride(through: 1, by: -1) {
for i in stride(from: (elements.count - 1), through: 1, by: -1) {
swap(&elements[0], &elements[i])
shiftDown(index: 0, heapSize: i)
shiftDown(0, heapSize: i)
}
return elements
}
Expand All @@ -12,7 +12,7 @@ extension Heap {
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: (T, T) -> Bool) -> [T] {
public func heapsort<T>(_ a: [T], _ sort: @escaping (T, T) -> Bool) -> [T] {
let reverseOrder = { i1, i2 in sort(i2, i1) }
var h = Heap(array: a, sort: reverseOrder)
return h.sort()
Expand Down
20 changes: 10 additions & 10 deletions Heap Sort/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ Here's how you can implement heap sort in Swift:

```swift
extension Heap {
public mutating func sort() -> [T] {
for i in (elements.count - 1).stride(through: 1, by: -1) {
swap(&elements[0], &elements[i])
shiftDown(index: 0, heapSize: i)
}
public mutating func sort() -> [T] {
for i in stride(from: (elements.count - 1), through: 1, by: -1) {
swap(&elements[0], &elements[i])
shiftDown(0, heapSize: i)
}
return elements
}
}
}
```

Expand All @@ -70,10 +70,10 @@ Because we need a max-heap to sort from low-to-high, you need to give `Heap` the
We can write a handy helper function for that:

```swift
public func heapsort<T>(a: [T], _ sort: (T, T) -> Bool) -> [T] {
let reverseOrder = { i1, i2 in sort(i2, i1) }
var h = Heap(array: a, sort: reverseOrder)
return h.sort()
public func heapsort<T>(_ a: [T], _ sort: @escaping (T, T) -> Bool) -> [T] {
let reverseOrder = { i1, i2 in sort(i2, i1) }
var h = Heap(array: a, sort: reverseOrder)
return h.sort()
}
```

Expand Down
10 changes: 9 additions & 1 deletion Heap Sort/Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = "Swift Algorithm Club";
TargetAttributes = {
7B2BBC7F1C779D720067B71D = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -149,8 +150,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down Expand Up @@ -193,8 +196,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand All @@ -213,6 +218,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
Expand All @@ -224,6 +230,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -235,6 +242,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
18 changes: 9 additions & 9 deletions Heap/Heap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct Heap<T> {
fileprivate mutating func buildHeap(fromArray array: [T]) {
elements = array
for i in stride(from: (elements.count/2 - 1), through: 0, by: -1) {
shiftDown(index: i, heapSize: elements.count)
shiftDown(i, heapSize: elements.count)
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ public struct Heap<T> {
*/
public mutating func insert(_ value: T) {
elements.append(value)
shiftUp(index: elements.count - 1)
shiftUp(elements.count - 1)
}

public mutating func insert<S: Sequence>(_ sequence: S) where S.Iterator.Element == T {
Expand All @@ -116,7 +116,7 @@ public struct Heap<T> {

assert(isOrderedBefore(value, elements[i]))
elements[i] = value
shiftUp(index: i)
shiftUp(i)
}

/**
Expand All @@ -142,14 +142,14 @@ public struct Heap<T> {
* Removes an arbitrary node from the heap. Performance: O(log n). You need
* to know the node's index, which may actually take O(n) steps to find.
*/
public mutating func removeAt(index: Int) -> T? {
public mutating func removeAt(_ index: Int) -> T? {
guard index < elements.count else { return nil }

let size = elements.count - 1
if index != size {
swap(&elements[index], &elements[size])
shiftDown(index: index, heapSize: size)
shiftUp(index: index)
shiftDown(index, heapSize: size)
shiftUp(index)
}
return elements.removeLast()
}
Expand All @@ -158,7 +158,7 @@ public struct Heap<T> {
* Takes a child node and looks at its parents; if a parent is not larger
* (max-heap) or not smaller (min-heap) than the child, we exchange them.
*/
mutating func shiftUp(index: Int) {
mutating func shiftUp(_ index: Int) {
var childIndex = index
let child = elements[childIndex]
var parentIndex = self.parentIndex(ofIndex: childIndex)
Expand All @@ -173,14 +173,14 @@ public struct Heap<T> {
}

mutating func shiftDown() {
shiftDown(index: 0, heapSize: elements.count)
shiftDown(0, heapSize: elements.count)
}

/**
* Looks at a parent node and makes sure it is still larger (max-heap) or
* smaller (min-heap) than its childeren.
*/
mutating func shiftDown(index: Int, heapSize: Int) {
mutating func shiftDown(_ index: Int, heapSize: Int) {
var parentIndex = index

while true {
Expand Down

0 comments on commit 82f5750

Please sign in to comment.