Created
August 20, 2015 12:26
-
-
Save chriseidhof/f18d9f4f2920db58b752 to your computer and use it in GitHub Desktop.
This file contains 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
import Foundation | |
extension Comparable { | |
static func compare(l: UnsafePointer<Void>, _ r: UnsafePointer<Void>) -> Int32 { | |
let left: Self = UnsafePointer(l).memory | |
let right: Self = UnsafePointer(r).memory | |
if left < right { return -1 } | |
if left == right { return 0 } | |
return 1 | |
} | |
} | |
typealias CompareFunc = (UnsafePointer<Void>, UnsafePointer<Void>) -> Int32 | |
func cmp(thunk: UnsafeMutablePointer<Void>, l: UnsafePointer<Void>, r: UnsafePointer<Void>) -> Int32 { | |
let compareFunc: CompareFunc = UnsafeMutablePointer(thunk).memory | |
return compareFunc(l,r) | |
} | |
extension Array where Element: Comparable { | |
func quickSort() -> [Element] { | |
var array = self | |
var compare = Element.compare | |
qsort_r(&array, array.count, strideof(Element), &compare, cmp) | |
return array | |
} | |
mutating func quickSortInline() { | |
self = quickSort() | |
} | |
} | |
//var myArray = [3,1,2,17,4] | |
var myArray = ["Hello", "ABC", "abc", "test"] | |
myArray.quickSortInline() | |
print(myArray) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment