Reorders the elements of the collection such that all the elements that match the given predicate are after all the elements that don’t match.
Required. Default implementation provided.
SDK
- Xcode 10.0+
Framework
- Swift Standard Library
Declaration
Parameters
belongsInSecondPartition
A predicate used to partition the collection. All elements satisfying this predicate are ordered after all elements not satisfying it.
Return Value
The index of the first element in the reordered collection that matches belongs
. If no elements in the collection match belongs
, the returned index is equal to the collection’s end
.
Discussion
After partitioning a collection, there is a pivot index p
where no element before p
satisfies the belongs
predicate and every element at or after p
satisfies belongs
.
In the following example, an array of numbers is partitioned by a predicate that matches elements greater than 30.
The numbers
array is now arranged in two partitions. The first partition, numbers[..<p]
, is made up of the elements that are not greater than 30. The second partition, numbers[p...]
, is made up of the elements that are greater than 30.
Complexity: O(n), where n is the length of the collection.