Skip to content

Commit 0904c23

Browse files
committed
Use swapAt for Lomuto's partitioning
1 parent 055cb68 commit 0904c23

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Quicksort/Quicksort.playground/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int
4242
var i = low
4343
for j in low..<high {
4444
if a[j] <= pivot {
45-
(a[i], a[j]) = (a[j], a[i])
45+
a.swapAt(i, j)
4646
i += 1
4747
}
4848
}
4949

50-
(a[i], a[high]) = (a[high], a[i])
50+
a.swapAt(i, high)
5151
return i
5252
}
5353

Quicksort/README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int
137137
var i = low
138138
for j in low..<high {
139139
if a[j] <= pivot {
140-
(a[i], a[j]) = (a[j], a[i])
140+
a.swapAt(i, j)
141141
i += 1
142142
}
143143
}
144144

145-
(a[i], a[high]) = (a[high], a[i])
145+
a.swapAt(i, high)
146146
return i
147147
}
148148
```

0 commit comments

Comments
 (0)