Skip to content

Commit

Permalink
declare 'index' as variable
Browse files Browse the repository at this point in the history
  • Loading branch information
denizcoskun authored Aug 21, 2021
1 parent 5d55104 commit 90e23a6
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions Insertion Sort/InsertionSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
guard array.count > 1 else { return array }

var sortedArray = array
for index in 1..<sortedArray.count {
var currentIndex = index
let temp = sortedArray[currentIndex]
while currentIndex > 0, temp < sortedArray[currentIndex - 1] {
sortedArray[currentIndex] = sortedArray[currentIndex - 1]
currentIndex -= 1
for var index in 1..<sortedArray.count {
let temp = sortedArray[index]
while index > 0, temp < sortedArray[index - 1] {
sortedArray[index] = sortedArray[index - 1]
index -= 1
}
sortedArray[currentIndex] = temp
sortedArray[index] = temp
}
return sortedArray
}

0 comments on commit 90e23a6

Please sign in to comment.