We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 744ff19 + 90e23a6 commit e592ed6Copy full SHA for e592ed6
Insertion Sort/InsertionSort.swift
@@ -28,14 +28,13 @@ func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
28
guard array.count > 1 else { return array }
29
30
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
+ 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
37
}
38
- sortedArray[currentIndex] = temp
+ sortedArray[index] = temp
39
40
return sortedArray
41
0 commit comments