Skip to content

Instantly share code, notes, and snippets.

@mhlangagc mhlangagc/quicksort.swift

Last active Apr 18, 2016
What would you like to do?
Implementation of a quick Sort algorithm in swift
import UIKit
//Random Interger Generator :: You could alternatively create your own array of Intergers. I prefer a a random way to my array length, minumum and maximum elements.
func randomIntergerInRange(low:Int, high:Int) -> Int {
let randomGeneratedNumber = low + Int(arc4random()) % (high - low);
return Int(randomGeneratedNumber)
Length : Number of elements in the Array
Min : Lowest Value in the Array
Max : Highest Interger in the array
func randomNumberArray(length: Int, min: Int, max: Int) -> [Int] {
// Create an Array of random Intergers from different Elements
var randomArray = [Int]()
for _ in 0..<length {
randomArray.append(randomIntergerInRange(min, high: max))
return randomArray
//Sorting Algorithm
func quickSortAlgorithm(createRandomArray: [Int])->Array<Int> {
var leftSide = [Int]() //Lower than the Pivot
var rightSide = [Int]() //Greater than the Pivot
var equal = [Int]() //Equal to the Pivot
//Sort the Random Arrat
if createRandomArray.count > 1{
let pivot = createRandomArray[0]
for x in createRandomArray {
if x < pivot{
if x == pivot {
if x > pivot {
return (quickSortAlgorithm(leftSide)+equal+quickSortAlgorithm(rightSide)) //Use Recursion to repeat the process for left & Right Side
else {
return createRandomArray
//Run the algorithm on the random Number Array with your own array length, Min number and Max Number
quickSortAlgorithm(randomNumberArray(10, min: 0, max: 100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.