In Swift, everything is implemented as a struct rather than a class. Values are being passed in, but the memory address does not store them. Here is an example:
import UIKit
var array = Array<Int>()
array = [6,2,9,1,10,4,9,3,5,8,7]
let count = array.count
func insertSort(var arr : Array<Int>)->(){
for(var i = 1; i<arr.count-1; i++){
var j : Int
for( j = i; j > 0; j--){
if(arr[j]<arr[j-1]){
var temp = arr[j-1]
var temp2 = arr[j]
arr.removeAtIndex(j-1)
arr.insert(temp2, atIndex: j-1)
arr.removeAtIndex(j)
arr.insert(temp, atIndex: j)
}
}
}
for item in arr{
print(item)
}
}
insertSort(array)
array
I know that this isn't the most concise way to implement an insertion sort, but I was just trying to use the most Swift I could.
My issue: through the print statement in the function, I can see that the algorithm works - the list is returned sorted. However, when I print the array at the bottom, Playground returns the same array as the one I declared at the top, ie unsorted. I know why that is (the array is not implemented as a class), I just wanted to know if there is a way to fix it.