Skip to content

Commit 5524ef4

Browse files
committed
Added SlowSort
1 parent 912c21d commit 5524ef4

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

Slow Sort/README.markdown

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Slow Sort
2+
3+
Goal: Sort an array of numbers from low to high (or high to low).
4+
5+
You are given an array of numbers and need to put them in the right order. The insertion sort algorithm works as follows:
6+
7+
We can decompose the problem of sorting n numbers in ascending order into
8+
9+
1. find the maximum of the numbers
10+
1. find the maximum of the first n/2 elements
11+
2. find the maximum of the remaining n/2 elements
12+
3. find the largest of those two maxima
13+
2. sorting the remaining ones
14+
15+
## The code
16+
17+
Here is an implementation of slow sort in Swift:
18+
19+
```swift
20+
public func slowsort(_ i: Int, _ j: Int) {
21+
if i>=j {
22+
return
23+
}
24+
let m = (i+j)/2
25+
slowsort(i,m)
26+
slowsort(m+1,j)
27+
if numberList[j] < numberList[m] {
28+
let temp = numberList[j]
29+
numberList[j] = numberList[m]
30+
numberList[m] = temp
31+
}
32+
slowsort(i,j-1)
33+
}
34+
```
35+
36+
## Performance
37+
38+
| Case | Performance |
39+
|:-------------: |:---------------:|
40+
| Worst | slow |
41+
| Best | O(n^(log(n)/(2+e)))) |
42+
| Average | O(n^(log(n)/2)) |
43+
44+
## See also
45+
46+
[Slow Sort explanation in the Internet](http://c2.com/cgi/wiki?SlowSort)
47+
48+
*Written for Swift Algorithm Club by Lukas Schramm*
49+
50+
(used the Insertion Sort Readme as template)

Slow Sort/SlowSort.swift

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// SlowSort.swift
3+
//
4+
//
5+
// Created by Pope Lukas Schramm (Dabendorf Orthodox Religion) on 16-07-16.
6+
//
7+
//
8+
9+
var numberList = [1,12,9,17,13,12]
10+
11+
public func slowsort(_ i: Int, _ j: Int) {
12+
if i>=j {
13+
return
14+
}
15+
let m = (i+j)/2
16+
slowsort(i,m)
17+
slowsort(m+1,j)
18+
if numberList[j] < numberList[m] {
19+
let temp = numberList[j]
20+
numberList[j] = numberList[m]
21+
numberList[m] = temp
22+
}
23+
slowsort(i,j-1)
24+
}
25+
26+
27+
slowsort(0,numberList.count-1)
28+
print(numberList)

swift-algorithm-club.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)