@@ -7,7 +7,7 @@ Quicksort is one of the most famous algorithms in history. It was invented way b
7
7
Here's an implementation in Swift that should be easy to understand:
8
8
9
9
``` swift
10
- func quicksort <T : Comparable >(a : [T]) -> [T] {
10
+ func quicksort <T : Comparable >(_ a : [T]) -> [T] {
11
11
guard a.count > 1 else { return a }
12
12
13
13
let pivot = a[a.count / 2 ]
@@ -131,7 +131,7 @@ In the first example of quicksort I showed you, partitioning was done by calling
131
131
Here's an implementation of Lomuto's partitioning scheme in Swift:
132
132
133
133
``` swift
134
- func partitionLomuto <T : Comparable >(inout a : [T], low : Int , high : Int ) -> Int {
134
+ func partitionLomuto <T : Comparable >(_ a : inout [T], low : Int , high : Int ) -> Int {
135
135
let pivot = a[high]
136
136
137
137
var i = low
@@ -250,7 +250,7 @@ And we return `i`, the index of the pivot element.
250
250
Let's use this partitioning scheme to build quicksort. Here's the code:
251
251
252
252
``` swift
253
- func quicksortLomuto <T : Comparable >(inout a : [T], low : Int , high : Int ) {
253
+ func quicksortLomuto <T : Comparable >(_ a : inout [T], low : Int , high : Int ) {
254
254
if low < high {
255
255
let p = partitionLomuto (& a, low : low, high : high)
256
256
quicksortLomuto (& a, low : low, high : p - 1 )
@@ -277,7 +277,7 @@ This partitioning scheme is by Hoare, the inventor of quicksort.
277
277
Here is the code:
278
278
279
279
``` Swift
280
- func partitionHoare <T : Comparable >(inout a : [T], low : Int , high : Int ) -> Int {
280
+ func partitionHoare <T : Comparable >(_ a : inout [T], low : Int , high : Int ) -> Int {
281
281
let pivot = a[low]
282
282
var i = low - 1
283
283
var j = high + 1
@@ -318,7 +318,7 @@ The pivot is placed somewhere inside one of the two partitions, but the algorith
318
318
Because of these differences, the implementation of Hoare's quicksort is slightly different:
319
319
320
320
``` swift
321
- func quicksortHoare <T : Comparable >(inout a : [T], low : Int , high : Int ) {
321
+ func quicksortHoare <T : Comparable >(_ a : inout [T], low : Int , high : Int ) {
322
322
if low < high {
323
323
let p = partitionHoare (& a, low : low, high : high)
324
324
quicksortHoare (& a, low : low, high : p)
@@ -380,7 +380,7 @@ Another common solution is to choose the pivot randomly. Sometimes this may resu
380
380
Here is how you can do quicksort with a randomly chosen pivot:
381
381
382
382
``` swift
383
- func quicksortRandom <T : Comparable >(inout a : [T], low : Int , high : Int ) {
383
+ func quicksortRandom <T : Comparable >(_ a : inout [T], low : Int , high : Int ) {
384
384
if low < high {
385
385
let pivotIndex = random (min : low, max : high) // 1
386
386
@@ -412,7 +412,7 @@ But as you've seen with the Lomuto partitioning scheme, if the pivot occurs more
412
412
The code for this scheme is:
413
413
414
414
``` swift
415
- func partitionDutchFlag <T : Comparable >(inout a : [T], low : Int , high : Int , pivotIndex : Int ) -> (Int , Int ) {
415
+ func partitionDutchFlag <T : Comparable >(_ a : inout [T], low : Int , high : Int , pivotIndex : Int ) -> (Int , Int ) {
416
416
let pivot = a[pivotIndex]
417
417
418
418
var smaller = low
@@ -461,7 +461,7 @@ Notice how the two `8`s are in the middle now. The return value from `partitionD
461
461
Here is how you would use it in quicksort:
462
462
463
463
``` swift
464
- func quicksortDutchFlag <T : Comparable >(inout a : [T], low : Int , high : Int ) {
464
+ func quicksortDutchFlag <T : Comparable >(_ a : inout [T], low : Int , high : Int ) {
465
465
if low < high {
466
466
let pivotIndex = random (min : low, max : high)
467
467
let (p, q) = partitionDutchFlag (& a, low : low, high : high, pivotIndex : pivotIndex)
0 commit comments