Created
June 13, 2019 01:35
-
-
Save davidinga/d947e8b1fba009822e8700f709ad746a to your computer and use it in GitHub Desktop.
Extends Swift's Array data type to include Insertion Sort Algorithm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Array where Element: Comparable { | |
| mutating func insertionSort() { | |
| for (i, element) in self.enumerated() { | |
| var i = i | |
| while i > 0 && element < self[i - 1] { | |
| self[i] = self[i - 1] | |
| i -= 1 | |
| } | |
| self[i] = element | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment