Swift为InsertionSort算法修改Int数组

时间:2015-07-04 15:37:56

标签: arrays swift sorting

我对Swift很新,我想知道我应该如何修改Int中包含的Int[]?我这样做就像我用Java做的那样但显然它不起作用:我得到Cannot assign to immutable value of type 'Int'编译错误。

这是我的代码:

import Foundation

func createRandomArray(length: Int) -> [Int]{
     var random = [Int]()
     for index in 0...length{
         random.append(Int(arc4random_uniform(100)))
     }
     return random
}

func insertionSort(toSort: [Int]) -> [Int]{
    var length = toSort.count
    for index in 1...length-1{
        var key = toSort[index]
        var previous = index-1
        while(previous>0 && toSort[previous]>key){
            // ERROR IS THERE
            toSort[previous+1] = toSort[previous]
            previous--
        }
    }
    return toSort
}

var array = createRandomArray(10)

print(array)

print(insertionSort(array))

我认为使用var代替let会产生一个可变对象,这就是为什么我有点迷失。

感谢您的帮助

修改

我得到了这个有效的答案,感谢@vacawama:

import Foundation

func createRandomArray(length: Int) -> [Int]{
     var random = [Int]()
     for index in 0...length{
         random.append(Int(arc4random_uniform(100)))
     }
     return random
}

func insertionSort(var toSort: [Int]) -> [Int]{
    for firstIterator in 1...toSort.count-1{
        var currentValue = toSort[firstIterator]
        var previousPosition = firstIterator-1
        while(previousPosition>=0 && toSort[previousPosition]>currentValue){
            swap(&toSort[previousPosition+1], &toSort[previousPosition])
            previousPosition--
        }
    }
    return toSort
}


var array = createRandomArray(10)

print(array)

print(insertionSort(array))

3 个答案:

答案 0 :(得分:1)

默认情况下,数组参数在被调用函数中是不可变的。如果要修改传递给insertionSort的数组的副本,请在函数头中的var之前添加toSort

func insertionSort(var toSort: [Int]) -> [Int]{

答案 1 :(得分:0)

我使用swift 3玩下面的内容。希望它能帮助一些来这里的人。

import Cocoa

func ints(cnt: Int, ceiling: Int) -> [Int] {
    var arr = [Int]()
    for _ in 0 ..< cnt {
        arr.append(Int(arc4random_uniform(UInt32(ceiling))))
    }
    return arr
}

func insertion(arr: inout [Int]) {
    for i in 1..<arr.count {
        var j = i
        let x = arr[i]
        while j > 0 && arr[j - 1] > x {
            arr[j] = arr[j - 1]
            j -= 1
        }
        arr[j] = x
    }
}

let a = ints(cnt: 10, ceiling: 100)
print(a)

var b = a
insertion(arr: &b)
print(b)

输出:

[13, 30, 68, 19, 1, 4, 28, 65, 96, 13]
[1, 4, 13, 13, 19, 28, 30, 65, 68, 96]

为了比较,这是java中的实现:

void insertionSort(int[] arr) {
    for(int i = 1; i < arr.length; i++) {
        int x = arr[i];
        int j = i;
        while(j > 0 && arr[j - 1] > x) {
            arr[j] = arr[j - 1];
            j--;
        }
        arr[j] = x;
    }
}

答案 2 :(得分:0)

快捷键5 常规插入排序

func insertionSort<U: Comparable>(with array: inout [U]) -> [U] {
    for i in 1..<array.count {
        let temp = array[i]
        var insertionIndex = i
        for j in (0...i).reversed() where j>0 && temp < array[j-1] {
            array[j] = array[j-1]
            insertionIndex = j-1
        }
        array[insertionIndex] = temp
    }
    return array
}

输入:-

var intArray = [8, 3, 5, 10, 4, -1, 17, 3, 18, 10]
var floatArray = [12.231, 12.23, 14.5, 3.4, 67.899, 0.0, -1.234]
var doubleArray = [123.43555, 123.1223332, -121.2212, 23.343434, 1.232434]
var stringArray = ["Ratheesh", "Srini", "Thangu", "Muthu", "Gopi"]

print(insertionSort(with: &intArray))
print(insertionSort(with: &floatArray))
print(insertionSort(with: &doubleArray))
print(insertionSort(with: &stringArray))

输出:-

[-1, 3, 3, 4, 5, 8, 10, 10, 17, 18]
[-1.234, 0.0, 3.4, 12.23, 12.231, 14.5, 67.899]
[-121.2212, 1.232434, 23.343434, 123.1223332, 123.43555]
["Gopi", "Muthu", "Ratheesh", "Srini", "Thangu"]