Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e592ed6

Browse files
Merge pull request #984 from denizcoskun/patch-1
declare 'index' as variable
2 parents 744ff19 + 90e23a6 commit e592ed6

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

‎Insertion Sort/InsertionSort.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
2828
guard array.count > 1 else { return array }
2929

3030
var sortedArray = array
31-
for index in 1..<sortedArray.count {
32-
var currentIndex = index
33-
let temp = sortedArray[currentIndex]
34-
while currentIndex > 0, temp < sortedArray[currentIndex - 1] {
35-
sortedArray[currentIndex] = sortedArray[currentIndex - 1]
36-
currentIndex -= 1
31+
for var index in 1..<sortedArray.count {
32+
let temp = sortedArray[index]
33+
while index > 0, temp < sortedArray[index - 1] {
34+
sortedArray[index] = sortedArray[index - 1]
35+
index -= 1
3736
}
38-
sortedArray[currentIndex] = temp
37+
sortedArray[index] = temp
3938
}
4039
return sortedArray
4140
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /