Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,37 @@ This is a rather inefficient but neat solution. Pick a pivot and remove it from
*/

func swiftyQuickSort<T>(var array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
guard array.count > 1 else {
return array
}

guard array.count > 1 else {
return array
}

let pivot = array.removeLast()
let lowers = array.filter {
isOrderedBefore($0, pivot)
}
let greaters = array.filter {
isOrderedBefore(pivot, $0)
}
let pivotsCount = array.count - lowers.count - greaters.count + 1
let pivots = [T](count: pivotsCount, repeatedValue: pivot)

return swiftyQuickSort(lowers, isOrderedBefore) + pivots + swiftyQuickSort(greaters, isOrderedBefore)
}

//let array1 = [1]
//assert(swiftyQuickSort(array1, <) == [1])
//
//let array2 = [Int]()
//assert(swiftyQuickSort(array2, <) == [])
//
//let array3 = ["c", "d", "b", "a"]
//assert(swiftyQuickSort(array3, <) == ["a", "b", "c", "d"])
//
//let array4 = [3, 0, 2, 1, 2, -1]
//assert(swiftyQuickSort(array4, <) == [-1, 0, 1, 2, 2, 3])
//
//let array6 = [5, 1, 2, 3]
//assert(swiftyQuickSort(array6, >) == [5, 3, 2, 1])
let array1 = [1]
assert(swiftyQuickSort(array1, <) == [1])

let array2 = [Int]()
assert(swiftyQuickSort(array2, <) == [])

let array3 = ["c", "d", "b", "a"]
assert(swiftyQuickSort(array3, <) == ["a", "b", "c", "d"])

let array4 = [3, 0, 2, 1, 2, -1]
assert(swiftyQuickSort(array4, <) == [-1, 0, 1, 2, 2, 3])

let array6 = [5, 1, 2, 3]
assert(swiftyQuickSort(array6, >) == [5, 3, 2, 1])
/*:
[Table of Contents](Table%20of%20Contents) | [Previous](@previous) | [Next](@next)
*/
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,37 @@ Implement [Lomuto's partition algorithm]( https://en.wikipedia.org/wiki/Quicksor
*/

func lomutoPartition<T>(inout array: [T], low: Int, high: Int, @noescape _ isOrderedBefore: (T, T) -> Bool) -> Int {
let pivot = array[high]

// ...

return 0
let pivot = array[high]
var i = low
for j in low..<high where isOrderedBefore(array[j], pivot) {
if i != j {
swap(&array[i], &array[j])
}
i += 1
}
if i != high {
swap(&array[i], &array[high])
}

return i
}

//var lomutoArray1 = [2, 1, 4]
//assert(lomutoPartition(&lomutoArray1, low: 0, high: lomutoArray1.count-1, <) == 2)
//assert(lomutoArray1 == [2, 1, 4])
//
//var lomutoArray2 = [2, 1, 0]
//assert(lomutoPartition(&lomutoArray2, low: 0, high: lomutoArray2.count-1, <) == 0)
//assert(lomutoArray2 == [0, 1, 2])
//
//var lomutoArray3 = [5, 6, 0, 2, 1, 7, 3]
//assert(lomutoPartition(&lomutoArray3, low: 0, high: lomutoArray3.count-1, <) == 3)
//assert(lomutoArray3 == [0, 2, 1, 3, 5, 7, 6])
//
//var lomutoArray4 = [1, 1, 1, 0]
//assert(lomutoPartition(&lomutoArray4, low: 0, high: lomutoArray4.count-1, <) == 0)
//assert(lomutoArray4 == [0, 1, 1, 1])

var lomutoArray1 = [2, 1, 4]
assert(lomutoPartition(&lomutoArray1, low: 0, high: lomutoArray1.count-1, <) == 2)
assert(lomutoArray1 == [2, 1, 4])

var lomutoArray2 = [2, 1, 0]
assert(lomutoPartition(&lomutoArray2, low: 0, high: lomutoArray2.count-1, <) == 0)
assert(lomutoArray2 == [0, 1, 2])

var lomutoArray3 = [5, 6, 0, 2, 1, 7, 3]
assert(lomutoPartition(&lomutoArray3, low: 0, high: lomutoArray3.count-1, <) == 3)
assert(lomutoArray3 == [0, 2, 1, 3, 5, 7, 6])

var lomutoArray4 = [1, 1, 1, 0]
assert(lomutoPartition(&lomutoArray4, low: 0, high: lomutoArray4.count-1, <) == 0)
assert(lomutoArray4 == [0, 1, 1, 1])



Expand All @@ -42,24 +51,42 @@ Implement [Hoare's partition algorithm]( https://en.wikipedia.org/wiki/Quicksort
*/

func hoarePartition<T>(inout array: [T], low: Int, high: Int, @noescape _ isOrderedBefore: (T, T) -> Bool) -> Int {
let pivot = array[low]

// ...

return 0
if low == high {
return low
}

let pivot = array[low]
var i = low - 1
var j = high + 1

while true {
repeat {
j -= 1
} while isOrderedBefore(pivot, array[j])

repeat {
i += 1
} while isOrderedBefore(array[i], pivot)

if i < j {
swap(&array[i], &array[j])
} else {
return j
}
}
}

//var hoareArray1 = [2, 5, 4]
//assert(hoarePartition(&hoareArray1, low: 0, high: hoareArray1.count-1, <) == 0)
//assert(hoareArray1 == [2, 5, 4])
//
//var hoareArray2 = [5, 6, 0, 2, 1, 7, 3]
//assert(hoarePartition(&hoareArray2, low: 0, high: hoareArray2.count-1, <) == 4)
//assert(hoareArray2 == [1, 3, 0, 2, 5, 7, 6])
//
//var hoareArray3 = [1, 1, 1, 0]
//assert(hoarePartition(&hoareArray3, low: 0, high: hoareArray3.count-1, <) == 1)
//assert(hoareArray3 == [0, 1, 1, 1])
var hoareArray1 = [2, 5, 4]
assert(hoarePartition(&hoareArray1, low: 0, high: hoareArray1.count-1, <) == 0)
assert(hoareArray1 == [2, 5, 4])

var hoareArray2 = [5, 6, 0, 2, 1, 7, 3]
assert(hoarePartition(&hoareArray2, low: 0, high: hoareArray2.count-1, <) == 3)
assert(hoareArray2 == [3, 1, 0, 2, 6, 7, 5])

var hoareArray3 = [1, 1, 1, 0]
assert(hoarePartition(&hoareArray3, low: 0, high: hoareArray3.count-1, <) == 1)
assert(hoareArray3 == [0, 1, 1, 1])


/*:
Expand All @@ -69,33 +96,36 @@ By using your `hoarePartition`, implement an in-place quicksort.
*/

func quickSort<T>(inout array: [T], low: Int, high: Int, @noescape _ isOrderedBefore: (T, T) -> Bool) {

// ...
guard low < high else { return }

let p = hoarePartition(&array, low: low, high: high, isOrderedBefore)
quickSort(&array, low: low, high: p, isOrderedBefore)
quickSort(&array, low: p+1, high: high, isOrderedBefore)
}

//var array1 = [1]
//quickSort(&array1, low: 0, high: array1.count - 1, <)
//assert(array1 == [1])
//
//var array2 = [Int]()
//quickSort(&array2, low: 0, high: array2.count - 1, <)
//assert(array2 == [])
//
//var array3 = [1, 2, 3]
//quickSort(&array3, low: 0, high: array3.count - 1, <)
//assert(array3 == [1, 2, 3])
//
//var array4 = [3, 0, 2, 1, 2, -1]
//quickSort(&array4, low: 0, high: array4.count - 1, <)
//assert(array4 == [-1, 0, 1, 2, 2, 3])
//
//var array5 = [1, 2, 3]
//quickSort(&array5, low: 0, high: array5.count - 1, >)
//assert(array5 == [3, 2, 1])
//
//var array6 = [5, 1, 2]
//quickSort(&array6, low: 0, high: array6.count - 1, <)
//assert(array6 == [1, 2, 5])
var array1 = [1]
quickSort(&array1, low: 0, high: array1.count - 1, <)
assert(array1 == [1])

var array2 = [Int]()
quickSort(&array2, low: 0, high: array2.count - 1, <)
assert(array2 == [])

var array3 = [1, 2, 3]
quickSort(&array3, low: 0, high: array3.count - 1, <)
assert(array3 == [1, 2, 3])

var array4 = [3, 0, 2, 1, 2, -1]
quickSort(&array4, low: 0, high: array4.count - 1, <)
assert(array4 == [-1, 0, 1, 2, 2, 3])

var array5 = [1, 2, 3]
quickSort(&array5, low: 0, high: array5.count - 1, >)
assert(array5 == [3, 2, 1])

var array6 = [5, 1, 2]
quickSort(&array6, low: 0, high: array6.count - 1, <)
assert(array6 == [1, 2, 5])


/*:
Expand All @@ -113,30 +143,45 @@ When the algorithm is running, there are 4 groups of elements. We need 3 indices
*/

func dutchFlagPartition<T: Comparable>(inout array: [T], pivotIndex: Int, low: Int, high: Int) -> (Int, Int) {
let pivot = array[pivotIndex]

var smaller = 0
var equal = 0
var larger = high

// ...

return (smaller, larger)
let pivot = array[pivotIndex]

var smaller = 0
var equal = 0
var larger = high

while equal <= larger {
if array[equal] < pivot {
if smaller != equal {
swap(&array[smaller], &array[equal])
}
smaller += 1
equal += 1
} else if array[equal] > pivot {
if equal != larger {
swap(&array[equal], &array[larger])
}
larger -= 1
} else {
equal += 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you notice the function running indefinitely here? I also implemented the wikipedia's pseudocode and smaller is always ++ while the function is not breaking/return the tuple.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't. It completes the calls and pass the tests. Did you try my version?
Let me know @diegopetrucci

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I was incrementing smaller, not equal. My bad. Thanks for the help Matteo!

}
}

return (smaller, larger)
}

//var dutcharray = [5, 1, 2, 2, 4, 5]
//let lowAndHigh = dutchFlagPartition(&dutcharray, pivotIndex: 2, low: 0, high: dutcharray.count - 1)
//assert(lowAndHigh.0 == 1)
//assert(lowAndHigh.1 == 2)
//assert(dutcharray == [1 ,2 ,2, 4, 5, 5])
//
//
//var dutcharray2 = [5, 10, 5, 5, 2, 5]
//let lowAndHigh2 = dutchFlagPartition(&dutcharray2, pivotIndex: 0, low: 0, high: dutcharray2.count - 1)
//assert(lowAndHigh2.0 == 1)
//assert(lowAndHigh2.1 == 4)
//assert(dutcharray2 == [2, 5, 5, 5, 5, 10])
//
var dutcharray = [5, 1, 2, 2, 4, 5]
let lowAndHigh = dutchFlagPartition(&dutcharray, pivotIndex: 2, low: 0, high: dutcharray.count - 1)
assert(lowAndHigh.0 == 1)
assert(lowAndHigh.1 == 2)
assert(dutcharray == [1 ,2 ,2, 4, 5, 5])


var dutcharray2 = [5, 10, 5, 5, 2, 5]
let lowAndHigh2 = dutchFlagPartition(&dutcharray2, pivotIndex: 0, low: 0, high: dutcharray2.count - 1)
assert(lowAndHigh2.0 == 1)
assert(lowAndHigh2.1 == 4)
assert(dutcharray2 == [2, 5, 5, 5, 5, 10])


/*:
### 1.5. Reading Swift Source Code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1602&amp;EndingColumnNumber=10&amp;EndingLineNumber=34&amp;StartingColumnNumber=9&amp;StartingLineNumber=34&amp;Timestamp=472497062.906165"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2065&amp;EndingColumnNumber=10&amp;EndingLineNumber=61&amp;StartingColumnNumber=9&amp;StartingLineNumber=61&amp;Timestamp=473006475.246227"
selectedRepresentationIndex = "2"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1602&amp;EndingColumnNumber=10&amp;EndingLineNumber=34&amp;StartingColumnNumber=9&amp;StartingLineNumber=34&amp;Timestamp=472497062.906439"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2065&amp;EndingColumnNumber=10&amp;EndingLineNumber=61&amp;StartingColumnNumber=9&amp;StartingLineNumber=61&amp;Timestamp=473006475.246504"
selectedRepresentationIndex = "2"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1602&amp;EndingColumnNumber=14&amp;EndingLineNumber=34&amp;StartingColumnNumber=13&amp;StartingLineNumber=34&amp;Timestamp=472497062.90666"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2065&amp;EndingColumnNumber=14&amp;EndingLineNumber=61&amp;StartingColumnNumber=13&amp;StartingLineNumber=61&amp;Timestamp=473006475.246735"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1617&amp;EndingColumnNumber=14&amp;EndingLineNumber=68&amp;StartingColumnNumber=9&amp;StartingLineNumber=68&amp;Timestamp=472497062.906874"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2253&amp;EndingColumnNumber=14&amp;EndingLineNumber=98&amp;StartingColumnNumber=9&amp;StartingLineNumber=98&amp;Timestamp=473006475.246963"
lockedSize = "{179, 289}"
selectedRepresentationIndex = "1"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2287&amp;EndingColumnNumber=14&amp;EndingLineNumber=69&amp;StartingColumnNumber=9&amp;StartingLineNumber=69&amp;Timestamp=472497131.944604"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2886&amp;EndingColumnNumber=14&amp;EndingLineNumber=99&amp;StartingColumnNumber=9&amp;StartingLineNumber=99&amp;Timestamp=473006475.247198"
lockedSize = "{178, 287}"
selectedRepresentationIndex = "1"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1014&amp;EndingColumnNumber=10&amp;EndingLineNumber=37&amp;StartingColumnNumber=9&amp;StartingLineNumber=37&amp;Timestamp=472497062.907309"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1463&amp;EndingColumnNumber=10&amp;EndingLineNumber=64&amp;StartingColumnNumber=9&amp;StartingLineNumber=64&amp;Timestamp=473006241.853156"
selectedRepresentationIndex = "2"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1458&amp;EndingColumnNumber=10&amp;EndingLineNumber=38&amp;StartingColumnNumber=9&amp;StartingLineNumber=38&amp;Timestamp=472497062.907522"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1894&amp;EndingColumnNumber=10&amp;EndingLineNumber=65&amp;StartingColumnNumber=9&amp;StartingLineNumber=65&amp;Timestamp=473006475.247656"
selectedRepresentationIndex = "2"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1014&amp;EndingColumnNumber=10&amp;EndingLineNumber=34&amp;StartingColumnNumber=9&amp;StartingLineNumber=34&amp;Timestamp=472497062.907733"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1463&amp;EndingColumnNumber=10&amp;EndingLineNumber=61&amp;StartingColumnNumber=9&amp;StartingLineNumber=61&amp;Timestamp=473006241.853604"
selectedRepresentationIndex = "2"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
****
[Start](@next)
*/