diff --git a/01_introduction_to_algorithms/swift/01_binary_search.swift b/01_introduction_to_algorithms/swift/01_binary_search.swift index 232c552..91a0c9e 100644 --- a/01_introduction_to_algorithms/swift/01_binary_search.swift +++ b/01_introduction_to_algorithms/swift/01_binary_search.swift @@ -8,7 +8,7 @@ func binarySearch (_ list: [T], item: T) -> Int? { // While you haven't narrowed it down to one element ... while low <= high { //... check the middle element - let mid = low + (high - low) / 2 + let mid = (low + high) / 2 let guess = list[mid] // Found the item. if guess == item { diff --git a/02_selection_sort/swift/01_selection_sort.swift b/02_selection_sort/swift/01_selection_sort.swift index df6edc7..e34f805 100644 --- a/02_selection_sort/swift/01_selection_sort.swift +++ b/02_selection_sort/swift/01_selection_sort.swift @@ -4,13 +4,9 @@ import Foundation func findSmallestIndex (_ arr: [T]) -> Int { // Stores the smallest value var smallest = arr[0] - // We don't need any calculation if the array length is 1 - if arr.count == 1 { - return 0 - } // Stores the index of the smallest value var smallestIndex = 0 - for i in 1...arr.count-1 { + for i in 1.. (_ arr: [T]) -> Int { // Sort array func selectionSort (arr: [T]) -> [T] { + // We don't need any calculation if the array length is 1 + guard arr.count > 1 else { return arr } + var newArr: [T] = [] + // We have to make mutableArray reference copy of original array, because Swift 3 doesn't allow to get var parameter var mutableArr = arr - for _ in 0...mutableArr.count-1 { + for _ in 0..