Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
abuzeid ibrahim committed Sep 3, 2019
2 parents 70fdd27 + 6dc91c4 commit b47e620
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions Insertion Sort/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ Here is an implementation of insertion sort in Swift:

```swift
func insertionSort(_ array: [Int]) -> [Int] {
var a = array // 1
for x in 1..<a.count { // 2
var y = x
while y > 0 && a[y] < a[y - 1] { // 3
a.swapAt(y - 1, y)
y -= 1
var sortedArray = array // 1
for index in 1..<sortedArray.count { // 2
var currentIndex = index
while currentIndex > 0 && sortedArray[currentIndex] < sortedArray[currentIndex - 1] { // 3
sortedArray.swapAt(currentIndex - 1, currentIndex)
currentIndex -= 1
}
}
return a
return sortedArray
}


Expand Down Expand Up @@ -154,17 +154,17 @@ In code that looks like this:

```swift
func insertionSort(_ array: [Int]) -> [Int] {
var a = array
for x in 1..<a.count {
var y = x
let temp = a[y]
while y > 0 && temp < a[y - 1] {
a[y] = a[y - 1] // 1
y -= 1
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] // 1
currentIndex -= 1
}
a[y] = temp // 2
sortedArray[currentIndex] = temp // 2
}
return a
return sortedArray
}
```

Expand Down

0 comments on commit b47e620

Please sign in to comment.