Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace VickreyAuction
- {
- /// <summary>
- /// A max Heap that has many functionalities
- /// </summary>
- public class Heap
- {
- /// <summary>
- /// The List/Heap
- /// </summary>
- List<KeyValuePair<int, string>> lst;
- /// <summary>
- /// Init the Heap
- /// </summary>
- public Heap()
- {
- lst = new List<KeyValuePair<int, string>>();
- }
- /// <summary>
- /// Insert a new bid into auction
- /// </summary>
- /// <param name="entry">The auction bid to insert: bid + bidder</param>
- public void Insert(KeyValuePair<int, string> entry)
- {
- lst.Add(entry);
- Sort();
- }
- /// <summary>
- /// Get the max bid made in the auction
- /// </summary>
- /// <returns>The max bid</returns>
- public KeyValuePair<int, string> MaxBid() => lst.First();
- /// <summary>
- /// Get the 'k' most biggest bid
- /// </summary>
- /// <param name="k">number of max bid</param>
- /// <returns>The 'k' numbered bid</returns>
- public KeyValuePair<int, string> KLargestBid(int k) => lst[k - 1];
- /// <summary>
- /// Sort the Heap by max
- /// </summary>
- private void Sort()
- {
- //Size of Heap
- int n = lst.Count;
- for (int i = n / 2 - 1; i >= 0; i--)
- Heapify(n, i);
- // One by one extract an element from heap
- for (int i = n - 1; i >= 0; i--)
- {
- // Move current root to end
- var temp = lst[0];
- lst[0] = lst[i];
- lst[i] = temp;
- // call max heapify on the reduced heap
- Heapify(i, 0);
- }
- }
- // To heapify a subtree rooted with node i which is
- // an index in lst. n is size of heap
- public void Heapify(int n, int i)
- {
- int largest = i; // Initialize largest as root
- int left = 2 * i + 1; // left = 2*i + 1
- int right = 2 * i + 2; // right = 2*i + 2
- if (left < n && lst[largest].Key > lst[left].Key)
- largest = left;
- if (right < n && lst[largest].Key > lst[right].Key)
- largest = right;
- // If largest is not root
- if (largest != i)
- {
- var swap = lst[i];
- lst[i] = lst[largest];
- lst[largest] = swap;
- // Recursively heapify the affected sub-tree
- Heapify(n, largest);
- }
- }
- /// <summary>
- /// Print the list
- /// </summary>
- public void PrintArray()
- {
- for (int i = 0; i < lst.Count; ++i)
- Console.Write($"{lst[i]} ");
- Console.WriteLine();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var auctionManager = new Heap();
- auctionManager.Insert(new KeyValuePair<int, string>(300, "Tzvi"));
- auctionManager.Insert(new KeyValuePair<int, string>(200, "Itzik"));
- auctionManager.Insert(new KeyValuePair<int, string>(400, "Itzik"));
- auctionManager.Insert(new KeyValuePair<int, string>(400, "Itzik"));
- auctionManager.Insert(new KeyValuePair<int, string>(700, "Tzvi"));
- Console.WriteLine($"Max Val = {auctionManager.MaxBid()}");
- Console.WriteLine($"K(=2) Max Val = {auctionManager.KLargestBid(2)}");
- Console.WriteLine($"Sorted Array:");
- auctionManager.PrintArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement