Created
June 5, 2014 17:48
-
-
Save fjcaetano/dc3ea46e007d43614594 to your computer and use it in GitHub Desktop.
💩Sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var randomNumbers = [42, 12, 88, 62, 63, 56, 1, 77, 88, 97, 97, 20, 45, 91, 62, 2, 15, 31, 59, 5] | |
randomNumbers | |
func 💩Sort(arr: Int[]) -> Int[] { | |
var array = arr.copy() | |
// Private function to check whether or not the array is sorted | |
func isSorted (arr: Int[]) -> Bool { | |
for i in 0..(arr.count - 1) { | |
if arr[i] > arr[i+1] { | |
return false | |
} | |
} | |
return true | |
} | |
while !isSorted(array) { | |
// Shuffles the array randomly | |
for i in 0..array.count { | |
let j = Int(arc4random()) % array.count | |
(array[i], array[j]) = (array[j], array[i]) | |
} | |
} | |
return array | |
} | |
💩Sort(randomNumbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment