Beispiel #1
0
        public void DelMax_PQWith3Element_ReturnsGreater()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.Insert(1);
            pq.Insert(3);
            pq.Insert(2);

            Assert.AreEqual(3, pq.DelMax());
            Assert.AreEqual(2, pq.DelMax());
            Assert.AreEqual(1, pq.DelMax());
        }
Beispiel #2
0
        public void IsEmpty_AfterRemovingLastElement_ReturnsTrue()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.Insert(1);
            pq.DelMax();

            Assert.IsTrue(pq.IsEmpty());
        }
Beispiel #3
0
        static long Test(MaxPQ <int> pq, int n)
        {
            // 生成数据
            var initData   = new int[n];
            var appendData = new int[n / 2];

            for (var i = 0; i < n; i++)
            {
                initData[i] = random.Next();
            }
            for (var i = 0; i < n / 2; i++)
            {
                appendData[i] = random.Next();
            }

            // 开始测试
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            // 插入 n 个元素
            for (var i = 0; i < n; i++)
            {
                pq.Insert(initData[i]);
            }
            // 删去一半
            for (var i = 0; i < n / 2; i++)
            {
                pq.DelMax();
            }
            // 插入一半
            for (var i = 0; i < n / 2; i++)
            {
                pq.Insert(appendData[i]);
            }
            // 删除全部
            for (var i = 0; i < n; i++)
            {
                pq.DelMax();
            }

            stopwatch.Stop();
            return(stopwatch.ElapsedMilliseconds);
        }
Beispiel #4
0
        public void DelMax_PQWithOneElement_ReturnsThatElement()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.Insert(1);

            int retVal = pq.DelMax();

            Assert.AreEqual(1, retVal);
        }
        static void Main(string[] args)
        {
            //    P
            //    R P
            //    R P I
            //    R P O I
            //    P O I
            //    R P O I
            //    P O I
            //    O I
            //    O I I
            //    I I
            //    T I I
            //    I I
            //    Y I I
            //    I I
            //    I
            //
            //    Q
            //    U Q
            //    U Q E
            //    Q E
            //    E
            //
            //    U
            //
            //    E
            //

            MaxPQ <char> pq    = new MaxPQ <char>();
            string       input = "P R I O * R * * I * T * Y * * * Q U E * * * U * E";

            foreach (char c in input)
            {
                if (c == ' ')
                {
                    continue;
                }
                else if (c == '*')
                {
                    pq.DelMax();
                }
                else
                {
                    pq.Insert(c);
                }

                foreach (char n in pq)
                {
                    Console.Write(n + " ");
                }
                Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            // 输入格式: buy 20.05 100
            var buyer  = new MaxPQ <Ticket>();
            var seller = new MinPQ <Ticket>();

            var n = int.Parse(Console.ReadLine());

            for (var i = 0; i < n; i++)
            {
                var ticket = new Ticket();
                var item   = Console.ReadLine().Split(' ');

                ticket.Price = double.Parse(item[1]);
                ticket.Share = int.Parse(item[2]);
                if (item[0] == "buy")
                {
                    buyer.Insert(ticket);
                }
                else
                {
                    seller.Insert(ticket);
                }
            }

            while (!buyer.IsEmpty() && !seller.IsEmpty())
            {
                if (buyer.Max().Price < seller.Min().Price)
                {
                    break;
                }
                var buy  = buyer.DelMax();
                var sell = seller.DelMin();
                Console.Write("sell $" + sell.Price + " * " + sell.Share);
                if (buy.Share > sell.Share)
                {
                    Console.WriteLine(" -> " + sell.Share + " -> $" + buy.Price + " * " + buy.Share + " buy");
                    buy.Share -= sell.Share;
                    buyer.Insert(buy);
                }
                else if (buy.Share < sell.Share)
                {
                    sell.Share -= buy.Share;
                    seller.Insert(sell);
                    Console.WriteLine(" -> " + buy.Share + " -> $" + buy.Price + " * " + buy.Share + " buy");
                }
                else
                {
                    Console.WriteLine(" -> " + sell.Share + " -> $" + buy.Price + " * " + buy.Share + " buy");
                }
            }
        }
Beispiel #7
0
        public void TestMaxPQ() {
            var arr = CreateRandomArray(10);
            Insertion<int>.Sort(arr);
            StdOut.WriteLine(arr);

            var pq = new MaxPQ<int>();
            foreach (int i in arr) {
                pq.Insert(i);
            }

            while (!pq.IsEmpty) {
                StdOut.WriteLine("delete max: {0}", pq.DelMax());
            }
        }
Beispiel #8
0
        public void TestMaxPQ()
        {
            int amounts = 10;

            MaxPQ pq = new MaxPQ(amounts);

            Random rand = new Random();
            for (int i = 0; i < amounts; i++)
                pq.Insert(rand.Next(0, 1000).ToString());

            Debug.WriteLine(Sort.Show(pq.PQs));
            pq.DelMax();
            Debug.WriteLine(Sort.Show(pq.PQs));
        }
        public void TestAdd()
        {
            int[] a = Range(100);
            Shuffle(a);
            MaxPQ <int> pq = new MaxPQ <int>();

            pq.Add(10);
            Assert.AreEqual(10, pq.Peek());
            pq.Add(20);
            Assert.AreEqual(20, pq.Peek());
            for (int i = 0; i < 20; ++i)
            {
                pq.Add(i);
                Assert.AreEqual(i + 3, pq.Count);
                Assert.AreEqual(20, pq.Peek());
            }
            Assert.AreEqual(20, pq.DelMax());
            Assert.AreEqual(19, pq.DelMax());
            foreach (int val in a)
            {
                pq.Add(val);
            }
            Assert.AreEqual(99, pq.Peek());
        }
        static long Test(MaxPQ <int> pq, int[] data)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < data.Length; i++)
            {
                pq.Insert(data[i]);
            }
            for (int i = 0; i < data.Length; i++)
            {
                pq.DelMax();
            }
            sw.Stop();
            return(sw.ElapsedMilliseconds);
        }
Beispiel #11
0
        public void TestMaxPQ()
        {
            var arr = CreateRandomArray(10);

            Insertion <int> .Sort(arr);

            StdOut.WriteLine(arr);

            var pq = new MaxPQ <int>();

            foreach (int i in arr)
            {
                pq.Insert(i);
            }

            while (!pq.IsEmpty)
            {
                StdOut.WriteLine("delete max: {0}", pq.DelMax());
            }
        }
Beispiel #12
0
        public void TestMaxPQ()
        {
            var pq = new MaxPQ <int>();

            for (var i = 0; i < 100; ++i)
            {
                pq.Enqueue(i);
            }
            Assert.Equal(100, pq.Count);
            Assert.False(pq.IsEmpty);
            for (var i = 0; i < 100; ++i)
            {
                Assert.Equal(99 - i, pq.DelMax());
                Assert.Equal(99 - i, pq.Count);
            }
            Assert.True(pq.IsEmpty);

            foreach (var v in pq)
            {
                console.WriteLine("{0}", v);
            }
        }
Beispiel #13
0
        static int Test(int n, MaxPQ <int> pq)
        {
            var timer = new Timer(1000);

            timer.Elapsed += new ElapsedEventHandler(StopRunning);
            for (var i = 0; i < n; i++)
            {
                pq.Insert(random.Next());
            }

            var delCount = 0;

            StartRunning();
            timer.Start();
            while (isRunning && !pq.IsEmpty())
            {
                pq.DelMax();
                delCount++;
            }
            timer.Stop();
            return(delCount);
        }
        static void Main(string[] args)
        {
            var pq  = new MaxPQLinked <char>();
            var pq2 = new MaxPQ <char>();
            // 利用 2.4.6 的输入做测试
            var input = "P R I O * R * * I * T * Y * * * Q U E * * * U * E";

            foreach (var c in input)
            {
                if (c == ' ')
                {
                    continue;
                }
                else if (c == '*')
                {
                    Console.WriteLine(pq.DelMax() + " " + pq2.DelMax());
                }
                else
                {
                    pq.Insert(c);
                    pq2.Insert(c);
                }
            }
        }
Beispiel #15
0
	public IEnumerable<int> KSE_MaxHeap(int[] A, int K){
		var pq = new MaxPQ(A);
				
		for(int i=0; i<K; i++)
			yield return pq.DelMax();
	}
Beispiel #16
0
        public void DelMax_EmptyPQ_ThrowsExceltion()
        {
            MaxPQ <int> pq = new MaxPQ <int>(100);

            pq.DelMax();
        }