Links

SieveSort Algorithm | Swift 3.0

SieveSort, based on the technique of divide-and-conquer, that takes time O(n2) in the worst-case. Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
/// sieve algo
func sieve (numbers: [Int]) -> [Int] {

    if numbers.isEmpty {
        return []
    }
    
    let p = numbers[0]
    
    return [p] + sieve(numbers: numbers[1.. 0
    })

}

sieve(numbers: [2,3,4,5,6,7,8,9])

Post a Comment