diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index 654fc56a1..3278de306 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -36,7 +36,7 @@ In code, it looks something like this: {% sample lang="rs" %} [import:16-20, lang:"rust"](code/rust/bogosort.rs) {% sample lang="swift" %} -[import:32-39, lang:"swift"](code/swift/bogosort.swift) +[import:25-31, lang:"swift"](code/swift/bogosort.swift) {% endmethod %} That's it. diff --git a/contents/bogo_sort/code/swift/bogosort.swift b/contents/bogo_sort/code/swift/bogosort.swift index 47df35d11..af11b0ccf 100644 --- a/contents/bogo_sort/code/swift/bogosort.swift +++ b/contents/bogo_sort/code/swift/bogosort.swift @@ -1,21 +1,16 @@ import Foundation - func isSorted(inputArray: [Int]) -> Bool { - for i in 0.. inputArray[i+1] { return false } } - + return true } - - func shuffle(inputArray: inout [Int]) -> [Int] { - var shuffledArray = [Int]() for _ in 0.. [Int] { shuffledArray.append(inputArray[rand]) inputArray.remove(at: rand) } - + return shuffledArray } - - func bogoSort(sortArray: inout [Int]) -> [Int] { - while(!isSorted(inputArray: sortArray)) { sortArray = shuffle(inputArray: &sortArray) } - + return sortArray } diff --git a/contents/bubble_sort/bubble_sort.md b/contents/bubble_sort/bubble_sort.md index 80f9b71fd..fe7dff4de 100644 --- a/contents/bubble_sort/bubble_sort.md +++ b/contents/bubble_sort/bubble_sort.md @@ -35,7 +35,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with {% sample lang="racket" %} [import:6-19, lang:"racket"](code/racket/bubbleSort.rkt) {% sample lang="swift" %} -[import:1-12, lang:"swift"](code/swift/bubblesort.swift) +[import:1-13, lang:"swift"](code/swift/bubblesort.swift) {% sample lang="ti83b" %} [import:2-13, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt) {% endmethod %} diff --git a/contents/bubble_sort/code/swift/bubblesort.swift b/contents/bubble_sort/code/swift/bubblesort.swift index 1c2c3818a..3552f2125 100644 --- a/contents/bubble_sort/code/swift/bubblesort.swift +++ b/contents/bubble_sort/code/swift/bubblesort.swift @@ -1,5 +1,4 @@ func bubbleSort(sortArray: inout [Int]) -> [Int] { - for i in (1.. sortArray[j+1] { diff --git a/contents/forward_euler_method/code/swift/euler.swift b/contents/forward_euler_method/code/swift/euler.swift index 2d6e51954..033b6d3fb 100644 --- a/contents/forward_euler_method/code/swift/euler.swift +++ b/contents/forward_euler_method/code/swift/euler.swift @@ -6,6 +6,7 @@ func solveEuler(timeStep: Double, n: Int) -> [Double] { for i in 1...n { result.append(result[i - 1] - 3 * result[i - 1] * timeStep) } + return result } @@ -19,6 +20,7 @@ func checkResult(result: [Double], threshold: Double, timeStep: Double) -> Bool isApprox = false } } + return isApprox } diff --git a/contents/monte_carlo_integration/code/swift/monte_carlo.swift b/contents/monte_carlo_integration/code/swift/monte_carlo.swift index 29c21f953..0559c1c3d 100644 --- a/contents/monte_carlo_integration/code/swift/monte_carlo.swift +++ b/contents/monte_carlo_integration/code/swift/monte_carlo.swift @@ -2,30 +2,21 @@ import Foundation - - public extension Double { - public static var random: Double { - return Double(arc4random()) / 0xFFFFFFFF // Returns a random double between 0.0 and 1.0, inclusive. } public static func random(min: Double, max: Double) -> Double { - return Double.random * (max - min) + min } } - func isInCircle(x: Double, y: Double, radius: Double) -> Bool { - return (x*x) + (y*y) < radius*radius } - func monteCarlo(n: Int) -> Double { - let radius: Double = 1 var piCount = 0 var randX: Double @@ -41,11 +32,9 @@ func monteCarlo(n: Int) -> Double { } let piEstimate = Double(4 * piCount)/(Double(n)) - return piEstimate } - func main() { let piEstimate = monteCarlo(n: 10000) print("Pi estimate is: ", piEstimate) diff --git a/contents/monte_carlo_integration/monte_carlo_integration.md b/contents/monte_carlo_integration/monte_carlo_integration.md index a703431e0..84a158e92 100644 --- a/contents/monte_carlo_integration/monte_carlo_integration.md +++ b/contents/monte_carlo_integration/monte_carlo_integration.md @@ -58,7 +58,7 @@ each point is tested to see whether it's in the circle or not: {% sample lang="java" %} [import:13-15, lang:"java"](code/java/MonteCarlo.java) {% sample lang="swift" %} -[import:21-25, lang:"swift"](code/swift/monte_carlo.swift) +[import:15-17 lang:"swift"](code/swift/monte_carlo.swift) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end,