Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 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. using System.Diagnostics;
  7.  
  8. namespace ConsoleApp9
  9. {
  10. class Program
  11. {
  12. public static int OpAssignment;
  13. public static int OpComparisonLT;
  14. public static int OpIncrement;
  15. public static int OpComparisonEQ;
  16. static void Main(string[] args)
  17. {
  18.  
  19.  
  20. Console.WriteLine("Ilosc liniowa Czas");
  21. for(int i = 1; i<=10;i++)
  22. {
  23. wywolajLiniowa(26843545 * i);
  24. }
  25. Console.WriteLine("Ilosc binarna Czas");
  26. for (int i = 1; i<=10;i++)
  27. {
  28. wywolajBinarna(26843545 * i);
  29. }
  30.  
  31. Console.ReadLine();
  32. }
  33. static void wywolajBinarna(int max_liczba)
  34. {
  35. int[] tablica_liczb = new int[max_liczba];
  36. for (int i = 0; i < max_liczba; i++)
  37. {
  38. tablica_liczb[i] = i;
  39. }
  40. IsPresentBinary(tablica_liczb, max_liczba - 1);
  41. liczCzasBinarny(tablica_liczb, max_liczba - 1);
  42. }
  43. static double liczSrednia(int[] Vector, int Number)
  44. {
  45. int suma = 0;
  46. for(int i = 0; i <= Vector.Length; i ++)
  47. {
  48. suma += i;
  49. }
  50. Console.WriteLine(suma +" "+ Vector.Length);
  51. double wynik = suma / Vector.Length;
  52. return wynik;
  53. }
  54. static bool IsPresentBinary(int[] Vector, int Number)
  55. {
  56. int Left = 0, Right = Vector.Length - 1, Middle;
  57. int licznik = 0;
  58. while (Left <= Right)
  59. {
  60. licznik++;
  61. Middle = (Left + Right) / 2;
  62. if (Vector[Middle] == Number)
  63. {
  64. Console.Write(licznik);
  65. return true;
  66. }
  67. else if (Vector[Middle] > Number) Right = Middle - 1;
  68. else Left = Middle + 1;
  69. }
  70. return false;
  71. }
  72. static void wywolajLiniowa(int max_liczba)
  73. {
  74. int[] tablica_liczb = new int[max_liczba];
  75. for (int i = 0; i < max_liczba; i++)
  76. {
  77. tablica_liczb[i] = i;
  78. }
  79. IsPresent(tablica_liczb,max_liczba - 1);
  80. liczCzasLiniowy(tablica_liczb,max_liczba - 1);
  81. }
  82. static bool IsPresent(int[] Vector, int Number)
  83. {
  84. OpAssignment = OpComparisonLT = 1;
  85. for (int i = 0; i <= Vector.Length; i++, OpIncrement++)
  86. {
  87. OpComparisonEQ++;
  88. if (Vector[i] == Number)
  89. {
  90. Console.Write(OpComparisonLT);
  91. return true;
  92. }
  93. OpComparisonLT++;
  94. }
  95.  
  96. return false;
  97. }
  98. static bool liczCzasLiniowy(int[] Vector, int Number)
  99. {
  100. long StartingTime = Stopwatch.GetTimestamp();
  101. for (int i = 0; i <= Vector.Length; i++, OpIncrement++)
  102. {
  103. if (Vector[i] == Number)
  104. {
  105. long EndingTime = Stopwatch.GetTimestamp();
  106. Console.WriteLine(" "+liczCzas(StartingTime, EndingTime));
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. static bool liczCzasBinarny(int[] Vector, int Number)
  113. {
  114. int Left = 0, Right = Vector.Length - 1, Middle;
  115. long StartingTime = Stopwatch.GetTimestamp();
  116. while (Left <= Right)
  117. {
  118. Middle = (Left + Right) / 2;
  119. if (Vector[Middle] == Number)
  120. {
  121. long EndingTime = Stopwatch.GetTimestamp();
  122. Console.WriteLine(" " + liczCzas(StartingTime, EndingTime));
  123. return true;
  124. }
  125. else if (Vector[Middle] > Number) Right = Middle - 1;
  126. else Left = Middle + 1;
  127. }
  128. return false;
  129. }
  130. static double liczCzas(long StartingTime, long EndingTime)
  131. {
  132. long ElapsedTime = EndingTime - StartingTime;
  133. double ElapsedSeconds = ElapsedTime * (1.0 / Stopwatch.Frequency);
  134. return Math.Round(ElapsedSeconds,4);
  135. }
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement