Example #1
0
        /// <summary>
        /// Heap sort with custom comparer.
        /// </summary>
        /// <typeparam name="T">Type of value</typeparam>
        /// <param name="array">Array to sort</param>
        /// <param name="comparer">Comparer</param>
        public static void HeapSort <T>(T[] array, IComparer <T> comparer)
        {
            if (array == null)
            {
                throw new ArgumentNullException("Array is null");
            }
            if (comparer == null)
            {
                throw new ArgumentNullException("Comparer is null");
            }
            var heap = new BinaryHeap <T>(comparer, array);

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = heap.Pop();
            }
        }
Example #2
0
        private void DijkstraImpl(Node start, Func <Node, Edge, bool> relaxationFunc)
        {
            var heap = new BinaryHeap <Node>(CreateNodeDistanceComparer(), CreateBinaryHeapChangeIndexNotifier());

            start.Distance = 0;
            heap.PushRange(nodes);
            while (heap.Count > 0)
            {
                var node = heap.Pop();
                if (node.Distance == double.MaxValue)
                {
                    break; // cannot continue, not accessible
                }
                foreach (var edge in node.OutEdges)
                {
                    if (relaxationFunc.Invoke(node, edge))
                    {
                        var target = edge.NodeTo.ThisNode as Node;
                        heap.ReplacedOnIndex((int)target.ProgramData);
                    }
                }
            }
        }