Sources/Algorithms/Documentation.docc/Partitioning.md
Partition a collection according to a unary predicate, rotate a collection around a particular index, or find the index where a collection is already partitioned.
A stable partition maintains the relative order of elements within both partitions.
// partition(by:) - unstable ordering
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let p1 = numbers.partition(by: { $0.isMultiple(of: 20) })
// p1 == 4
// numbers == [10, 70, 30, 50, 40, 60, 20, 80]
// ^ start of second partition
// stablePartition(by:) - maintains relative ordering
numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let p2 = numbers.stablePartition(by: { $0.isMultiple(of: 20) })
// p2 == 4
// numbers == [10, 30, 50, 70, 20, 40, 60, 80]
// ^ start of second partition
Use the rotate method to shift the elements of a collection to start at a new position, moving the displaced elements to the end:
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let p = numbers.rotate(toStartAt: 2)
// numbers == [30, 40, 50, 60, 70, 80, 10, 20]
// p == 6 -- numbers[p] == 10
Swift/MutableCollection/stablePartition(by:)Swift/MutableCollection/stablePartition(subrange:by:)Swift/Sequence/partitioned(by:)Swift/Collection/partitioned(by:)Swift/MutableCollection/partition(subrange:by:)-5vdh7Swift/MutableCollection/partition(subrange:by:)-4gpqzSwift/Collection/partitioningIndex(where:)Swift/MutableCollection/rotate(toStartAt:)-9fp48Swift/MutableCollection/rotate(toStartAt:)-2r55jSwift/MutableCollection/rotate(subrange:toStartAt:)-ov6aSwift/MutableCollection/rotate(subrange:toStartAt:)-5teoqSwift/MutableCollection/reverse(subrange:)