├── .gitignore ├── BinarySearch.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ └── contents.xcworkspacedata ├── Fizzbuzz.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ └── contents.xcworkspacedata ├── InsertionSort.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ └── contents.xcworkspacedata ├── MergeSort.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ └── contents.xcworkspacedata ├── Q1_1_StringUniqueCharacters.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ └── zafer.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── Q1_8_ZeroMatrix.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ └── zafer.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── Q1_9_StringRotation.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ └── zafer.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── QuickSort.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ └── contents.xcworkspacedata └── SelectionSort.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace └── contents.xcworkspacedata /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | .DS_Store 3 | xcuserdata -------------------------------------------------------------------------------- /BinarySearch.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | func findMinimumIndexFor(_ array: [T], _ targetValue: T, _ guessedIndex: Int, _ minimumIndex: Int) -> Int { 4 | return (array[guessedIndex] < targetValue) ? guessedIndex + 1 : minimumIndex 5 | } 6 | 7 | func findMaximumIndexFor(_ array: [T], _ targetValue: T, _ guessedIndex: Int, _ maximumIndex: Int) -> Int { 8 | return (array[guessedIndex] > targetValue) ? guessedIndex - 1 : maximumIndex 9 | } 10 | 11 | func binarySearchInRange(array: [T], targetValue: T, minimumIndex: Int, maximumIndex: Int) -> Int { 12 | if minimumIndex > maximumIndex { 13 | return -1 14 | } 15 | let guessedIndex = (minimumIndex + maximumIndex) / 2 16 | // base case 17 | if array[guessedIndex] == targetValue { 18 | return guessedIndex 19 | } 20 | let newMinimumIndex = findMinimumIndexFor(array, targetValue, guessedIndex, minimumIndex) 21 | let newMaximumIndex = findMaximumIndexFor(array, targetValue, guessedIndex, maximumIndex) 22 | return binarySearchInRange(array: array, targetValue: targetValue, minimumIndex: newMinimumIndex, maximumIndex: newMaximumIndex) 23 | } 24 | 25 | func binarySearch(array: [T], targetValue: T) -> Int { 26 | return binarySearchInRange(array: array, targetValue: targetValue, minimumIndex: 0, maximumIndex: array.count - 1) 27 | } 28 | 29 | let numbers = [-1, 0, 1, 2, 5, 13, 15, 20, 45, 51, 59, 68, 77] 30 | let indexOfTargetValue = binarySearch(array: numbers, targetValue: 59) 31 | let indexOfTargetValueNotInNumbers = binarySearch(array: numbers, targetValue: 888) 32 | -------------------------------------------------------------------------------- /BinarySearch.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /BinarySearch.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Fizzbuzz.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | let thousandNumbers = [Int](1...1000) 4 | 5 | func printFizzBuzz(numbers: [Int]) { 6 | for i in numbers { 7 | if i % 15 == 0 { 8 | print("\(i) fizzbuzz") 9 | } 10 | else if i % 3 == 0 { 11 | print("\(i) fizz") 12 | } 13 | else if i % 5 == 0 { 14 | print("\(i) buzz") 15 | } 16 | else { 17 | print("\(i)") 18 | } 19 | } 20 | } 21 | 22 | printFizzBuzz(numbers: thousandNumbers) -------------------------------------------------------------------------------- /Fizzbuzz.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Fizzbuzz.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /InsertionSort.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | func insert(array: inout [T], rightIndex: Int, value: T) { 4 | var index = rightIndex 5 | 6 | while index >= 0 && value < array[index] { 7 | array[index+1] = array[index] 8 | index -= 1 9 | } 10 | array[index+1] = value 11 | } 12 | 13 | func insertionSort(array: inout [T]) { 14 | if array.isEmpty { 15 | return 16 | } 17 | 18 | for index in 1.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /InsertionSort.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MergeSort.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | func merge (array: inout [T], startIndex: Int, middleIndex: Int, endIndex: Int) { 4 | let leftSubarray = Array(array[startIndex...middleIndex]) 5 | let rightSubarray = Array(array[middleIndex+1...endIndex]) 6 | 7 | var index = startIndex 8 | var leftIndex = 0 9 | var rightIndex = 0 10 | 11 | while leftIndex < leftSubarray.count && rightIndex < rightSubarray.count { 12 | if leftSubarray[leftIndex] < rightSubarray[rightIndex] { 13 | array[index] = leftSubarray[leftIndex] 14 | leftIndex += 1 15 | } 16 | else { 17 | array[index] = rightSubarray[rightIndex] 18 | rightIndex += 1 19 | } 20 | index += 1 21 | } 22 | 23 | while leftIndex < leftSubarray.count { 24 | array[index] = leftSubarray[leftIndex] 25 | index += 1 26 | leftIndex += 1 27 | } 28 | 29 | while rightIndex < rightSubarray.count { 30 | array[index] = rightSubarray[rightIndex] 31 | index += 1 32 | rightIndex += 1 33 | } 34 | } 35 | 36 | func mergeSort(array: inout [T], startIndex: Int, endIndex: Int) { 37 | // Base case 38 | if startIndex >= endIndex { 39 | return 40 | } 41 | 42 | let middleIndex = (startIndex + endIndex) / 2 43 | mergeSort(array: &array, startIndex: startIndex, endIndex: middleIndex) 44 | mergeSort(array: &array, startIndex: middleIndex+1, endIndex: endIndex) 45 | merge(array: &array, startIndex: startIndex, middleIndex: middleIndex, endIndex: endIndex) 46 | } 47 | 48 | func mergeSort(array: inout [T]) { 49 | mergeSort(array: &array, startIndex: 0, endIndex: array.count-1) 50 | } 51 | 52 | var numbers = [13, 77, 20, 45, 2, 15, 0, 59, 5, 68, 51, 1, -1, 77] 53 | mergeSort(array: &numbers) 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /MergeSort.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /MergeSort.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Q1_1_StringUniqueCharacters.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | // Q 1.1 Implement an algorithm to determine if a sting has all unique characters. 4 | 5 | func isUniqueUTF8Characters(string: String) -> Bool { 6 | if string.characters.count > 256 { 7 | return false 8 | } 9 | 10 | var characterSet = [Bool](repeating: false, count: 256) 11 | 12 | for s in string.utf8 { 13 | let intValue: Int = Int(s) 14 | if characterSet[intValue] { 15 | return false 16 | } 17 | characterSet[intValue] = true 18 | } 19 | return true 20 | } 21 | 22 | isUniqueUTF8Characters(string: "SWIFT") 23 | isUniqueUTF8Characters(string: "Apple") 24 | 25 | 26 | // Alternative solution 27 | // For a string consists of lowercase letters a through z 28 | // You can reduce to space usage by using a bit-vector. 29 | 30 | func isUniqueLowercaseLetter(string: String) -> Bool { 31 | var checker: Int32 = 0 32 | 33 | for s in string.unicodeScalars { 34 | let value = Int32(s.value - ("a".unicodeScalars.first?.value)!) 35 | 36 | if (checker & (1 << value)) > 0 { 37 | return false 38 | } 39 | checker = checker | (1 << value) 40 | } 41 | 42 | return true 43 | } 44 | 45 | isUniqueLowercaseLetter(string: "swift") 46 | isUniqueLowercaseLetter(string: "apple") 47 | 48 | -------------------------------------------------------------------------------- /Q1_1_StringUniqueCharacters.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Q1_1_StringUniqueCharacters.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Q1_1_StringUniqueCharacters.playground/playground.xcworkspace/xcuserdata/zafer.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zafersevik/algorithms-in-swift/4466e376fe3d07235a6b21c021ce0d0b978f4f3e/Q1_1_StringUniqueCharacters.playground/playground.xcworkspace/xcuserdata/zafer.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Q1_8_ZeroMatrix.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | // Q 1.8 2 | // Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. 3 | 4 | var matrix = [ 5 | [1,2,0,4,5], 6 | [6,7,8,9,3], 7 | [1,2,0,4,5], 8 | [0,7,8,9,3] 9 | ] 10 | 11 | // Expected matrix after conversion 12 | // let zeroMatrix = [ 13 | // [0,0,0,0,0], 14 | // [0,7,0,9,3], 15 | // [0,0,0,0,0], 16 | // [0,0,0,0,0] 17 | // ] 18 | 19 | func convertToZeroMatrix(matrix: inout [[Int]]) { 20 | var zeroRows = [Bool](repeating: false, count: matrix.count) 21 | var zeroColumns = [Bool](repeating: false, count: matrix[0].count) 22 | 23 | // detect zero rows and columns 24 | for rowIndex in 0.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /Q1_8_ZeroMatrix.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Q1_8_ZeroMatrix.playground/playground.xcworkspace/xcuserdata/zafer.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zafersevik/algorithms-in-swift/4466e376fe3d07235a6b21c021ce0d0b978f4f3e/Q1_8_ZeroMatrix.playground/playground.xcworkspace/xcuserdata/zafer.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Q1_9_StringRotation.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | // Q 1.9 6 | // Assume you have a method isSubstring which checks if one word is a substring of another. 7 | // Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one 8 | // call to isSubstring (e.g. "waterbottle" is a rotation of "erbottlewat"). 9 | 10 | 11 | // All rotations could be found in waterbottlewaterbottle -> ...erbottlewat........ 12 | // Instead of isSubstring we could use String.contains in Swift 13 | func isRotation(s1: String, s2: String) -> Bool { 14 | 15 | if s1.characters.count != s2.characters.count { 16 | return false 17 | } 18 | 19 | let s3 = s1 + s1 20 | return s3.contains(s2) 21 | } 22 | 23 | isRotation(s1: "erbottlewat", s2: "waterbottle") 24 | isRotation(s1: "water", s2: "waterbottle") 25 | isRotation(s1: "erbottleaaa", s2: "waterbottle") 26 | isRotation(s1: "ewaterbottl", s2: "waterbottle") 27 | -------------------------------------------------------------------------------- /Q1_9_StringRotation.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Q1_9_StringRotation.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Q1_9_StringRotation.playground/playground.xcworkspace/xcuserdata/zafer.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zafersevik/algorithms-in-swift/4466e376fe3d07235a6b21c021ce0d0b978f4f3e/Q1_9_StringRotation.playground/playground.xcworkspace/xcuserdata/zafer.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /QuickSort.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | func swap(leftValue: inout T, rightValue: inout T) { 4 | (leftValue, rightValue) = (rightValue, leftValue) 5 | } 6 | 7 | func partition(array: inout [T], startIndex: Int, endIndex: Int) -> Int { 8 | var q = startIndex 9 | for index in startIndex..(array: inout [T], startIndex: Int, endIndex: Int) { 21 | // Base case 22 | if startIndex >= endIndex { 23 | return 24 | } 25 | let placedItemIndex = partition(array: &array, startIndex: startIndex, endIndex: endIndex) 26 | quickSort(array: &array, startIndex: startIndex, endIndex: placedItemIndex-1) 27 | quickSort(array: &array, startIndex: placedItemIndex+1, endIndex: endIndex) 28 | } 29 | 30 | func quickSort(array: inout [T]) { 31 | quickSort(array: &array, startIndex: 0, endIndex: array.count-1) 32 | } 33 | 34 | 35 | var numbers = [13, 77, 20, 45, 2, 15, 0, 59, 5, 68, 51, 1, -1, 77] 36 | quickSort(array: &numbers) 37 | -------------------------------------------------------------------------------- /QuickSort.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /QuickSort.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SelectionSort.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | func indexOfMinimumItem(array: [T], startIndex: Int) -> Int { 4 | var minimumItemIndex = startIndex 5 | for index in startIndex.. array[index] { 7 | minimumItemIndex = index 8 | } 9 | } 10 | return minimumItemIndex 11 | } 12 | 13 | func selectionSort(array: inout [T]) { 14 | for index in 0.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /SelectionSort.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | --------------------------------------------------------------------------------