Sorting algorithms/Bogosort: Difference between revisions
Content added Content deleted
(Add Swift) |
|||
Line 1,840: | Line 1,840: | ||
117: 3 2 1 5 4 |
117: 3 2 1 5 4 |
||
118: 1 2 3 4 5</pre> |
118: 1 2 3 4 5</pre> |
||
=={{header|Swift}}== |
|||
<lang swift>func shuffle<T>(array: T[]) { |
|||
for i in 1..array.count { |
|||
let j = Int(arc4random_uniform(UInt32(i))) |
|||
(array[i], array[j]) = (array[j], array[i]) |
|||
} |
|||
} |
|||
func issorted<T:Comparable>(ary: T[]) -> Bool { |
|||
for i in 1..(ary.count-1) { |
|||
if ary[i] > ary[i+1] { |
|||
return false |
|||
} |
|||
} |
|||
return true |
|||
} |
|||
func bogosort<T:Comparable>(ary: T[]) { |
|||
while !issorted(ary) { |
|||
shuffle(ary) |
|||
} |
|||
}</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |