Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds Stalin Sort #951

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Stalin Sort/MyPlayground.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

var array = [4,2,1,3]

print("before:",array)
print("after:", stalinSort(array))
print("after:", stalinSort(array, <))
print("after:", stalinSort(array, >))
53 changes: 53 additions & 0 deletions Stalin Sort/MyPlayground.playground/Sources/StalinSort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// StalinSort.swift
//
// Created by Julio Brazil on 1/10/18.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
//

import Foundation

/// 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.
/// - Parameters:
/// - elements: The array containing the elements that are to be "sorted" by the algorithm.
/// - Returns: The new array containing the same amount or fewer elements than provided, but guaranteed to be in order.
public func stalinSort<T: Comparable>(_ originalArray: [T]) -> [T] {
return stalinSort(originalArray, >)
}

/// 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.
/// - Parameters:
/// - elements: The array containing the elements that are to be "sorted" by the algorithm.
/// - isInOrder: A function that takes 2 comparable inputs and returns if the elements provided are considered "in order".
/// - Returns: The new array containing the same amount or fewer elements than provided, but guaranteed to be in order.
public func stalinSort<T: Comparable>(_ elements: [T], _ isInOrder: (T, T) -> Bool) -> [T] {
var index = 1
var array = elements

while index < array.count {
let current = array[index]
let previous = array[index-1]

if isInOrder(previous, current) {
index += 1
} else {
array.remove(at: index)
}
}

return array
}
4 changes: 4 additions & 0 deletions Stalin Sort/MyPlayground.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
58 changes: 58 additions & 0 deletions Stalin Sort/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Stalin Sort

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.

##### Runtime:
- Average: O(n)
- Worst: O(n)

##### Memory:
- O(1)

### Implementation:

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.

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.

#### Example
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.
Let's take the array `[5, 1, 8, 2, 4]`, and sort the array from lowest number to greatest number using Stalin sort.

##### First Pass
[ 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.

##### Scont Pass
[ 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.

##### Third Pass
[ 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.

#### Code
```swift
var index = 1

while index < array.count {
let current = array[index]
let previous = array[index-1]

if previous > current {
index += 1
} else {
array.remove(at: index)
}
}
```

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...

#### Conclusion

# DO NOT USE THIS ALGORITHM TO ORDER AN ARRAY

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.

*Created for the Swift Algorithm Club by [Julio Brazil](https://github.com/JulioBBL)*

##### Supporting Links
[Original post by Mathew @mastodon.social](https://mastodon.social/@mathew/100958177234287431)