Skip to content

Commit

Permalink
Merge pull request #984 from denizcoskun/patch-1
Browse files Browse the repository at this point in the history
declare 'index' as variable
  • Loading branch information
kelvinlauKL authored Jan 6, 2022
2 parents 744ff19 + 90e23a6 commit e592ed6
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 e592ed6

Please sign in to comment.