|
| 1 | +class Search { |
| 2 | + |
| 3 | + static sequentialSearch(dataStore, data) { |
| 4 | + for (var i = 0; i < dataStore.length; i++) { |
| 5 | + if (dataStore[i] === data) { |
| 6 | + return i; |
| 7 | + } |
| 8 | + } |
| 9 | + return -1; |
| 10 | + } |
| 11 | + |
| 12 | + static binarySearch(dataStore, data) { |
| 13 | + let upperBound = dataStore.length - 1; |
| 14 | + let lowerBound = 0; |
| 15 | + while (lowerBound <= upperBound) { |
| 16 | + let mid = Math.floor((lowerBound + upperBound) / 2); |
| 17 | + if(dataStore[mid] < data) { |
| 18 | + lowerBound = mid + 1; |
| 19 | + } else if(dataStore[mid] > data) { |
| 20 | + upperBound = mid - 1; |
| 21 | + } else { |
| 22 | + return mid; |
| 23 | + } |
| 24 | + } |
| 25 | + return - 1; |
| 26 | + } |
| 27 | + |
| 28 | + static countOccurrences(dataStore, data) { |
| 29 | + let count = 0; |
| 30 | + let position = this.binarySearch(dataStore, data); |
| 31 | + if(position > -1) { |
| 32 | + count += 1; |
| 33 | + for (var i = position; i > 0; i--) { |
| 34 | + if(dataStore[i] === data) { |
| 35 | + count += 1; |
| 36 | + } else { |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
| 40 | + for (var i = position + 1; i < dataStore.length; i++) { |
| 41 | + if (dataStore[i] === data) { |
| 42 | + count += 1; |
| 43 | + } else { |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return count; |
| 49 | + } |
| 50 | + |
| 51 | + static minValue(dataStore) { |
| 52 | + let min = dataStore[0]; |
| 53 | + for (var i = 1; i < dataStore.length; i++) { |
| 54 | + if (dataStore[i] < min) { |
| 55 | + min = dataStore[i]; |
| 56 | + } |
| 57 | + } |
| 58 | + return min; |
| 59 | + } |
| 60 | + |
| 61 | + static maxValue(dataStore) { |
| 62 | + let max = dataStore[0]; |
| 63 | + for (var i = 1; i < dataStore.length; i++) { |
| 64 | + if (dataStore[i] > max) { |
| 65 | + max = dataStore[i]; |
| 66 | + } |
| 67 | + } |
| 68 | + return max; |
| 69 | + } |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +function setData(numElements = 100) { |
| 74 | + let dataStore = []; |
| 75 | + for (let i = 0; i < numElements; i++) { |
| 76 | + dataStore[i] = Math.floor(Math.random() * (numElements + 1)); |
| 77 | + } |
| 78 | + return dataStore; |
| 79 | +} |
| 80 | + |
| 81 | +function insertionSort(dataStore) { |
| 82 | + let temp, inner; |
| 83 | + for (let outer = 1; outer <= dataStore.length-1; outer++) { |
| 84 | + temp = dataStore[outer]; |
| 85 | + inner = outer; |
| 86 | + while (inner > 0 && (dataStore[inner-1] >= temp)) { |
| 87 | + dataStore[inner] = dataStore[inner-1]; |
| 88 | + --inner; |
| 89 | + } |
| 90 | + dataStore[inner] = temp; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Implementation |
| 95 | +// ########################################################################## |
| 96 | +const fs = require('fs'); |
| 97 | + |
| 98 | +function readMyData(file) { |
| 99 | + let data = file.split(' '); |
| 100 | + const word = 'rhetoric'; |
| 101 | + let start = new Date().getTime(); |
| 102 | + let position = Search.sequentialSearch(data, word); |
| 103 | + let end = new Date().getTime(); |
| 104 | + console.log((position > 0) ? `${word} is present in the set and is in the position ${position}` : `${word} is not present in the set`); |
| 105 | + console.log(`Sequential search took ${end - start} milliseconds.`); |
| 106 | + console.log(); |
| 107 | + |
| 108 | + insertionSort(data); |
| 109 | + start = new Date().getTime(); |
| 110 | + position = Search.binarySearch(data, word); |
| 111 | + end = new Date().getTime(); |
| 112 | + console.log((position > 0) ? `${word} is present in the set and is in the position ${position}` : `${word} is not present in the set`); |
| 113 | + console.log(`Binary search took ${end - start} milliseconds.`); |
| 114 | +} |
| 115 | + |
| 116 | +const number = 11; |
| 117 | +let dataStore = setData(); |
| 118 | +const result = Search.sequentialSearch(dataStore, number); |
| 119 | +console.log((result > 0) ? `${number} is present in the set and is in the position ${result}` : `${number} is not present in the set`); |
| 120 | +console.log(); |
| 121 | +console.log(`The minimum value is: ${Search.minValue(dataStore)}`); |
| 122 | +console.log(`The maximum value is: ${Search.maxValue(dataStore)}`); |
| 123 | +console.log(); |
| 124 | +insertionSort(dataStore); |
| 125 | +const resBin = Search.binarySearch(dataStore, number); |
| 126 | +const count = Search.countOccurrences(dataStore, number); |
| 127 | +const present = `${number} is present in the set and is in the position ${resBin} and has ${count} occurrences`; |
| 128 | +console.log((resBin > 0) ? present : `${number} is not present in the set`); |
| 129 | +console.log(); |
| 130 | + |
| 131 | +fs.readFile('paragraph.txt', 'utf8', (err, data) => { |
| 132 | + if (err) { |
| 133 | + if (err.code === "ENOENT") { |
| 134 | + console.error('myfile does not exist'); |
| 135 | + return; |
| 136 | + } else { |
| 137 | + throw err; |
| 138 | + } |
| 139 | + } |
| 140 | + readMyData(data); |
| 141 | +}); |
0 commit comments