Files
hello-algo/en/codes/swift/chapter_computational_complexity/complexity_exercises.swift
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

63 lines
1.1 KiB
Swift

/**
File: complexity_exercises.swift
Created Time: 2026-08-18
Author: Hello Algo Team
*/
/* Sum using iteration */
func sumIter(n: Int) -> Int {
var res = 0
for i in 1 ... n {
res += i
}
return res
}
/* Sum using recursion */
func sumRecur(n: Int) -> Int {
if n == 1 {
return 1
}
return n + sumRecur(n: n - 1)
}
/* Linear-time loop */
func linearLoop(n: Int) -> Int {
var res = 0
for i in 0 ..< n {
res += i
}
return res
}
/* Quadratic-time loop */
func quadraticLoop(n: Int) -> Int {
var res = 0
for i in 0 ..< n {
for j in i ..< n {
res += j
}
}
return res
}
/* Logarithmic-time loop */
func logarithmicLoop(n: Int) -> Int {
var n = n
while n > 1 {
n /= 2
}
return n
}
@main
enum ComplexityExercises {
static func main() {
assert(sumIter(n: 1) == 1 && sumRecur(n: 1) == 1)
assert(sumIter(n: 4) == 10 && sumRecur(n: 4) == 10)
assert(linearLoop(n: 4) == 6)
assert(quadraticLoop(n: 4) == 20)
assert(logarithmicLoop(n: 4) == 1 && logarithmicLoop(n: 5) == 1)
}
}