示例#1
0
        /// <summary>
        /// Sort an array that has an out-of-order element atmost k distance away from its correct position.
        /// </summary>
        /// <param name="A">Array</param>
        /// <param name="k">k distance</param>
        /// <returns></returns>
        public static int[] SortAlmostSortedArray(int[] A, int k)
        {
            int i    = 0;
            var heap = new MyHeap <int>(k + 1, new HeapItemComparer <int>());

            while (i <= k)
            {
                heap.Insert(A[i++]);
            }

            for (int j = 0; j < A.Length; j++)
            {
                A[j] = heap.Remove();
                if (i < A.Length)
                {
                    heap.Insert(A[i++]);
                }
            }

            return(A);
        }
示例#2
0
        public static Planet[] Get_K_ClosestPlanetsFromEarth(int k, StreamReader sr)
        {
            var heap = new MyHeap <Planet>(k, new HeapItemComparer <Planet>(true));

            while (heap.Count() < k)
            {
                var p = GetNextPlanet(sr);
                if (p == null)
                {
                    break;
                }

                heap.Insert(p);
            }

            while (!sr.EndOfStream)
            {
                var p = GetNextPlanet(sr);
                if (p == null)
                {
                    break;
                }

                var currentMaxFromHeap = heap.Peek();
                // if planet p has EarthDistance lesser than the MaxDistancePlanet from heap,
                // we should remove the MaxDistancePlanet (root) and add p.
                if (p.CompareTo(currentMaxFromHeap) < 0)
                {
                    var r = heap.Remove();
                    Console.WriteLine($"Removed Planet = {r}");
                    heap.Insert(p);
                    Console.WriteLine($"Inserted Planet = {p}");
                }
            }

            return(heap.GetData());
        }
示例#3
0
        static void Main(string[] args)
        {
            ConsoleKeyInfo choice = default(ConsoleKeyInfo);

            while (true)
            {
                Console.WriteLine("Enter new patient?");
                choice = Console.ReadKey();

                if (choice.Key == ConsoleKey.N)
                {
                    break;
                }

                if (patientPriorityQueue.Count() == 10)
                {
                    Console.WriteLine("Queue is FULL..");
                    break;
                }

                Console.WriteLine("Name:");
                var name = Console.ReadLine();
                Console.WriteLine("Criticality:");
                Console.WriteLine("1.Trauma, 2.HeartAttack, 3.Breathlessness, 4.Concussion, 5. MinorAilments");
                var     criticalilty = Console.ReadKey();
                Patient p            = new Patient()
                {
                    Name = name,
                    Id   = ++counter
                };

                switch (criticalilty.Key)
                {
                case ConsoleKey.NumPad1:
                case ConsoleKey.D1:
                    p.Priority = (counter * (int)CriticalFactor.Trauma);
                    break;

                case ConsoleKey.NumPad2:
                case ConsoleKey.D2:
                    p.Priority = (counter * (int)CriticalFactor.HeartAttack);
                    break;

                case ConsoleKey.NumPad3:
                case ConsoleKey.D3:
                    p.Priority = (counter * (int)CriticalFactor.Breathlessness);
                    break;

                case ConsoleKey.NumPad4:
                case ConsoleKey.D4:
                    p.Priority = (counter * (int)CriticalFactor.Concussion);
                    break;

                default:
                    p.Priority = (counter * (int)CriticalFactor.MinorAilments);
                    break;
                }

                patientPriorityQueue.Insert(p);

                //Thread.Sleep(TimeSpan.FromSeconds(20));
            }

            while (!patientPriorityQueue.IsEmpty)
            {
                var nextPatient = patientPriorityQueue.Remove();
                Console.WriteLine($"Name: {nextPatient.Name}, Priority={nextPatient.Priority}");
            }
            // create a PQueue of patients
            //var enterPatientTask = Task.Factory.StartNew(() => {
            //    while (true)
            //    {
            //        Console.WriteLine("Enter new patient?");
            //        choice = Console.ReadKey();
            //        if (choice.Key == ConsoleKey.N)
            //            break;

            //        Console.WriteLine("Name:");
            //        var name = Console.ReadLine();
            //        Console.WriteLine("Criticality:");
            //        Console.WriteLine("1.Trauma, 2.HeartAttack, 3.Breathlessness, 4.Concussion, 5. MinorAilments");
            //        var criticalilty = Console.ReadKey();
            //        Patient p = new Patient()
            //        {
            //            Name = name,
            //            Id = ++counter
            //        };

            //        switch(criticalilty.Key)
            //        {
            //            case ConsoleKey.NumPad1:
            //            case ConsoleKey.D1:
            //                p.Priority = (counter * (int)CriticalFactor.Trauma);
            //                break;
            //            case ConsoleKey.NumPad2:
            //            case ConsoleKey.D2:
            //                p.Priority = (counter * (int)CriticalFactor.HeartAttack);
            //                break;
            //            case ConsoleKey.NumPad3:
            //            case ConsoleKey.D3:
            //                p.Priority = (counter * (int)CriticalFactor.Breathlessness);
            //                break;
            //            case ConsoleKey.NumPad4:
            //            case ConsoleKey.D4:
            //                p.Priority = (counter * (int)CriticalFactor.Concussion);
            //                break;
            //            default:
            //                p.Priority = (counter * (int)CriticalFactor.MinorAilments);
            //                break;
            //        }

            //        patientPriorityQueue.Insert(p);

            //        Thread.Sleep(TimeSpan.FromSeconds(20));
            //    }
            //});

            //var getNextPatientTask = Task.Factory.StartNew(() => {
            //    while (!patientPriorityQueue.IsEmpty)
            //    {
            //        var nextPatient = patientPriorityQueue.Remove();
            //        Console.WriteLine("Next Patient");
            //        Console.WriteLine("-----------------------");
            //        Console.WriteLine($"Name: {nextPatient.Name}, Priority={nextPatient.Priority}");
            //        Thread.Sleep(TimeSpan.FromSeconds(25));
            //    }
            //});

            //var tasks = new Task[] { enterPatientTask , getNextPatientTask };
            //Task.WaitAll(tasks);
        }