Skip to content

Commit 271edbf

Browse files
committed
adds Stalin Sort
1 parent 2fdd8b8 commit 271edbf

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Foundation
2+
3+
var array = [4,2,1,3]
4+
5+
print("before:",array)
6+
print("after:", stalinSort(array))
7+
print("after:", stalinSort(array, <))
8+
print("after:", stalinSort(array, >))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// StalinSort.swift
3+
//
4+
// Created by Julio Brazil on 1/10/18.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7+
// associated documentation files (the "Software"), to deal in the Software without restriction, including
8+
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
10+
// following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all copies or substantial
13+
// portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16+
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
17+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18+
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19+
// OR OTHER DEALINGS IN THE SOFTWARE.
20+
//
21+
22+
import Foundation
23+
24+
/// This is a joke sorting algorithm proposed on social media to poke fun at dictatorial regimes, it excludes elements considered not in order based on the first element.
25+
/// - Parameters:
26+
/// - elements: The array containing the elements that are to be "sorted" by the algorithm.
27+
/// - Returns: The new array containing the same amount or fewer elements than provided, but guaranteed to be in order.
28+
public func stalinSort<T: Comparable>(_ originalArray: [T]) -> [T] {
29+
return stalinSort(originalArray, >)
30+
}
31+
32+
/// This is a joke sorting algorithm proposed on social media to poke fun at dictatorial regimes, it excludes elements considered not in order based on the first element.
33+
/// - Parameters:
34+
/// - elements: The array containing the elements that are to be "sorted" by the algorithm.
35+
/// - isInOrder: A function that takes 2 comparable inputs and returns if the elements provided are considered "in order".
36+
/// - Returns: The new array containing the same amount or fewer elements than provided, but guaranteed to be in order.
37+
public func stalinSort<T: Comparable>(_ elements: [T], _ isInOrder: (T, T) -> Bool) -> [T] {
38+
var index = 1
39+
var array = elements
40+
41+
while index < array.count {
42+
let current = array[index]
43+
let previous = array[index-1]
44+
45+
if isInOrder(previous, current) {
46+
index += 1
47+
} else {
48+
array.remove(at: index)
49+
}
50+
}
51+
52+
return array
53+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Stalin Sort/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Stalin Sort/README.markdown

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Stalin Sort
2+
3+
Stalin sort is a joke algorithm [proposed on social media](https://mastodon.social/@mathew/100958177234287431) poking fun at dictatorial regimes. Even though it has no use as a sorting algorithm, it is a valid logic exercise.
4+
5+
##### Runtime:
6+
- Average: O(n)
7+
- Worst: O(n)
8+
9+
##### Memory:
10+
- O(1)
11+
12+
### Implementation:
13+
14+
The implementation will not be shown as, you know, it's not usefull for sorting things. However, having a grasp of the concept will help you understand the basics of simple sorting algorithms.
15+
16+
Stalin sort is a very simple sorting algorithm, it consists in comparing elements in the array with the previous one, if the element you are comparing is not considered "in order" it is removed from the array and the next one is comparade, until you either run out of elements or reach the end of the array. The easiest way of implementing this would be with an While Loop, since we are do not know how many elements will be taken out and what size will the array be at the end.
17+
18+
#### Example
19+
We begin analyzing the array by the secong element, since the first one will always be in order (the first element is the reference for the rest of the array). Comparing a given element with the previous one, if they are considered ordered, the element is kept in the array and a counter is increased, indicating the index of the next element to be analysed.
20+
Let's take the array `[5, 1, 8, 2, 4]`, and sort the array from lowest number to greatest number using Stalin sort.
21+
22+
##### First Pass
23+
[ 5 **1** 8 2 4 ] -> [ 5 8 2 4 ], Here, the algorithm compares the second element (of value **1**) with the previous (of value 5), and romeves it, since 5 > 1.
24+
25+
##### Scont Pass
26+
[ 5 **8** 2 4 ] -> [ 5 **8** 2 4 ], This time the situation is different, since 8 > 5, the element is considered to be in order, so it is left alone.
27+
28+
##### Third Pass
29+
[ 5 8 **2** 4 ] -> [ 5 8 4 ], i believe you are getting the hang of it, 8 > 2, and since we are ordering from lowest to gratest number, 2 is considered "out of order" and is forcibly removed from the group.
30+
31+
#### Code
32+
```swift
33+
var index = 1
34+
35+
while index < array.count {
36+
let current = array[index]
37+
let previous = array[index-1]
38+
39+
if previous > current {
40+
index += 1
41+
} else {
42+
array.remove(at: index)
43+
}
44+
}
45+
```
46+
47+
The code presented in this repository is different, to allow grater flexibility and reusability, not that you should use it, specially if your intent is to order an array, as a matter of fact, let me be even clearer...
48+
49+
#### Conclusion
50+
51+
# DO NOT USE THIS ALGORITHM TO ORDER AN ARRAY
52+
53+
This sorting algorithm is a joke and should be trated as one, it is only described here because it has **some** value as an teaching resource.
54+
55+
*Created for the Swift Algorithm Club by [Julio Brazil](https://github.com/JulioBBL)*
56+
57+
##### Supporting Links
58+
[Original post by Mathew @mastodon.social](https://mastodon.social/@mathew/100958177234287431)

0 commit comments

Comments
 (0)