SwapTrackingSort<'a, 'coeff> Type
A selection sort implementation that tracks position changes for computing phase factors.
This class implements selection sort with a twist: after each element is moved to its sorted position, a tracking function is called with the number of positions the element moved. This is crucial for fermionic operators where each adjacent swap introduces a factor of -1.
For fermionic normal ordering, if an element at position i is moved to position 0, it effectively swapped past i elements, contributing a phase of (-1)^i.
Example
// Create a sort that counts total swaps
let swapCounter = SwapTrackingSort((<=), 0, fun pos count -> count + pos)
let (sorted, totalSwaps) = swapCounter.Sort 0 [| 3; 1; 2 |]
// sorted = [| 1; 2; 3 |], totalSwaps = 2
Constructors
| Constructor |
Description
|
Full Usage:
SwapTrackingSort(compareFunction, zero, trackingFunction)
Parameters:
'a -> 'a -> bool
zero : 'a
trackingFunction : int -> 'coeff -> 'coeff
Returns: SwapTrackingSort<'a, 'coeff>
|
|
Instance members
| Instance member |
Description
|
Full Usage:
this.IsSorted
Parameters:
'a[]
-
The array to check.
Returns: bool
True if the array is sorted (each element compares favorably with its successor); false otherwise.
|
Checks whether an array is already sorted according to the comparison function. An empty array or single-element array is considered sorted. This can be used to skip sorting when the input is already in order.
|
Full Usage:
this.Sort
Parameters:
'coeff
-
The initial coefficient/phase value before sorting.
inputArray : 'a[]
-
The array to sort (not modified; a new sorted array is returned).
Returns: 'a array * 'coeff
A tuple of (sorted array, final coefficient) where the final coefficient
reflects all position tracking accumulated during the sort.
|
Sorts the input array while tracking position changes via the tracking function. The algorithm repeatedly finds the minimum element in the remaining unsorted portion and moves it to the sorted portion. Each time an element at position i is selected, the tracking function is called with i and the current phase, simulating i adjacent swaps.
|