Last active
August 29, 2015 14:02
-
-
Save cezarywojcik/37b957ba70bbec46839e to your computer and use it in GitHub Desktop.
Swift Quick Sort Test
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 | |
let NUM_INTS = 10000 | |
func swiftQuickSort<T : Comparable>(arr : T[], left: Int, right: Int) { | |
if right > left { | |
var i = left | |
for j in (left + 1)..(right + 1) { | |
if arr[j] < arr[left] { | |
i++ | |
(arr[i], arr[j]) = (arr[j], arr[i]) | |
} | |
} | |
(arr[i], arr[left]) = (arr[left], arr[i]) | |
swiftQuickSort(arr, left, i - 1) | |
swiftQuickSort(arr, i + 1, right) | |
} | |
} | |
// generate random array | |
var arr : Int[] = [] | |
for _ in 0..NUM_INTS { | |
arr += Int(arc4random()) | |
} | |
// test sort | |
println("Starting Quick Sort...") | |
let start = NSDate.date() | |
swiftQuickSort(arr, 0, arr.count - 1) | |
let timeInterval = -1 * start.timeIntervalSinceNow | |
println("Time: \(timeInterval)s") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment