Skip to content

Commit

Permalink
Merge pull request kodecocodes#902 from freak4pc/quicksort-swap
Browse files Browse the repository at this point in the history
Use swapAt for Lomuto's partitioning
  • Loading branch information
kelvinlauKL authored Dec 3, 2019
2 parents 055cb68 + 0904c23 commit 942064d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Quicksort/Quicksort.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int
var i = low
for j in low..<high {
if a[j] <= pivot {
(a[i], a[j]) = (a[j], a[i])
a.swapAt(i, j)
i += 1
}
}

(a[i], a[high]) = (a[high], a[i])
a.swapAt(i, high)
return i
}

Expand Down
4 changes: 2 additions & 2 deletions Quicksort/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int
var i = low
for j in low..<high {
if a[j] <= pivot {
(a[i], a[j]) = (a[j], a[i])
a.swapAt(i, j)
i += 1
}
}

(a[i], a[high]) = (a[high], a[i])
a.swapAt(i, high)
return i
}
```
Expand Down

0 comments on commit 942064d

Please sign in to comment.