Skip to content

Instantly share code, notes, and snippets.

@fjcaetano
Created June 5, 2014 17:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjcaetano/dc3ea46e007d43614594 to your computer and use it in GitHub Desktop.
Save fjcaetano/dc3ea46e007d43614594 to your computer and use it in GitHub Desktop.
💩Sort
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