Advertisement
XyloGaming

: )

Oct 12th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. KROK 1
  2. /* Returns either the index of the location in the array,
  3. or -1 if the array did not contain the targetValue */
  4. var doSearch = function(array, targetValue) {
  5. var min = 0;
  6. var max = array.length - 1;
  7. var guess;
  8.  
  9. while (min <= max) {
  10. guess = Math.floor((max+min)/2);
  11. if (array[guess] === targetValue){
  12. return guess;
  13. } else if (array[guess] < targetValue) {
  14. min = guess + 1;
  15. } else {
  16. max = guess - 1;
  17. }
  18. }
  19.  
  20.  
  21. return -1;
  22. };
  23.  
  24. var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
  25. 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
  26.  
  27. var result = doSearch(primes, 73);
  28. println("Found prime at index " + result);
  29.  
  30. Program.assertEqual(doSearch(primes, 73), 20);
  31.  
  32. ==================================================================================================================
  33. KROK 2
  34.  
  35. /* Returns either the index of the location in the array,
  36. or -1 if the array did not contain the targetValue */
  37. var doSearch = function(array, targetValue) {
  38. var min = 0;
  39. var max = array.length - 1;
  40. var guess;
  41.  
  42. while (min <= max) {
  43. guess = Math.floor((max+min)/2);
  44. println(guess);
  45. if (array[guess] === targetValue){
  46. return guess;
  47. } else if (array[guess] < targetValue) {
  48. min = guess + 1;
  49. } else {
  50. max = guess - 1;
  51. }
  52. }
  53.  
  54.  
  55. return -1;
  56. };
  57.  
  58. var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
  59. 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
  60.  
  61. var result = doSearch(primes, 73);
  62. println("Found prime at index " + result);
  63.  
  64. Program.assertEqual(doSearch(primes, 73), 20);
  65.  
  66. ===================================================================================================
  67.  
  68. KROK 3
  69.  
  70. /* Returns either the index of the location in the array,
  71. or -1 if the array did not contain the targetValue */
  72. var doSearch = function(array, targetValue) {
  73. var min = 0;
  74. var max = array.length - 1;
  75. var guess;
  76. var guessNumber = 1;
  77.  
  78. while (min <= max) {
  79. guess = Math.floor((max+min)/2);
  80. println(guess);
  81. if (array[guess] === targetValue){
  82. println(guessNumber);
  83. return guess;
  84. } else if (array[guess] < targetValue) {
  85. min = guess + 1;
  86. } else {
  87. max = guess - 1;
  88. }
  89. guessNumber++;
  90. }
  91.  
  92.  
  93. return -1;
  94. };
  95.  
  96. var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
  97. 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
  98.  
  99. var result = doSearch(primes, 73);
  100. println("Found prime at index " + result);
  101.  
  102. Program.assertEqual(doSearch(primes, 73), 20);
  103.  
  104. ==================================================================================================
  105.  
  106. KROK 4
  107.  
  108. /* Returns either the index of the location in the array,
  109. or -1 if the array did not contain the targetValue */
  110. var doSearch = function(array, targetValue) {
  111. var min = 0;
  112. var max = array.length - 1;
  113. var guess;
  114. var guessNumber = 1;
  115.  
  116. while(min <= max) {
  117. guess = Math.floor((max + min)/2);
  118. println(guess);
  119. if (array[guess] === targetValue) {
  120. println(guessNumber);
  121. return guess;
  122.  
  123. } else if (array[guess] < targetValue) {
  124. min = guess + 1;
  125. } else {
  126. max = guess -1;
  127. }
  128. guessNumber++;
  129. }
  130.  
  131. return -1;
  132.  
  133. };
  134.  
  135. var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
  136. 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
  137.  
  138. var result = doSearch(primes, 73);
  139. println("Found prime at index " + result);
  140.  
  141. Program.assertEqual(doSearch(primes, 73), 20);
  142. Program.assertEqual(doSearch(primes, 61), 17);
  143. Program.assertEqual(doSearch(primes, 89), 23);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement