static void Main(string[] args) { var data = new SwappableData(5, 2, 7, 4, 1, 6, 3); PartitionStrategy partition = PartitionUtil.DoublePointerPartition; Quicksort(data, 0, data.Length - 1, partition); Console.WriteLine(data); }
static void Quicksort(SwappableData data, int begin, int end, PartitionStrategy partition) { if (begin < end) { var mid = partition(data, begin, end); Quicksort(data, begin, mid, partition); Quicksort(data, mid + 1, end, partition); } }
private int GetNextSegmentIndex(List <Bounds> boundsSegments, PartitionStrategy strategy) { switch (strategy) { case PartitionStrategy.Random: { int index = UnityEngine.Random.Range(0, boundsSegments.Count - 1); return(index); } case PartitionStrategy.Ratio: { int selectedIndex = 0; float smallestRatio = boundsSegments[selectedIndex].size.x > boundsSegments[selectedIndex].size.z ? boundsSegments[selectedIndex].size.x / boundsSegments[selectedIndex].size.z : boundsSegments[selectedIndex].size.z / boundsSegments[selectedIndex].size.x; smallestRatio = boundsSegments[selectedIndex].size.magnitude; for (int i = 1; i < boundsSegments.Count; i++) { float currRatio = boundsSegments[i].size.x > boundsSegments[i].size.z ? boundsSegments[i].size.x / boundsSegments[i].size.z : boundsSegments[i].size.z / boundsSegments[i].size.x; currRatio = boundsSegments[selectedIndex].size.magnitude; if (currRatio < smallestRatio) { smallestRatio = currRatio; selectedIndex = i; } } return(selectedIndex); } case PartitionStrategy.Size: { int index = 0; float largestSize = boundsSegments[0].size.x * boundsSegments[0].size.z; for (int i = 1; i < boundsSegments.Count; i++) { float currSize = boundsSegments[i].size.x * boundsSegments[i].size.z; if (currSize > largestSize) { largestSize = currSize; index = i; } } return(index); } default: throw new System.ArgumentException("Unsupported PartitionStrategy " + strategy); } }