Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update words to match the code. #999

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Insertion Sort/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func insertionSort(_ array: [Int]) -> [Int] {
}
```

The line at `//1` is what shifts up the previous elements by one position. At the end of the inner loop, `y` is the destination index for the new number in the sorted portion, and the line at `//2` copies this number into place.
The line at `//1` is what shifts up the previous elements by one position. At the end of the inner loop, `currentIndex` is the destination index for the new number in the sorted portion, and the line at `//2` copies this number into place.

## Making it generic

Expand All @@ -187,10 +187,10 @@ The new parameter `isOrderedBefore: (T, T) -> Bool` is a function that takes two
The only other change is in the inner loop, which now becomes:

```swift
while y > 0 && isOrderedBefore(temp, a[y - 1]) {
while currentIndex > 0 && isOrderedBefore(temp, sortedArray[currentIndex - 1]) {
```

Instead of writing `temp < a[y - 1]`, we call the `isOrderedBefore()` function. It does the exact same thing, except we can now compare any kind of object, not just numbers.
Instead of writing `temp < sortedArray[currentIndex - 1]`, we call the `isOrderedBefore()` function. It does the exact same thing, except we can now compare any kind of object, not just numbers.

To test this in a playground, do:

Expand Down