Last active
November 11, 2017 16:32
-
-
Save volkanbicer/392468769143c0a36eacaae4a070fadd to your computer and use it in GitHub Desktop.
Implementation of insertion sort in Swift
This file contains 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
// swap | |
func insertionSort(_ a:[Int]) -> [Int]{ | |
var array = a | |
for i in 1..<array.count{ | |
var k = i | |
while k>0 && array[k-1] > array[k]{ | |
(array[k-1], array[k]) = (array[k], array[k-1]) | |
k -= 1 | |
} | |
} | |
return array | |
} | |
//without swap | |
func insertionSort(_ a:[Int]) -> [Int]{ | |
var array = a | |
for i in 1..<array.count{ | |
var k = i | |
var temp = a[k] | |
while k>0 && array[k-1] > array[k]{ | |
array[k] = array[k-1] | |
k -= 1 | |
} | |
array[k] = temp | |
} | |
return array | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment