This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func recursiveFib(_ n: Int) -> Int { | |
| if n < 2 { | |
| return n | |
| } else { | |
| return recursiveFib(n - 2) + recursiveFib(n - 1) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Array where Element: Comparable { | |
| mutating func insertionSort() { | |
| for (i, element) in self.enumerated() { | |
| var i = i | |
| while i > 0 && element < self[i - 1] { | |
| self[i] = self[i - 1] | |
| i -= 1 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public struct Heap<Element> { | |
| var elements: [Element] | |
| var priorityFunction: (Element, Element) -> Bool | |
| init(elements: [Element] = [], priorityFunction: @escaping (Element, Element) -> Bool) { | |
| self.elements = elements | |
| self.priorityFunction = priorityFunction | |
| buildHeap() | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct HashTableOnlyKeys<Key: Hashable> { | |
| typealias Element = Key | |
| typealias List = SinglyLinkedList<Element> | |
| private var table: [List] | |
| private(set) public var count = 0 | |
| public var size: Int { | |
| return table.count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func shiftZeros(in array: inout [Int]) { | |
| var indexToPlaceZero = array.count - 1 | |
| for i in array.indices { | |
| if array[i] == 0 { | |
| while array[indexToPlaceZero] == 0, i < indexToPlaceZero { | |
| indexToPlaceZero -= 1 | |
| } | |
| array.swapAt(i, indexToPlaceZero) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func isPalindrome(_ list: DoublyLinkedList<String>) -> Bool { | |
| // Simple solution. Iterate over linked list and store as string. | |
| // Uses linear space and time. | |
| // Then compare string to a reversed string. | |
| // If equal, then we have a palindrome. | |
| var node = list.first | |
| var string = "" | |
| while node != nil { | |
| string += String(node!.element) | |
| node = node!.next |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Array where Element == Int { | |
| mutating func radixSort() { | |
| let radix = 10 | |
| var digit = 1 | |
| var done = false | |
| var unsortedArray = self | |
| while !done { | |
| done = true | |
| /// Counting Sort: Step 1 | |
| /// - Tally each number in count array. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Array where Element: Comparable { | |
| private func partition(array: inout [Element], low: Int, high: Int) -> Int { | |
| let pivot = array[high] | |
| var i = low - 1 | |
| for j in low..<high { | |
| if array[j] <= pivot { | |
| i += 1 | |
| array.swapAt(i, j) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func mergeSort<Element>(array: inout [Element]) where Element: Comparable { | |
| if array.count > 1 { | |
| let mid = array.count / 2 | |
| var L = Array(array[..<mid]) | |
| var R = Array(array[mid...]) | |
| mergeSort(array: &L) | |
| mergeSort(array: &R) | |
| var i = 0, j = 0, k = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func heapify<Element>(_ array: inout [Element], _ count: Int, _ index: Int) where Element: Comparable { | |
| var largest = index | |
| let l = 2 * index + 1 | |
| let r = 2 * index + 2 | |
| //// Check if left child exists and is greater than parent. | |
| if l < count && array[index] < array[l] { | |
| largest = l | |
| } | |