diff --git a/Stalin Sort/MyPlayground.playground/Contents.swift b/Stalin Sort/MyPlayground.playground/Contents.swift new file mode 100644 index 000000000..45d1e8460 --- /dev/null +++ b/Stalin Sort/MyPlayground.playground/Contents.swift @@ -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, >)) diff --git a/Stalin Sort/MyPlayground.playground/Sources/StalinSort.swift b/Stalin Sort/MyPlayground.playground/Sources/StalinSort.swift new file mode 100644 index 000000000..8b6e99963 --- /dev/null +++ b/Stalin Sort/MyPlayground.playground/Sources/StalinSort.swift @@ -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(_ 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(_ 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 +} diff --git a/Stalin Sort/MyPlayground.playground/contents.xcplayground b/Stalin Sort/MyPlayground.playground/contents.xcplayground new file mode 100644 index 000000000..5da2641c9 --- /dev/null +++ b/Stalin Sort/MyPlayground.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Stalin Sort/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata b/Stalin Sort/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 000000000..919434a62 --- /dev/null +++ b/Stalin Sort/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Stalin Sort/MyPlayground.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Stalin Sort/MyPlayground.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 000000000..18d981003 --- /dev/null +++ b/Stalin Sort/MyPlayground.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Stalin Sort/README.markdown b/Stalin Sort/README.markdown new file mode 100644 index 000000000..7a603c360 --- /dev/null +++ b/Stalin Sort/README.markdown @@ -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)