This file contains 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
//iterative | |
func binarySearch<T: Comparable>(_ a: [T], key: T) -> Int? { | |
var lowerBound = 0 | |
var upperBound = a.count | |
while lowerBound < upperBound { | |
let midIndex = lowerBound + (upperBound - lowerBound) / 2 | |
if a[midIndex] == key { | |
return midIndex | |
} else if a[midIndex] < key { | |
lowerBound = midIndex + 1 |
This file contains 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 class BinarySearchTree<T: Comparable> { | |
private(set) public var value: T | |
private(set) public var parent: BinarySearchTree? | |
private(set) public var left: BinarySearchTree? | |
private(set) public var right: BinarySearchTree? | |
public init(value: T) { | |
self.value = value | |
} |
This file contains 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
var randomNumbers = [5, 4, 3, 2, 1] | |
func selectionSort(nums: inout [Int]) -> [Int] { | |
// index of element we're trying to put in the leftmost position | |
for i in 0..<nums.count - 1 { | |
// set our smallest element to be the current index's element | |
var smallestElement = nums[i] | |
// set our smallest index to be the current index's element |
This file contains 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 bubbleSort(nums: inout [Int]) -> [Int] { | |
while true { | |
var swapped = false | |
for i in 0..<nums.count - 1 { | |
if nums[i] > nums[i + 1] { | |
let temp = nums[i] | |
nums[i] = nums[i + 1] | |
nums[i + 1] = temp |
This file contains 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
// Without threading | |
func slow() { | |
let methodStart = Date() | |
let url = URL(string: "some string url goes here for an image") | |
do { | |
let data = try Data(contentsOf: url!) | |
self.imageView.image = UIImage(data: data) | |
} catch { | |
print("error" |
This file contains 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
var noParametersOrReturn: () -> () = { | |
print("I have no parameters or return values, so I print" | |
} | |
// noParametersOrReturn() | |
var noParameterAndOneReturn: () -> String = { | |
return "I am a string" | |
} |
NewerOlder