Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: RedQueenCoder/swift-algorithm-club
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: Swift3BoundedPriorityQueue
Choose a base ref
...
head repository: RedQueenCoder/swift-algorithm-club
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
Loading
Showing with 1,864 additions and 1,758 deletions.
  1. +1 −1 .travis.yml
  2. +1 −1 ...h-First Search/BreadthFirstSearch.playground/Pages/Simple example.xcplaygroundpage/Contents.swift
  3. +2 −2 Breadth-First Search/BreadthFirstSearch.playground/Sources/Edge.swift
  4. +6 −6 Breadth-First Search/BreadthFirstSearch.playground/Sources/Graph.swift
  5. +4 −4 Breadth-First Search/BreadthFirstSearch.playground/Sources/Node.swift
  6. +1 −1 Breadth-First Search/BreadthFirstSearch.playground/Sources/Queue.swift
  7. +1 −1 Breadth-First Search/BreadthFirstSearch.swift
  8. +1 −1 Breadth-First Search/README.markdown
  9. +6 −5 Brute-Force String Search/BruteForceStringSearch.playground/Contents.swift
  10. +0 −6 Brute-Force String Search/BruteForceStringSearch.playground/timeline.xctimeline
  11. +14 −14 Brute-Force String Search/BruteForceStringSearch.swift
  12. +14 −14 Brute-Force String Search/README.markdown
  13. +5 −5 Counting Sort/CountingSort.playground/Contents.swift
  14. +4 −4 Counting Sort/README.markdown
  15. +1 −1 Depth-First Search/DepthFirstSearch.playground/Pages/Simple Example.xcplaygroundpage/Contents.swift
  16. +0 −6 ...irst Search/DepthFirstSearch.playground/Pages/Simple Example.xcplaygroundpage/timeline.xctimeline
  17. +2 −2 Depth-First Search/DepthFirstSearch.playground/Sources/Edge.swift
  18. +6 −6 Depth-First Search/DepthFirstSearch.playground/Sources/Graph.swift
  19. +4 −4 Depth-First Search/DepthFirstSearch.playground/Sources/Node.swift
  20. +1 −1 Depth-First Search/DepthFirstSearch.swift
  21. +1 −1 Depth-First Search/README.markdown
  22. +3 −3 Deque/Deque-Optimized.swift
  23. +2 −2 Deque/Deque-Simple.swift
  24. +3 −3 Deque/Deque.playground/Contents.swift
  25. +0 −6 Deque/Deque.playground/timeline.xctimeline
  26. +5 −5 Deque/README.markdown
  27. +1 −1 Fizz Buzz/FizzBuzz.playground/Contents.swift
  28. +7 −0 Fizz Buzz/FizzBuzz.playground/playground.xcworkspace/contents.xcworkspacedata
  29. +0 −6 Fizz Buzz/FizzBuzz.playground/timeline.xctimeline
  30. +1 −1 Fizz Buzz/FizzBuzz.swift
  31. +1 −1 Fizz Buzz/README.markdown
  32. +2 −2 GCD/GCD.playground/Contents.swift
  33. +3 −3 GCD/GCD.swift
  34. +3 −3 GCD/README.markdown
  35. +10 −105 Hash Table/HashTable.playground/Contents.swift
  36. +153 −0 Hash Table/HashTable.playground/Sources/HashTable.swift
  37. +0 −6 Hash Table/HashTable.playground/timeline.xctimeline
  38. +0 −159 Hash Table/HashTable.swift
  39. +19 −22 Hash Table/README.markdown
  40. +17 −17 Kth Largest Element/kthLargest.playground/Contents.swift
  41. +30 −0 ...rgest Element/kthLargest.playground/playground.xcworkspace/xcshareddata/kthLargest.xcscmblueprint
  42. +0 −6 Kth Largest Element/kthLargest.playground/timeline.xctimeline
  43. +27 −20 Linked List/LinkedList.playground/Contents.swift
  44. +58 −51 Linked List/LinkedList.swift
  45. +16 −16 Linked List/Tests/LinkedListTests.swift
  46. +9 −9 Longest Common Subsequence/LongestCommonSubsequence.playground/Contents.swift
  47. +1 −1 Longest Common Subsequence/LongestCommonSubsequence.playground/contents.xcplayground
  48. +10 −9 Longest Common Subsequence/LongestCommonSubsequence.swift
  49. +15 −16 Longest Common Subsequence/README.markdown
  50. +40 −1 Longest Common Subsequence/Tests/Tests.xcodeproj/project.pbxproj
  51. +1 −1 Longest Common Subsequence/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme
  52. +1 −1 Monty Hall Problem/MontyHall.playground/Contents.swift
  53. +0 −6 Monty Hall Problem/MontyHall.playground/timeline.xctimeline
  54. +19 −17 Ordered Array/OrderedArray.playground/Contents.swift
  55. +27 −25 Ordered Array/README.markdown
  56. +1 −1 Ordered Set/OrderedSet.playground/Pages/Example 1.xcplaygroundpage/Contents.swift
  57. +21 −0 Ordered Set/OrderedSet.playground/Pages/Example 1.xcplaygroundpage/timeline.xctimeline
  58. +1 −1 Ordered Set/OrderedSet.playground/Pages/Example 2.xcplaygroundpage/Contents.swift
  59. +3 −3 Ordered Set/OrderedSet.playground/Pages/Example 3.xcplaygroundpage/Contents.swift
  60. 0 ...undpage → Ordered Set/OrderedSet.playground/Pages/Example 3.xcplaygroundpage}/timeline.xctimeline
  61. +12 −12 Ordered Set/OrderedSet.playground/Sources/OrderedSet.swift
  62. +1 −1 Ordered Set/OrderedSet.playground/Sources/Player.swift
  63. +1 −1 Ordered Set/OrderedSet.playground/Sources/Random.swift
  64. +20 −20 Ordered Set/README.markdown
  65. +3 −5 Palindromes/Palindromes.playground/Contents.swift
  66. +1 −5 Palindromes/Palindromes.swift
  67. +5 −5 Priority Queue/PriorityQueue.swift
  68. +2 −2 Priority Queue/README.markdown
  69. +9 −1 Priority Queue/Tests/Tests.xcodeproj/project.pbxproj
  70. +1 −1 Priority Queue/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme
  71. +19 −19 Radix Tree/RadixTree.playground/Sources/RadixTree.swift
  72. +365 −365 Radix Tree/RadixTree.swift
  73. +0 −459 Red-Black Tree/RBTree.swift
  74. +119 −0 Red-Black Tree/README.markdown
  75. +0 −69 Red-Black Tree/README.md
  76. +20 −0 Red-Black Tree/Red-Black Tree 2.playground/Contents.swift
  77. +521 −0 Red-Black Tree/Red-Black Tree 2.playground/Sources/RBTree.swift
  78. +4 −0 Red-Black Tree/Red-Black Tree 2.playground/contents.xcplayground
  79. +7 −0 Red-Black Tree/Red-Black Tree 2.playground/playground.xcworkspace/contents.xcworkspacedata
  80. BIN Red-Black Tree/files/fig1.png
  81. BIN Red-Black Tree/files/fig2.png
  82. +14 −14 Segment Tree/README.markdown
  83. +96 −96 Segment Tree/SegmentTree.playground/Contents.swift
  84. +1 −1 Segment Tree/SegmentTree.playground/contents.xcplayground
  85. +0 −6 Segment Tree/SegmentTree.playground/timeline.xctimeline
  86. +58 −58 Segment Tree/SegmentTree.swift
  87. +0 −3 swift-algorithm-club.xcworkspace/contents.xcworkspacedata
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ script:
- 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
# - xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var queue = Queue<Node>()
queue.enqueue(source)

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
public class Edge: Equatable {
public var neighbor: Node

public init(neighbor: Node) {
public init(_ neighbor: Node) {
self.neighbor = neighbor
}
}

public func == (lhs: Edge, rhs: Edge) -> Bool {
public func == (_ lhs: Edge, rhs: Edge) -> Bool {
return lhs.neighbor == rhs.neighbor
}
Original file line number Diff line number Diff line change
@@ -5,14 +5,14 @@ public class Graph: CustomStringConvertible, Equatable {
self.nodes = []
}

public func addNode(label: String) -> Node {
let node = Node(label: label)
public func addNode(_ label: String) -> Node {
let node = Node(label)
nodes.append(node)
return node
}

public func addEdge(source: Node, neighbor: Node) {
let edge = Edge(neighbor: neighbor)
public func addEdge(_ source: Node, neighbor: Node) {
let edge = Edge(neighbor)
source.neighbors.append(edge)
}

@@ -27,7 +27,7 @@ public class Graph: CustomStringConvertible, Equatable {
return description
}

public func findNodeWithLabel(label: String) -> Node {
public func findNodeWithLabel(_ label: String) -> Node {
return nodes.filter { $0.label == label }.first!
}

@@ -50,6 +50,6 @@ public class Graph: CustomStringConvertible, Equatable {
}
}

public func == (lhs: Graph, rhs: Graph) -> Bool {
public func == (_ lhs: Graph, rhs: Graph) -> Bool {
return lhs.nodes == rhs.nodes
}
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ public class Node: CustomStringConvertible, Equatable {
public var distance: Int?
public var visited: Bool

public init(label: String) {
public init(_ label: String) {
self.label = label
neighbors = []
visited = false
@@ -22,11 +22,11 @@ public class Node: CustomStringConvertible, Equatable {
return distance != nil
}

public func remove(edge: Edge) {
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
public func remove(_ edge: Edge) {
neighbors.remove(at: neighbors.index { $0 === edge }!)
}
}

public func == (lhs: Node, rhs: Node) -> Bool {
public func == (_ lhs: Node, rhs: Node) -> Bool {
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
}
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ public struct Queue<T> {
return array.count
}

public mutating func enqueue(element: T) {
public mutating func enqueue(_ element: T) {
array.append(element)
}

2 changes: 1 addition & 1 deletion Breadth-First Search/BreadthFirstSearch.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var queue = Queue<Node>()
queue.enqueue(source)

2 changes: 1 addition & 1 deletion Breadth-First Search/README.markdown
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ For an unweighted graph, this tree defines a shortest path from the starting nod
Simple implementation of breadth-first search using a queue:

```swift
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var queue = Queue<Node>()
queue.enqueue(source)

Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//: Playground - noun: a place where people can play

extension String {
func indexOf(pattern: String) -> String.Index? {
for i in self.startIndex ..< self.endIndex {
func indexOf(_ pattern: String) -> String.Index? {

for i in self.characters.indices {
var j = i
var found = true
for p in pattern.startIndex ..< pattern.endIndex {
if j == self.endIndex || self[j] != pattern[p] {
for p in pattern.characters.indices{
if j == self.characters.endIndex || self[j] != pattern[p] {
found = false
break
} else {
j = j.successor()
j = self.characters.index(after: j)
}
}
if found {

This file was deleted.

28 changes: 14 additions & 14 deletions Brute-Force String Search/BruteForceStringSearch.swift
Original file line number Diff line number Diff line change
@@ -2,21 +2,21 @@
Brute-force string search
*/
extension String {
func indexOf(pattern: String) -> String.Index? {
for i in self.startIndex ..< self.endIndex {
var j = i
var found = true
for p in pattern.startIndex ..< pattern.endIndex {
if j == self.endIndex || self[j] != pattern[p] {
found = false
break
} else {
j = j.successor()
func indexOf(_ pattern: String) -> String.Index? {
for i in self.characters.indices {
var j = i
var found = true
for p in pattern.characters.indices{
if j == self.characters.endIndex || self[j] != pattern[p] {
found = false
break
} else {
j = self.characters.index(after: j)
}
}
if found {
return i
}
}
if found {
return i
}
}
return nil
}
28 changes: 14 additions & 14 deletions Brute-Force String Search/README.markdown
Original file line number Diff line number Diff line change
@@ -28,21 +28,21 @@ Here is a brute-force solution:

```swift
extension String {
func indexOf(pattern: String) -> String.Index? {
for i in self.startIndex ..< self.endIndex {
var j = i
var found = true
for p in pattern.startIndex ..< pattern.endIndex {
if j == self.endIndex || self[j] != pattern[p] {
found = false
break
} else {
j = j.successor()
func indexOf(_ pattern: String) -> String.Index? {
for i in self.characters.indices {
var j = i
var found = true
for p in pattern.characters.indices{
if j == self.characters.endIndex || self[j] != pattern[p] {
found = false
break
} else {
j = self.characters.index(after: j)
}
}
if found {
return i
}
}
if found {
return i
}
}
return nil
}
10 changes: 5 additions & 5 deletions Counting Sort/CountingSort.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//: Playground - noun: a place where people can play

enum CountingSortError: ErrorType {
enum CountingSortError: Error {
case ArrayEmpty
}

@@ -11,9 +11,9 @@ func countingSort(array: [Int]) throws -> [Int] {

// Step 1
// Create an array to store the count of each element
let maxElement = array.maxElement() ?? 0
let maxElement = array.max() ?? 0

var countArray = [Int](count: Int(maxElement + 1), repeatedValue: 0)
var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
for element in array {
countArray[element] += 1
}
@@ -29,7 +29,7 @@ func countingSort(array: [Int]) throws -> [Int] {

// Step 3
// Place the element in the final array as per the number of elements before it
var sortedArray = [Int](count: array.count, repeatedValue: 0)
var sortedArray = [Int](repeating: 0, count: array.count)
for element in array {
countArray[element] -= 1
sortedArray[countArray[element]] = element
@@ -38,4 +38,4 @@ func countingSort(array: [Int]) throws -> [Int] {
}


try countingSort([10, 9, 8, 7, 1, 2, 7, 3])
try countingSort(array: [10, 9, 8, 7, 1, 2, 7, 3])
8 changes: 4 additions & 4 deletions Counting Sort/README.markdown
Original file line number Diff line number Diff line change
@@ -20,9 +20,9 @@ Count 0 1 1 1 0 0 0 2 1 1 1
Here is the code to accomplish this:

```swift
let maxElement = array.maxElement() ?? 0
var countArray = [Int](count: Int(maxElement + 1), repeatedValue: 0)
let maxElement = array.max() ?? 0

var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
for element in array {
countArray[element] += 1
}
@@ -62,7 +62,7 @@ Output 1 2 3 7 7 8 9 10
Here is the code for this final step:

```swift
var sortedArray = [Int](count: array.count, repeatedValue: 0)
var sortedArray = [Int](repeating: 0, count: array.count)
for element in array {
countArray[element] -= 1
sortedArray[countArray[element]] = element
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var nodesExplored = [source.label]
source.visited = true

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
public class Edge: Equatable {
public var neighbor: Node

public init(neighbor: Node) {
public init(_ neighbor: Node) {
self.neighbor = neighbor
}
}

public func == (lhs: Edge, rhs: Edge) -> Bool {
public func == (_ lhs: Edge, rhs: Edge) -> Bool {
return lhs.neighbor == rhs.neighbor
}
Original file line number Diff line number Diff line change
@@ -5,14 +5,14 @@ public class Graph: CustomStringConvertible, Equatable {
self.nodes = []
}

public func addNode(label: String) -> Node {
let node = Node(label: label)
public func addNode(_ label: String) -> Node {
let node = Node(label)
nodes.append(node)
return node
}

public func addEdge(source: Node, neighbor: Node) {
let edge = Edge(neighbor: neighbor)
public func addEdge(_ source: Node, neighbor: Node) {
let edge = Edge(neighbor)
source.neighbors.append(edge)
}

@@ -27,7 +27,7 @@ public class Graph: CustomStringConvertible, Equatable {
return description
}

public func findNodeWithLabel(label: String) -> Node {
public func findNodeWithLabel(_ label: String) -> Node {
return nodes.filter { $0.label == label }.first!
}

@@ -50,6 +50,6 @@ public class Graph: CustomStringConvertible, Equatable {
}
}

public func == (lhs: Graph, rhs: Graph) -> Bool {
public func == (_ lhs: Graph, rhs: Graph) -> Bool {
return lhs.nodes == rhs.nodes
}
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ public class Node: CustomStringConvertible, Equatable {
public var distance: Int?
public var visited: Bool

public init(label: String) {
public init(_ label: String) {
self.label = label
neighbors = []
visited = false
@@ -22,11 +22,11 @@ public class Node: CustomStringConvertible, Equatable {
return distance != nil
}

public func remove(edge: Edge) {
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
public func remove(_ edge: Edge) {
neighbors.remove(at: neighbors.index { $0 === edge }!)
}
}

public func == (lhs: Node, rhs: Node) -> Bool {
public func == (_ lhs: Node, rhs: Node) -> Bool {
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
}
2 changes: 1 addition & 1 deletion Depth-First Search/DepthFirstSearch.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var nodesExplored = [source.label]
source.visited = true

2 changes: 1 addition & 1 deletion Depth-First Search/README.markdown
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ The parent of a node is the one that "discovered" that node. The root of the tre
Simple recursive implementation of depth-first search:

```swift
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var nodesExplored = [source.label]
source.visited = true

6 changes: 3 additions & 3 deletions Deque/Deque-Optimized.swift
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ public struct Deque<T> {
private var head: Int
private var capacity: Int

public init(capacity: Int = 10) {
public init(_ capacity: Int = 10) {
self.capacity = max(capacity, 1)
array = .init(count: capacity, repeatedValue: nil)
head = capacity
@@ -22,11 +22,11 @@ public struct Deque<T> {
return array.count - head
}

public mutating func enqueue(element: T) {
public mutating func enqueue(_ element: T) {
array.append(element)
}

public mutating func enqueueFront(element: T) {
public mutating func enqueueFront(_ element: T) {
if head == 0 {
capacity *= 2
let emptySpace = [T?](count: capacity, repeatedValue: nil)
Loading