Advertisement
Guest User

C# auction

a guest
Jan 26th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace VickreyAuction
  8. {
  9. /// <summary>
  10. /// A max Heap that has many functionalities
  11. /// </summary>
  12. public class Heap
  13. {
  14. /// <summary>
  15. /// The List/Heap
  16. /// </summary>
  17. List<KeyValuePair<int, string>> lst;
  18.  
  19. /// <summary>
  20. /// Init the Heap
  21. /// </summary>
  22. public Heap()
  23. {
  24. lst = new List<KeyValuePair<int, string>>();
  25. }
  26.  
  27. /// <summary>
  28. /// Insert a new bid into auction
  29. /// </summary>
  30. /// <param name="entry">The auction bid to insert: bid + bidder</param>
  31. public void Insert(KeyValuePair<int, string> entry)
  32. {
  33. lst.Add(entry);
  34. Sort();
  35. }
  36.  
  37. /// <summary>
  38. /// Get the max bid made in the auction
  39. /// </summary>
  40. /// <returns>The max bid</returns>
  41. public KeyValuePair<int, string> MaxBid() => lst.First();
  42.  
  43. /// <summary>
  44. /// Get the 'k' most biggest bid
  45. /// </summary>
  46. /// <param name="k">number of max bid</param>
  47. /// <returns>The 'k' numbered bid</returns>
  48. public KeyValuePair<int, string> KLargestBid(int k) => lst[k - 1];
  49.  
  50.  
  51. /// <summary>
  52. /// Sort the Heap by max
  53. /// </summary>
  54. private void Sort()
  55. {
  56. //Size of Heap
  57. int n = lst.Count;
  58.  
  59. for (int i = n / 2 - 1; i >= 0; i--)
  60. Heapify(n, i);
  61.  
  62. // One by one extract an element from heap
  63. for (int i = n - 1; i >= 0; i--)
  64. {
  65. // Move current root to end
  66. var temp = lst[0];
  67. lst[0] = lst[i];
  68. lst[i] = temp;
  69.  
  70. // call max heapify on the reduced heap
  71. Heapify(i, 0);
  72. }
  73. }
  74.  
  75. // To heapify a subtree rooted with node i which is
  76. // an index in lst. n is size of heap
  77. public void Heapify(int n, int i)
  78. {
  79. int largest = i; // Initialize largest as root
  80. int left = 2 * i + 1; // left = 2*i + 1
  81. int right = 2 * i + 2; // right = 2*i + 2
  82.  
  83. if (left < n && lst[largest].Key > lst[left].Key)
  84. largest = left;
  85.  
  86. if (right < n && lst[largest].Key > lst[right].Key)
  87. largest = right;
  88.  
  89. // If largest is not root
  90. if (largest != i)
  91. {
  92. var swap = lst[i];
  93. lst[i] = lst[largest];
  94. lst[largest] = swap;
  95.  
  96. // Recursively heapify the affected sub-tree
  97. Heapify(n, largest);
  98. }
  99. }
  100.  
  101. /// <summary>
  102. /// Print the list
  103. /// </summary>
  104. public void PrintArray()
  105. {
  106. for (int i = 0; i < lst.Count; ++i)
  107. Console.Write($"{lst[i]} ");
  108. Console.WriteLine();
  109. }
  110. }
  111. class Program
  112. {
  113. static void Main(string[] args)
  114. {
  115. var auctionManager = new Heap();
  116. auctionManager.Insert(new KeyValuePair<int, string>(300, "Tzvi"));
  117. auctionManager.Insert(new KeyValuePair<int, string>(200, "Itzik"));
  118. auctionManager.Insert(new KeyValuePair<int, string>(400, "Itzik"));
  119. auctionManager.Insert(new KeyValuePair<int, string>(400, "Itzik"));
  120. auctionManager.Insert(new KeyValuePair<int, string>(700, "Tzvi"));
  121. Console.WriteLine($"Max Val = {auctionManager.MaxBid()}");
  122. Console.WriteLine($"K(=2) Max Val = {auctionManager.KLargestBid(2)}");
  123. Console.WriteLine($"Sorted Array:");
  124. auctionManager.PrintArray();
  125.  
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement