Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. const howFast = function howFast(func, params) {
  2. const start = performance.now();
  3. func(params);
  4. const end = performance.now();
  5.  
  6. return (end - start).toFixed(10) + ' ms';
  7.  
  8. }
  9.  
  10. const swap = function swap(array, first, second) {
  11. let temp = array[first];
  12. array[first] = array[second];
  13. array[second] = temp;
  14. }
  15.  
  16. const print = function print(array) {
  17. console.log(arrToString(array));
  18. }
  19.  
  20.  
  21. const arrToString = function arrToString(array) {
  22. return array.join(' - ');
  23. }
  24.  
  25. const createNodeWithContent = function createNodeWithContent(htmlElement, content) {
  26.  
  27. if (typeof htmlElement !== 'string') throw new Error('Please enter a valid html element!');
  28.  
  29. const newNode = document.createElement(htmlElement);
  30. newNode.appendChild(document.createTextNode(content));
  31.  
  32. return newNode;
  33. }
  34.  
  35. const test = function test(functionToTest, testCase, label) {
  36.  
  37. const contentNode = document.getElementById('sorting');
  38.  
  39. const labelNode = createNodeWithContent('h2', label);
  40. const resultNode = createNodeWithContent('p', functionToTest(testCase));
  41. const timeNode = createNodeWithContent('small', howFast(functionToTest, testCase))
  42.  
  43. contentNode.appendChild(labelNode);
  44. contentNode.appendChild(resultNode);
  45. contentNode.appendChild(timeNode);
  46. contentNode.appendChild(document.createElement('hr'));
  47. }
  48.  
  49. const selectionSort = function selectionSort(array) {
  50. let min;
  51.  
  52. for (let i = 0; i < array.length; i++) {
  53. min = i;
  54.  
  55. for (let j = i + 1; j < array.length; j++) {
  56. if (array[j] < array[min]) {
  57. min = j;
  58. }
  59. }
  60.  
  61. if (i != min) {
  62. swap(array, i, min);
  63. }
  64. }
  65.  
  66. return arrToString(array);
  67. }
  68.  
  69. const bubbleSort = function bubbleSort(array) {
  70. for (let k = 1; k < array.length; k++) {
  71. let flag = false;
  72.  
  73. // to avoid sorting on sorted part
  74. // loop from 0 to n-k
  75. for (let i = 0; i < array.length - k; i++) {
  76. if (array[i] > array[i + 1]) {
  77. // swap two adjacent elements
  78. swap(array, i, i + 1);
  79.  
  80. flag = true;
  81. }
  82. }
  83. if (flag === false) break;
  84. }
  85.  
  86. return arrToString(array);
  87. }
  88.  
  89. const insertionSort = function insertionSort(array) {
  90. for (let i = 1; i < array.length; i++) {
  91. let value = array[i];
  92.  
  93. // index of the comparing value
  94. let holeIndex = i;
  95.  
  96. while (holeIndex > 0 && array[holeIndex] > value) {
  97.  
  98. // keep going backward until the right place is found
  99. array[holeIndex] = array[holIndex - 1];
  100. holeIndex -= 1;
  101. }
  102.  
  103. // put the comparing value to the right position
  104. array[holeIndex] = value;
  105. }
  106.  
  107. return arrToString(array);
  108. }
  109.  
  110.  
  111. const mergeSort = function mergeSort(array) {
  112.  
  113. const merge = function merge(left, right) {
  114.  
  115. let rightIndex = 0,
  116. leftIndex = 0,
  117. result = [];
  118.  
  119. while (leftIndex < left.length && rightIndex < right.length) {
  120.  
  121. if (left[leftIndex] <= right[rightIndex]) {
  122.  
  123. result.push(left[leftIndex]);
  124. leftIndex++;
  125. } else {
  126.  
  127. result.push(right[rightIndex]);
  128. rightIndex++;
  129. }
  130. }
  131.  
  132. return arrToString(result
  133. .concat(left.slice(leftIndex))
  134. .concat(right.slice(rightIndex)));
  135. }
  136.  
  137. const arrayLength = array.length;
  138.  
  139. if (arrayLength < 2) {
  140. return array;
  141. }
  142.  
  143. let midPoint = Math.floor(arrayLength / 2);
  144.  
  145. const left = array.slice(0, midPoint);
  146. const right = array.slice(midPoint);
  147.  
  148. mergeSort(left);
  149. mergeSort(right);
  150.  
  151. return merge(left, right);
  152. }
  153.  
  154. const interativeBNS = function interativeBNS(sortedArray, target) {
  155.  
  156. let start = 0,
  157. end = sortedArray.length - 1;
  158.  
  159. while (start <= end) {
  160.  
  161. let mid = Math.floor((start + end) / 2);
  162.  
  163. if (target === sortedArray[mid]) {
  164.  
  165. return mid;
  166. } else if (target < sortedArray[mid]) {
  167.  
  168. end = mid - 1;
  169. } else {
  170.  
  171. start = mid + 1;
  172. }
  173. }
  174.  
  175. return -1;
  176. }
  177.  
  178. const recursiveBNS = function recursiveBNS(sortedArray, target, start = 0, end = sortedArray.length - 1) {
  179.  
  180. if (start > end) return 'Error 404: Not found.';
  181.  
  182. let mid = Math.floor((start + end) / 2);
  183.  
  184. if (target === sortedArray[mid]) {
  185.  
  186. return mid;
  187. } else if (target < sortedArray[mid]) {
  188.  
  189. end = mid - 1;
  190. } else {
  191.  
  192. start = mid + 1;
  193. }
  194.  
  195. return recursiveBNS(sortedArray, target, start, end);
  196. }
  197.  
  198. /**
  199. * Find the {first} or {last} occurence of a target number in a sorted array.
  200. *
  201. * @param {Array} sortedArray
  202. * @param {number} target The number to search for.
  203. * @param {string} condition Either 'first' or 'last'.
  204. * @param {number} result The index of the number that we are looking for.
  205. * @param {number} start Start index. Defautl 0.
  206. * @param {number} end End index. Default array.length - 1
  207. */
  208. const findOccurence = function findOccurence(sortedArray, target, condition = 'first', result = -1, start = 0, end = sortedArray.length - 1) {
  209.  
  210. if (start > end) {
  211.  
  212. if (result === -1) {
  213.  
  214. return 'Error 404: Not found.';
  215. } else {
  216.  
  217. return result;
  218. }
  219. }
  220.  
  221. let mid = Math.floor((start + end) / 2);
  222.  
  223. if (target === sortedArray[mid]) {
  224.  
  225. result = mid;
  226.  
  227. if (condition === 'first') {
  228.  
  229. // if we want to find the first occurence, keep going to the left
  230. end = mid - 1;
  231. } else if (condition === 'last') {
  232.  
  233. // if we want to find the last occurence, keep going to the right
  234. start = mid + 1;
  235. } else {
  236.  
  237. throw new Error('The condition must either be `last` or `first`!')
  238. }
  239. } else if (target < sortedArray[mid]) {
  240.  
  241. end = mid - 1;
  242. } else {
  243.  
  244. start = mid + 1;
  245. }
  246.  
  247. return findOccurence(sortedArray, target, condition, result, start, end);
  248. }
  249.  
  250. ///////////////////////////////////////////////////////////////////////////////////////////////////
  251.  
  252. const array = [6, 3, 9, 2, 1];
  253. const sortedArray = [2, 4, 6, 7, 8, 9, 9, 10];
  254. const testCaseNode = document.createTextNode(arrToString(array));
  255. document.getElementById('test-case').appendChild(testCaseNode);
  256.  
  257. test(bubbleSort, array, 'Bubble Sort');
  258. test(selectionSort, array, 'Selection Sort');
  259. test(insertionSort, array, 'Insertion Sort');
  260. test(mergeSort, array, 'Merge Sort');
  261.  
  262.  
  263. console.log(findOccurence(sortedArray, 9, 'last'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement