File tree 2 files changed +12
-15
lines changed
SlowSort.playground/Sources
2 files changed +12
-15
lines changed Original file line number Diff line number Diff line change @@ -17,19 +17,17 @@ We can decompose the problem of sorting n numbers in ascending order into
17
17
Here is an implementation of slow sort in Swift:
18
18
19
19
``` swift
20
- public func slowsort (_ i : Int , _ j : Int ) {
21
- if i>= j {
22
- return
23
- }
20
+ func slowSort (_ i : Int , _ j : Int , _ numberList : inout [Int ]) {
21
+ guard if i < j else { return }
24
22
let m = (i+ j)/ 2
25
- slowsort (i,m )
26
- slowsort (m+ 1 ,j )
23
+ slowSort (i, m, & numberList )
24
+ slowSort (m+ 1 , j, & numberList )
27
25
if numberList[j] < numberList[m] {
28
26
let temp = numberList[j]
29
27
numberList[j] = numberList[m]
30
28
numberList[m] = temp
31
29
}
32
- slowsort (i,j- 1 )
30
+ slowSort (i, j- 1 , & numberList )
33
31
}
34
32
```
35
33
@@ -47,4 +45,4 @@ public func slowsort(_ i: Int, _ j: Int) {
47
45
48
46
* Written for Swift Algorithm Club by Lukas Schramm*
49
47
50
- (used the Insertion Sort Readme as template)
48
+ (used the Insertion Sort Readme as template)
Original file line number Diff line number Diff line change 1
1
import Foundation
2
2
3
- public func slowsort( _ i: Int , _ j: Int , _ numberList: inout [ Int ] ) {
4
- if i>= j {
5
- return
6
- }
3
+ public func slowSort( _ i: Int , _ j: Int , _ numberList: inout [ Int ] ) {
4
+ guard i < j else { return }
5
+
7
6
let m = ( i+ j) / 2
8
- slowsort ( i, m, & numberList)
9
- slowsort ( m+ 1 , j, & numberList)
7
+ slowSort ( i, m, & numberList)
8
+ slowSort ( m+ 1 , j, & numberList)
10
9
if numberList [ j] < numberList [ m] {
11
10
let temp = numberList [ j]
12
11
numberList [ j] = numberList [ m]
13
12
numberList [ m] = temp
14
13
}
15
- slowsort ( i, j- 1 , & numberList)
14
+ slowSort ( i, j- 1 , & numberList)
16
15
}
You can’t perform that action at this time.
0 commit comments