-
Notifications
You must be signed in to change notification settings - Fork 5k
Insersion sort: rename variables with convenience names #887
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
Changes from 3 commits
eb9424d
92e797a
6dc91c4
70fdd27
b47e620
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <playground version='5.0' target-platform='osx' executeOnSourceChanges='false'> | ||
| <playground version='5.0' target-platform='osx'> | ||
| <timeline fileName='timeline.xctimeline'/> | ||
| </playground> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you clarify what you mean by making them vertical |
||
| 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 | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| currentIndex -= 1 | ||
| } | ||
| a[y] = temp // 2 | ||
| sortedArray[currentIndex] = temp // 2 | ||
| } | ||
| return a | ||
| return sortedArray | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.